• Post Reply Bookmark Topic Watch Topic
  • New Topic

Once again on gc()  RSS feed

 
amit bhadre
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How the gc() come to know that the objects are no longer usable ? There may be case of objects later may be used....then big dragging back of JVM...is int it...? :-)





["If C & C++ are sea and Java is Ocean/Galaxies/ whole Universe"]

thanks in advance

cheers
amit bhadre
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How garbage collectors work is a big topic, but very roughly, modern ones are based on an idea called "mark and sweep." The collector will start at a set of known "root references." These include all static variables and all variables in all stack frames of all runnable threads. The collector looks at every one of these, and any objects these variables point to are "marked". Then all the member variables of all the marked objects are looked at, and all the member variables of those objects, and every visited object is marked. Eventually, you get to the "leaves" of the big object tree and then you're done.

Now, elsewhere, imagine that Java has its own secret list of all the objects ever allocated. The collector can then go through that list. Any unmarked object on that list can be garbage collected, since it's impossible for any part of the running program to refer to it.

Now, this is a simplified view. The collector won't do all of this at once -- it would take too long. And there may not really be that list -- there may be other ways of finding the unmarked objects. But that's the basic idea.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add more:

The garbage collector runs in low memory situations. When it runs, it releases the memory allocated by an unreachable object. The garbage collector runs on a low priority daemon (background) thread. You can nicely ask the garbage collector to collect garbage by calling System.gc() but you can�t force it.

What is an unreachable object? An object�s life has no meaning unless something has reference to it. If you can�t reach it then you can�t ask it to do anything. Then the object becomes unreachable and the garbage collector will figure it out. Java automatically collects all the unreachable objects periodically and releases the memory consumed by those unreachable objects to be used by the future reachable objects.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!