Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Garbage Collection Eligibility

 
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a question from Khalid Mughal's SCJP book:

How many objects are eligible for garbage collection when control reaches (1) ?



Select the one correct answer:

(a) 0
(b) 5
(c) 10
(d) Hard to say.

My answer was (c) 10.
But the book says the answer is (d) Hard to say. The reasoning is that some of the object's could have been GC'ed when control reaches (1).
I see the point - some objects could have been automatically GC'ed before we explicitly call GC.

My question is that is this a valid way to think about GC questions on the real exam ? I would assume that ten objects will be eligible for GC as soon as the loop ends.

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

generates an anonymous Eligible object which is immediately eligible for garbage collection. After the first time the loop body has been executed

removes the only reference obj was pointing to before, making the old Eligible object immediately eligible for garbage collection.
So 9 objects are eligible for gc even before the for loop finishes. How do you know garbage collection at least of some objects has not already taken place when
is reached? But, yes I was also puzzled whether they want to hear '10' or 'hard to say'.

John
 
Saloon Keeper
Posts: 13493
305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid you're mistaken John. At the end of every iteration of the loop, 2 more objects will be eligible (assuming no GC takes place in the mean time).

The variable obj goes out of scope after the iteration, so the reference is lost even before it gets overwritten with a new object.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with stephen in this situation. The obj reference variable is lost after each iteration, hence you arnt sure what has been already GC or what needs to be at the end of the for loop.
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right, sorry.

John
 
Suhrid Karthik
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone. The book's answer makes sense now.
reply
    Bookmark Topic Watch Topic
  • New Topic