• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage collection

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please any one clearly explain the garbage collection for me..


i know that the object which is not having an active reference is eligible GC

can any one explain with the sample code like how many objects are eligible for GC.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are two ways to tell the system when is a good time to collect the garbage. you can search them online.

Class1 obj1 = new Class1();
Class1 obj2 = new Class1();
obj1=obj2;

obj1 is pointing the second object now,and now you can collect the first object.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, Arif!

Welcome to the Java Ranch!

Hope you enjoy it.


For the others: some notes about garbage collection:

First, regard that only objects are garbage collected. Sometimes beginners tend to try to garbage collect variables.

Second main thing: an object is eligible for garbage collection when no live thread can access it.

As in Arif's example: no other variable refers the first object, so it is useless. The object is still in memory, but nobody can use it. It can be GCd.


Citing myself:
is eligible for garbage collection

Means, that the garbage collector may dump the object. You cannot do it by hand, as garbage collection works automatically in background in a daemon thread.
You cannot force it, but you can kind of persuade it to do it's job.

Therefore in all this questions, it says "is eligible for gc". When an answer to a question is "is garbage collected" then it the answer is possibly false, as you cannot tell, when the thing is dumped actually.


For "island of isolation" follow the hint from Arif and search for it.


Yours,
Bu.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think "Dog Catcher"!

If the town dog catcher drives the streets of town all day and sees people walking dogs on a leash (meaning referenced dogs, a dog object held referenced to a person by a leash) he/she will simply ignore them and keep driving.

As soon as a dog becomes "unreferenced" (person let go of that leash, or let go to grab another leash) then the dog is just hanging around unreferenced. The next time the dog catcher happens by, THAT dog is elligible to be picked up and taken back to the pound.

This is a pretty wide stretch I know.... but it gives an idea. The garbage collector keeps track of objects and references. When all references to an object go away (either nulled, or replaced by another object reference, or the reference variable dies) then the object is elligible for garbage collection.
 
Have you no shame? Have you no decency? Have you no tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic