• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Garbage collection Q

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you ensure that the memory allocated by an object is freed. Select the one correct answer.

a.By invoking the free method on the object.
b.By calling system.gc() method.
c.By setting all references to the object to new values (say null).
d.Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object.

Ans is given d.

I agree with d, but i think c is also correct. - Any other suggestions please?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think again thbs is based on the 'Gauranteed Behavior' . So even if the Objects are made eligible for GC by nulling the reference, only if the GC is scheduled, the memory could be recovered for sure.??!!!
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If references are not set to null, and gc is called, then it is of no use. So, i think basic requirement before calling to System.gc() is to set reference to null. There is no user of calling System.gc() without clearning object reference.

On the otherhand, even if you are not calling System.gc(), system will invoke when ever memory is low.
Thank you
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"I agree with d, but i think c is also correct. - Any other suggestions please?"

The wording of Choice d if selected logically excludes all the other choices. That said, I think the correct choice is d because garbage collecton cannot be forced.

"If references are not set to null, and gc is called, then it is of no use. So, i think basic requirement before calling to System.gc() is to set reference to null."

No. An object is also eligible for garbage collection if, for example, all references to the object go out of scope or the reference is no longer accessible by a live thread.
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you J Borderi, nice explanation
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's not possible to force garbage collection.invoking System.gc may start garbage collection process.thus any memory allocated during program execution might remain allocated after program termination,unless reclaimed by OS or by other means.
so D is correct.
but can anybody explain me following statement :
An object is only eligible for garbage collection,if the only references to the object r from other objects that r also eligible for garbage collection.
(pls use some code in explanation)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic