• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Malini Santhanam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody let me know when an object is eligible for garbage collection with some examples?

Thanks in advance,
Malini
 
Divij Mahajan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Malini,

An object is said to be ready for garbage collection whenever it is not holding any references. The JVM may then execute the Garabge Collector "at will" and remove such unreferenced objects from memory.

Another scenario is when a gc may need to free up memory for other objects. At this time it may remove objects in a given order (depends on JVM), say in order of last usage time or if the object is holding weak references.

Finally, a developer may want to call gc at a specific time and thats where one uses calls to methods of the GC or System class explicitly.(ref api)

-- A coding best practice related to GC is to call the finalize method, wherein references held by objects, that are no longer in use can be released. Finalise method is executed just before gc completes execution. But beware that this method may not be called everytime so try using alternatives.

Lemme know if you need more details.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic