• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain Garbage Collection

 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember - the best practice to solve that type of questions is to sketch a graph of references and objects
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic