How does garbage collection work

Showing Answers 1 - 11 of 11 Answers

Pankaj Dwivedi

  • Jun 16th, 2005
 

There are several basic strategies for garbage collection: reference counting, mark-sweep, mark-compact, and copying. In addition, some algorithms can do their job incrementally (the entire heap need not be collected at once, resulting in shorter collection pauses), and some can run while the user program runs (concurrent collectors). Others must perform an entire collection at once while the user program is suspended (so-called stop-the-world collectors). Finally, there are hybrid collectors, such as the generational collector employed by the 1.2 and later JDKs, which use different collection algorithms on different areas of the heap

  Was this answer useful?  Yes

chinnasamy

  • Jul 27th, 2005
 

what r the properties of struts framework?

  Was this answer useful?  Yes

Moulali Basha

  • Aug 13th, 2005
 

In java Objects are dynamically allocated memory by using the new operator but memory is not destroyed manually, java will automatically delete this when there is no reference to that object. that is the object is no longer needed. the memory occupied by the object can be reclaimed. this is called "garbage collectyion"

  Was this answer useful?  Yes

Pavan

  • Sep 1st, 2005
 

Garbage Collection is a robust feature of Java. There is no question of destructors in Java like in C++. Whenever an object is going out of scope it will be Garbage Collected. Actually it is a low priority thread running always behind the scenes. W e can explicitly call the Garbage Collector by the command System.gc();. but it is not sure that it will collect the garbage instantly. It will be done whenever it is possible...........

  Was this answer useful?  Yes

kamruddin

  • Sep 11th, 2005
 

Garbage collector act as Destructor. it is automatically to remove th memory allocation of an object

ss

  • Sep 29th, 2005
 

It acts as a destructor

  Was this answer useful?  Yes

Khilan

  • Oct 2nd, 2005
 

Garbage collection is the mechanism for reclaiming or free up the unused memory.it is similler to destructor in C++.only difference is that in C++ its programmer's responsibility to write the code for destroy the objects(which are not being used), but in java this process has been automated, for that in java a lower priority thread has been defined (called garbage collector) which  runs in background and checkes that there are any object that doesn't have any live reference or any object that is not being referenced from the code then this object is eligible to be garbage collected.so it will free up the memory that was using by the object and this memory can be re-used.

but we cann't force garbage collection in java, there is a method system.gc(), this method will try to force garbage collection.

rvs_rs

  • Oct 3rd, 2005
 

When Java was originally developed, the JDK shipped with a mark-and-sweep garbage collector. A mark-and-sweep garbage collector proceeds in two phases:

  1. Mark: identifies garbage objects
  2. Sweep: reclaims the memory for the garbage objects

Garbage objects are identified by traversing references from the current application stack frames; unreachable objects are assumed to be garbage.

Mark and sweep is a "stop-the-world" garbage collection technique; that is, all application threads stop until garbage collection completes, or until a higher-priority thread interrupts the garbage collector. If the garbage collector is interrupted, it must restart, which can lead to application thrashing with little apparent result. The other problem with mark and sweep is that many types of applications can't tolerate its stop-the-world nature. That is especially true of applications that require near real-time behavior or those that service large numbers of transaction-oriented clients.

Because of these problems, Sun Microsystems' Java HotSpot VM split the heap into three sections and added three garbage collection techniques. Splitting the heap allows different algorithms to be used for newly created objects and for objects that have been around for a while. This technique is based on the observation that most Java objects are small and short-lived. The heap's three sections are:

  1. Permanent space: used for JVM class and method objects
  2. Old object space: used for objects that have been around a while
  3. New (young) object space: used for newly created objects

The new object space is further subdivided into three parts: Eden, where all newly created objects go, and survivor spaces 1 and 2, where objects go before they become old. The survivor spaces make it easier to use copy-compaction with young objects; more details later.

The J2SE 1.3 garbage collection techniques are:

  1. Copy-compaction: used for new object space.

  2. Mark-compact: used in old object space. Similar to mark and sweep, mark-compact marks all unreachable objects; in the second phase, the unreachable objects compact. This technique avoids fragmentation problems and works well when the garbage collector runs infrequently.

  3. Incremental garbage collection (optional): Incremental GC creates a new middle section in the heap, which divides into multiple trains. Garbage is reclaimed from each train one at a time. This provides fewer, more frequent pauses for garbage collection, but it can decrease overall application performance. Incremental garbage collection can be enabled with the -Xincgc command-line option.

All of these techniques are stop-the-world techniques. Though incremental garbage collection makes this effect less obvious, the application threads must still stop. That proves problematic for applications that can't afford to pause for garbage collection.

Garbage collection is based on live objects; that is, those reachable from the current stack space. Live objects are copied from new object space to survivor space (1 or 2), and then from survivor space to old object space. The amount of time objects spend in survivor space can be controlled with command-line parameters (see Tables 2 and 3 below).

The garbage collector typically runs in a low-priority thread, attempting to reclaim memory when the application is idle. This is fine for applications that regularly have idle time, such as graphical user interface (GUI)-driven applications. Unfortunately, if there is little or no idle time, the garbage collector may not get a chance to run.

Garbage collection can also be triggered if the heap's subregions are nearly full. In this case, the garbage collection thread's priority increases, thus increasing the chance that the garbage collection will run to completion. If the new generation is full, a minor collection is triggered; if the old generation is full, a major collection is triggered. The steps in a minor collection are:

  1. Copy objects from Eden to survivor space (1 or 2).
  2. Copy from survivor space 1 to survivor space 2, or vice versa. After a certain number of copies (controllable from the command line), an object becomes tenured, that is, a candidate for old object space.
  3. Tenured objects move from survivor space 1 or 2 to old object space.

A major collection uses the old generation garbage collector (mark-compact for J2SE 1.3) to reclaim old objects.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions