Garbage Collection

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Garbage Collection as PDF for free.

More details

  • Words: 1,035
  • Pages: 19
Garbage Collection

Garbage Collection ■ The Java platform allows creation of as many objects as you want and you don't have to worry about destroying them. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

When ■ An object is eligible for garbage collection when there are no more references to that object. ■ References that are held in a variable are usually dropped when the variable goes out of scope. Or, you can explicitly drop an object reference by setting the variable to the special value null. ■ Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.

Garbage Collector ■ The garbage collector does its job automatically, although, in some situations, you may want to run the garbage collection explicitly by calling the gc method in the System class. System.gc();

Finalization ■ Before an object gets garbage-collected, the garbage collector gives the object an opportunity to clean up after itself through a call to the object's ‘finalize’ method. This process is known as finalization ■ The finalize method is a member of the Object class, which is the top of the Java platform's class hierarchy and a superclass of all classes. ■ A class can override the finalize method to perform any finalization necessary for objects of that type. If you override finalize, your implementation of the method should call “super.finalize” as the last thing it does.

Historical background ■ 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: Mark: identifies garbage objects 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.

Problems… ■ 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. ■ The other problem 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 transactionoriented clients.

Heap Split ■ Because of these problems, Sun Microsystems' Java HotSpot VM split the heap into 3 sections  Permanent space: used for JVM class and method objects  Old object space: used for objects that have been around for a while  New (young) object space: used for newly created objects

New(young) object space ■ The new object space is further subdivided into three parts:  Eden, where all newly created objects go, and  survivor space 1 and  survivor space 2, where objects go before they become old.

The J2SE 1.3 garbage collection techniques are: ■ Copy-compaction: used for new object space. ■ 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. ■ 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.

Issue !!! ■ All of these techniques are stop-the-world techniques. 

Working… ■ The garbage collector typically runs in a low-priority thread, attempting to reclaim memory when the application is idle. ■ 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.  If the new generation is full, a minor collection is triggered; if the old generation is full, a major collection is triggered.

Minor Collection ■ The steps in a minor collection are:  Copy objects from Eden to survivor space (1 or 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.  Tenured objects move from survivor space 1 or 2 to old object space.

Garbage collection in J2SE 1.4.1 ■ The new algorithms are based on the observation that many machines are used for low-pause or high-throughput applications that have large amounts of memory and multiple processors. The algorithms are optimized to take advantage of these extra resources

The complete set of GC algorithms (*=new in 1.4.1)

Parallel ■ The parallel (multithreaded) algorithms are optimized for machines with multiple CPUs. ■ In J2SE 1.4.1, they are only used in the young generation. ■ Using multiple threads allows garbage collection to proceed in parallel with the application thread, so the application can proceed without perceptible pauses. ■ The parallel scavenging collector is optimized for very large (gigabyte) heaps.

Concurrent ■ The concurrent collector works with the old generation. It divides garbage collection into six phases:  Initial mark  stop-the-world  Mark  Pre-cleaning  Remark  stop-the-world  Sweep  Reset ■ The multi-step garbage collection allows the stop-the-world phase to be as short as possible, which means that application pauses for garbage collection should be minimized.

How to select a GC algorithm ■

For most Java applications, the default algorithms work fine.



If your application performance is reasonable, you don't need to use another GC algorithm. As the saying goes, "If it ain't broke, don't fix it."



If you have a single-processor client machine and are having problems with pause times in your application, try the incremental garbage collector.



If you have a single-processor server machine with lots of memory and experience trouble with application pause times, try the concurrent garbage collector.



If you have a multiprocessor machine, especially with four or more processors, try one of the parallel garbage collection algorithms.

Sources… ■ Sun Java Tutorial ■ http://www.javaworld.com/javaworld/jw-03-2003/jw-0307-j2segc.html

Related Documents

Garbage Collection
November 2019 17
Garbage Collection
June 2020 9
Garbage Collection
June 2020 11
Garbage Collection
November 2019 24
Garbage Collection
October 2019 20