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

Garbage Collection (Mala Gupta's OCA Java SE7, Mock Exam Question 45)

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code I think there is only one object eligible for garbage collection after Line 10 executes but the book I got it from says two objects are available. Since c2.var is a reference variable it doesn't count as an object does it? c1 is still referring to an object so the only isolated object is the one c2 used to refer to. Am I missing something? Thanks!
P.S. When you get to Line 12 both objects would be eligible because they were made inside the main method, correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:but the book I got it from

Could you please QuoteYourSources? Thanks!

Kendall Ponder wrote:says two objects are available

So according to the book there are 2 object available => no objects eligible for GC

Kendall Ponder wrote:Since c2.var is a reference variable it doesn't count as an object does it?

Indeed! Only actual objects, created using the new operator should be taken into account. But be careful: you can create primitive wrapper instances, strings and arrays without using the new keyword and they are all objects which could be eligble for GC

Kendall Ponder wrote:c1 is still referring to an object so the only isolated object is the one c2 used to refer to. Am I missing something?

I don't think you are missing something: with the code as-is you are correct, 1 object eligible for GC. But what if I made a (very) small adjustment to the existing code snippet?How many objects will now be eligible for GC after Line 10 executes?

Kendall Ponder wrote:P.S. When you get to Line 12 both objects would be eligible because they were made inside the main method, correct?

Yes!

Hope it helps!
Kind regards,
Roel
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example is from the mock exam in Mala Gupta's book. Maybe I'm copying it down wrong (or I misunderstood her explanation) but I think I have the structure right. When I said I thought 1 object was available for GC but the book said two were available, I meant the book said 2 were available for GC. With your changes no objects would be available on Line 10 because c1.var can get you to the second object. Thanks!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:The example is from the mock exam in Mala Gupta's book.

You are talking about ME-Q45 I believe (but you changed the class names to make it harder for me to find the appropriate question )

Kendall Ponder wrote:Maybe I'm copying it down wrong (or I misunderstood her explanation) but I think I have the structure right. When I said I thought 1 object was available for GC but the book said two were available, I meant the book said 2 were available for GC.

The mistake in the explanation is already (also) confirmed in this thread.

Kendall Ponder wrote:With your changes no objects would be available on Line 10 because c1.var can get you to the second object.

Excellent!
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Next time I will keep the same names!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic