• Post Reply Bookmark Topic Watch Topic
  • New Topic

About Garbage Collection  RSS feed

 
vinay kalal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning about the Garbage Collection.I have a doubt about following code that how many object in this code are eligible for Garbage Collection.Answer is two, but don't know why and which objects.please help.
 
Stephan van Hulst
Saloon Keeper
Posts: 7962
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c1 and c1.story are eligible.

c1 and c2 are both assigned a new CardBoard object, both objects hold a Short object referred to by story. That's 4 objects. If you look at the method go(), you will see it does nothing at all, except return null. So c3 is assigned null. No new objects.

Then c1 is nulled out, so it and its member story are eligible, because there are no more references to either.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These can be tricky, but just try to remember the rule is that an object is eligible for garbage collection when there are no more references to it. (There are some complications to this rule dealing with reference chains, and client/server programing, but we safely ignore those for now.)

We have two places in this example where references are set to null. The trick is that the one at line 4 (in the go() method) doesn't cause an object to become eligible for GC, while the one at line 11 does. To understand why, you have to understand a bit about the internal workings of Java. On line 10, we make a call to go() passing it in c2. This c2 is the only reference to second CardBoard object we created. However, in making the method call, Java makes a copy of the reference. It's this copy that it sets to null, so when the method returns, you will see that c2 is still a reference to the second CardBoard object, just as before.

However, when line 11 set the c1 reference to null, that's it for the first CardBoard object. Nothing else refers to it, so it's lost to the program and is eligible for garbage collection. What's the second object? All instance members of an object cannot be garbage collected while the containing object has a reference to it, but once the containing object is eligible for garbage collection, then its members may also be, as long as they aren't shared by some other object. In this case, the Short (story), is an object, not a literal, so it can be collected as well. (Whether it actually is or not is a separate matter. Some JVMs treat small integer values differently, but I'm not sure if this applies to the Short class or not.)

On Edit: what Stephan said.
 
Stephan van Hulst
Saloon Keeper
Posts: 7962
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And everything Greg said

Also, if you want to look deeper into it, read this: http://www.brpreiss.com/books/opus5/html/page424.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!