jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection..." Watch "garbage collection..." New topic
Author

garbage collection...

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362


i have seen these type of code and he is asking that after which line object created in line1 is eligible for garbage collection?

i am confused here

one object is created at line 1

after executing line 2 the instance var of that object is referencing itself...

LINE3: when a1=null then a1.a is null as a result object is eligible for GC or is it still referenced by a1.a?

my thought is that here a is an instance variable of object a1 so when a1=null then a1.a also null

am i right?



SCJP5 and SCWCD1.5
Think Twice Act Wise...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
no a1.a is not null, it is pointing to object created at line1, but you cannot reach a1.a now, it is now unreachable code.

In your code after executing line3, only one object is eligible for GC.


SCJP 6
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

After line 2


After line 3


my thought is that here a is an instance variable of object a1 so when a1=null then a1.a also null


Not really,
when a1 is set to null, a is not set to null.
But since a is reachable only through a1, the GC decides that "since a1 is not reachable , no other object can access a1.a. Which implies that the object created at line 1 is not reachable. so it deletes the object."


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

sorry for posting a similar reply (like punit singh).
His post came few seconds befrore my post.
I think Punit has made the point clear
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
Hi punit

are you sure?

here Ony one object is referenced by a1 as well as a1.a(instance variable of a1)..

if a1.a is not null then how can you say that one object is eligible for GC...?
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
i have seen that

when reference variable is null then instance varaible of that particular object which is pointing to the same object is also becomes null

in old posts...

can anyone give me final statement for my doubt?
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Static A a;

make this change.And answer yourself.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
when reference variable is null then instance varaible of that particular object which is pointing to the same object is also becomes null


I have not read such statement anywhere, but I have read one concept that is
island of isolation. That is detected by GC.

See some discussion here in GC doubt.
Ashika Chhabda
Greenhorn

Joined: Dec 29, 2008
Posts: 3
Hi,

a1 will not be garbage collected because till a reference is pointing to and Onject instance in memory or pointing to null object i will not eligible for GC.
only when any new instance of a class is created and not reffered or pointed by any ref var , it will be GC.

example:
A a1 = null; // it will be eligible for GC but not gauranteed ti be GC immediately so it will Gc when exits the pgm.
B b1 = new B(); // will not be GC coz pointing to null object.
new B(); //will be GC coz it is not reffered by any ref var.
[ December 29, 2008: Message edited by: Ashika Chhabda ]
Paul Kemp
Ranch Hand

Joined: Dec 03, 2008
Posts: 35
I'm a bit confused about this so can we take a step back!

The issue here is that after line 2 there are 2 objects on the heap: a1.a and a1.

If a1 is set to null, a1.a still continues to point to an object.

My understanding is that an object is eligible for garbage collection if it is "unreachable".
What does "unreachable" mean here? As a programmer I can't reach a1.a after line 3 because I'll get an NPE, so from my point of view a1.a is unreachable. So you could say both a1 and a1.a are unreachable and eligible for gc (2 objects are eligible after line 3).

However according to Khalid and Mungal, "an object in the heap is said to be reachable if it is denoted by any local reference in a runtime stack". So though we as programmers can't reach it, there's still a local reference to a1.a, so unless that is explicitly set to null, it is still reachable and not eligible for gc (1 object eligible after line 3).
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
My understanding is that an object is eligible for garbage collection if it is "unreachable".
What does "unreachable" mean here? As a programmer I can't reach a1.a after line 3 because I'll get an NPE, so from my point of view a1.a is unreachable. So you could say both a1 and a1.a are unreachable and eligible for gc (2 objects are eligible after line 3).


No no you are going on wrong track.
First of all remember a1 and a1.a are not objects, they are just a reference to an object.
Here a1 and a1.a both referencing to same object that was created on line 1.

A a1=new A();=====================LINE1

So question is here, if you are creating just one object on the heap, how the GC will collect two objects from the heap?
 
wood burning stoves
 
subject: garbage collection...
 
Similar Threads
eligible for garbage collection
Taking out the Garbage!
Doubt in Garbage Collection
is this object eligible for garbage collection??
when will it be Garbage collected?