| Author |
Please explain Garbage Collection
|
indra negi
Ranch Hand
Joined: Aug 12, 2009
Posts: 68
|
|
Hi,
I am not able to guess the correct answer for a question which asks about the number of objects that will be applicable for garbage collection after a particular line is executed. Please can somebody guide me for that.
Thanks in advance.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
A lot of people have problem with this. Search the forum and you'll find a lof of results which might be helpful. You might choose any strategy that people use. Just go through a few of the posts and you might find a strategy for these questions useful...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
indra negi
Ranch Hand
Joined: Aug 12, 2009
Posts: 68
|
|
Thanks Ankit.
Well I tried to understand that by reviewing those questions however I don't understand the main logic behind that. I have gone through the K&B book once related to that topic but could not make it out.
Please can anybody help me in understanding that.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Suppose there is a code like this
Now what you need to solve this question is to develop a virtual memory representation of the objects created. So the memory map after line (1) would be
But when a1 is set to null, the memory representation changes to this
So here there is an island of isolation and thus 2 objects are eligible for GC i.e. the ones on the left...
|
 |
indra negi
Ranch Hand
Joined: Aug 12, 2009
Posts: 68
|
|
Usually I get confused when there are so many objects which are getting assigned to one another.
What will happen if Class B contains an additional object apart from String s, let's say of class A.
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
Remember - the best practice to solve that type of questions is to sketch a graph of references and objects
|
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
|
 |
Mo Jay
Ranch Hand
Joined: Feb 16, 2009
Posts: 83
|
|
Sorry Ankit Garg but only one object will be garbage collected and that's the object that a1 was pointing to.
With regard to the B object with string s ref in it, that object was pointed to by b (from the stack) and it is still referred to by b which I didn't see in your diagram. The following statement : a1.obj = b; all it does is just copies the reference pattern of b to a1.obj but b is still pointing to B object with s string instance variable, thus only one object is collected.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Yes, I created the diagram wrong for the code, here is the revised code and diagrams
at (1)
after a1 is set to null
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Let me try to give a complicated example for indra (this time I'll try not to make a mistake )
Now lets try to build a memory map till line (1)
then at line (2), b1 is changed so the map becomes
after line (3), a2.b is set to null, so it becomes
at line (4), another reference variables starts pointing to a1's object
at 5, a1's (also a3's) b is set to null
And finally at (6), a3 is set to null
I hope this makes sense, the diagrams a little repetitive but that's how I find solution to such questions...
|
 |
Vince Kennedy
Greenhorn
Joined: Aug 26, 2009
Posts: 20
|
|
Ankit,
Thanks for the diagrams but I'm having a little trouble understanding exactly how to build these - especially when the instances inside of a class are another class.
Question, in your example how many objects are ready for GC? 3?
When it says something like A a1 = new A(); even if A has instances of B or C, those don't have objects until they are set to something or a constructor is used? I was using ExamLab and I'm not entirely sure why I got the diagramming wrong on this example:
If someone could help me out it'd be greatly appreciated.
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
Well its the same as passing an object reference to a method. Here you will be passing the reference to the constructor. In the constructor, for that instance it can be given to an instance reference. If you clear this thing in your concept, rest of the thing will fall into place. Also what object is eligible for gc is the one which is not referenced from a live thread. Also creating object, go from right to left i.e first evaluate the new and then assign that to a reference. Use the same analogy for multiple objects in the same line.
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
 |
|
|
subject: Please explain Garbage Collection
|
|
|