• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with garbage collection and references!

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a question regarding the following question from Marcus Green:

Given the following code, how many objects will be eligible for garbage collection on the line with the comment //here



The answer to this question is 0. I thought it should have been 2, because by the time the //here is reached, there are no longer any references inside or outside of this method that will refer to the 2 existing Integer objects again. I thought that when this was the case, the garbage collector could go ahead and collect those objects which have no further references in the code. Is this not the case?

Thanks again, Karen
[ September 30, 2005: Message edited by: Karen Jirak ]
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No objects will be eligible for GC since , Integer(10) is still referenced by x
Even though Z a reference varible is set to null, Integer(99) is still referenced by y.

In the findOutMethod() only the dummyVariable is set to null
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you are passing object 'x' to findOut() , the object created for Integer(10) is now referenced by two instance variables one is the member variable & the other one is the local variable.
when you make the local variable to null the the object for Integer(10) is still referenced by x the member variable.
 
Karen Jirak
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srinivasa,

Thanks for the help, but as far as I can tell, they are all local variables, and so as soon as they are not referenced again (by the time \\here comes about), any objects referenced by them, and only them, should be eligible for garbage collection. I am wondering if I am misunderstanding this - is it not true that even if there are still references to an object which are in the same block, yet the JVM has passed the point of any more references to that object, are they not eligible to be garbage collected?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karen,
See what happens in the findOut() method are you creating any new object , No .. The same object is referenced by two variables. This object is eligible for garbage collection only if bot the variables are set to null, or gone out of scope.

If you change the findout() method in this way. an object gets eligible for GC
 
Karen Jirak
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srinivasa,

Thanks again for your help and I apologize if I am being dense . I think I understand what you are saying, but I don't think that I am making the source of my confusion clear. Let me give a simpler example:

The above shows my current understanding. I thought that the Integer referenced by aa could be collected at //1, since the method never references aa again, and the object referenced by aa is not referenced by any other variable, as well. In other words, I thought the garbage collector would know that that object would never be used again, and that would make it eligible for collection. However, the Integer referenced by bb cannot be collected until //3, since there is a final reference to the bb object there. Is this a misunderstanding on my part?

Thank you, Karen
 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karen,I have explained the same type of problem in detailtwo times before .
Now i will suggest you always make a graphical diagram for this type of problem where you denote reference as dot(.) and object as box.Try and tell me result
[ September 30, 2005: Message edited by: agrah upadhyay ]
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karen, Your are not on the right track.
For problems like this , as in the previous post , represent all objects & references pictorially.

What you should do is draw a circle in a paper for each object created and make pointers to it, when ever you are assigining a variable to a object. Strike of that pointer when any object goes out of scope or when any variable is set to null.
An object is said to be eligible for GC when it soesnt have any pointers.


Integer aa = new Integer(1);
//1 to my undertanding, aa can be collected here
Integer bb = new Integer(2);


No aa cannot be collected. Because the scope off aa still exists even after the line where you created the bb object.
 
Karen Jirak
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Srinivasa and Argah,

Thank you both so much! I do think that I understand clearly now. I cannot remember what source made me think objects can be collected like that, but I always seemed to get gc questions wrong due to that misunderstanding. Now I can be clear . It is scope, and not just if any references to the object appear again in the same scope.

Luck to you both, Karen
[ September 30, 2005: Message edited by: Karen Jirak ]
reply
    Bookmark Topic Watch Topic
  • New Topic