Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about Garbage Collection in heap memory again

 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy java ranchers,

I made this program based on Q45 in the end of book full mock exam in the excellent Gupta book. What I am wondering is if a reference is set to null and the reference points to an object that has another object reference that points to a different object is this object also eligible for garbage collection since it can only be accessed through the reference that was explicitly set to null?





After the main method runs all objects are eligible for garbage collection since the program is done correct?

I should probably re-read the section on garbage collection instead of bombard the OCAJP7 forum with my specific questions. I hope this is not completely annoying spam.

Respectfully,

Ted
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/* at this point a2.member is referencing the A object a1 initially referenced in this case does both a1 and a2.member point to the same object on the heap? */

Yes

// all these method calls print the same thing

Yes, only because you have given a general print statement in method which do not contain any specific information about object state.
For example if you change SOP to be as System.out.println("instance method in class A :: " + this); . Using this statement, you shall get different output now as Object toString representation shall be printed. Try this change and see the result yourself.

since a2 is set to null does this also make a2.member eligible for garbage collection?

Object represented by a2.member reference shall only be eleigible for GC if it is not being used by any other live reference in JVM.

I guess a2.member is not reachable without an a2 reference...

Yes. Once object is set to null, its state variables are not accessible any more

a2.member.method(); this results in a NullPointerException so I guess this is eligible for garbage collection

This results in NullPointer because a2 has been set to null in previous line of code. So any operation [ in this case, accessing its state variable] on null results in NullPointerException.

end of main method all objects created in this method eligible for garbage collection right? Especially since they are not passed to other objects outside of main?

Yes because we have not given any object references to any external JVM thread
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More over, Feel free to post all your java query in this forum. Peoples here shall be more than happy to solve your queries.
~ abhay
 
Ted North
Ranch Hand
Posts: 201
1
Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay,

Thank-you for your detailed help and stepping through the lines of code with your java knowledge.

I re-read the few pages of GC in the SCJP6 book by Bates & Sierra and this makes more sense now. I think the specific problem where an object has an instance variable that also points to another object and the 'outer' object that holds the instance member reference is set to null is called the "islands of isolation" (Sierra & Bates, p. 259) paradigm. This is basically where objects references that point to objects that have instance variables that point to other objects are inaccessible because of the fact that the reference to them is set to null so that these 'inner' object members are not reachable since the main reference to the object is lost.

Abhay Agarwal wrote:

More over, Feel free to post all your java query in this forum. Peoples here shall be more than happy to solve your queries.
~ abhay


Thank-you. This is awesome to read.

Respectfully,

Ted
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic