Skip to main content

Posts

Showing posts with the label gc

Concluding the series on Java garbage collection

Over the past few blog posts we have covered some basics of garbage collection in Java. Garbage collection is a key strength of the JVM, since we do not have to worry about releasing object memory. It reduces memory leaks and other problems associated with improper memory management code that often creeps in when we program against hard deadlines :-) However, for large programs we very often have to tweak the default garbage collection mechanism to improve performance. Here's a nice article that explains garbage collection in much more detail with good examples. This page contains several links to memory management in the Java Hotspot VM. Here's another page that explains how to fine tune the Java garbage collector in Java 1.5. It is also a good idea to keep up with the latest in technology, so here's a page that describes enhancements to the Java VM in version 1.6, that influence the garbage collector. I hope you enjoyed this series. Over the next few months I hope to ...

Garbage collection example

A Java program can request the underlying JVM to perform garbage collection by calling the gc() method of the System class. Note that System.gc() is a request to the underlying JVM. Garbage collection may not always happen. Hence we cannot depend on this method, however we can use it to optimize performance, with the understanding that it may not work as desired on all JVM's.   Study the program shown below. It ilustrates how we can invoke the garbage collector from a Java program. 01   /** This program creates instances of Bag objects in a loop which will run 100000 times. 02     * Whenever the program is in a loop that is a multiple of 1000, it requests the JVM 03     * to start the garbage collector. We will see that...

Adaptive algorithms

Adaptive algorithms do not use any fixed algorithm for garbage collection. They monitor the heap and choose an algorithm that is most effective for the current usage pattern. Such algorithms may also divide the heap into sub heaps and use a different algorithm for every sub heap, depending on the usage. Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net

Generational algorithms

Introduction: Generational algorithms make use of a property found in most Java programs. Several studies have revealed a pattern in the way objects are used. It has been found that objects that get created early in program execution usually live longer than objects that get created later. It is usually the youngest objects that are garbage collected first. Look at the diagram below. The blue area in the diagram is a typical distribution for the lifetimes of objects. The X axis represents the bytes allocated by the JVM, and the Y axis represents th number of surviving bytes (live objects). The sharp peak at the left represents objects that can be reclaimed (i.e., have "died") shortly after being allocated. Iterator objects, for example, are often alive for the duration of a single loop. Image source: The above image has been taken from the document " Tuning garbage collection with the 5.0 Java[tm] virtual machine " If you notice, the distribution stret...

Copying algorithms

Copying algorithms are tracing algorithms that divide the heap into multiple regions; one object region in which objects are created, and one or more free space regions (which are ignored, ie. objects are never created in these regions). When the object space becomes full, all live objects are moved to the free space. Now the free space becomes the object space, and the old object space becomes the free space. Since objects are placed contiguously in the new region, holes (fragments) are not formed. The most commonly used copying algorithm is known as stop and copy, which uses one object region and one free space region. When the object region becomes full, the (Java) program is paused until live objects are copied into the free space. The program is restarted after the copying phase. Copying algorithms have an advantage over compacting algorithms in that the address of object references does not change when the objects are moved. Discuss this post in the learning forum . ...

Compacting algorithms

Tracing algorithms result in a fragmented heap, because objects that are garbage collected in the sweep phase, usually occupy arbitrary positions in the heap, resulting in holes, wherever the old objects were garbage collected. Compacting algorithms have a modified sweep phase, in which all live objects are copied into one end of the heap. After they are copied, the rest of the heap is freed. This places all live objects contiguously, and the heap is defragmented. Advantages: The memory is defragmented in the sweep phase Disadvantages: The [Java] program has to be paused when the garbage collector starts. References have to be updated when the live objects are moved. Discuss this post in the learning forum . Commercial Links Buy programming books from our Amazon.com powered storefront . Earn as you browse. Sign up for Algoco . Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net

Tracing algorithms

Before we understand what tracing algorithms are, we must first understand the meaning of the phrase “root set of references” . Essentially the root set of references are those references that are always accessible to an executing Java program. They constitute local variables, method arguments, class variables, and instance variables. When triggered, tracing algorithms mark all objects on the heap that can be traced from the root set of references. These are objects that are directly or indirectly referenced by the root set. Let's consider these to be live objects. This forms the mark phase of the tracing algorithm. The mark phase is followed by a sweep phase, in which all objects that were not marked in the mark phase are garbage collected. Because of it's two phases, this type of tracing algorithm is also known as “mark and sweep” algorithm. Advantages: Can detect and handle cycles Disadvantages: The heap is fragmented, ater the sweep phase. Having a fra...

Reference counting algorithms

Early JVM's used reference counting to determine objects no longer in use. In reference counting, an objects reference count is initially set to one when it is instantiated and it's reference is assigned to a variable. Every time a reference of that object is assigned to another variable, the count is incremented by one. When a variable holding the reference goes out of scope, the count of the object is decremented by one. An object is eligible for garbage collection when it's reference count becomes zero. Let's see how reference counting algorithms work, with a simple animation. Right click in the region below and select 'play' to start the animation. Advantages: It is very simple to implement. The JVM does not need to be paused when the garbage collector is running. Disadvantages: There is an extra overhead since the reference count of objects needs to be updated very often. ...

Garbage collection in Java

I am starting a series of posts on the Java garbage collector and various garbage collection algorithms. Over the 10 days or so, we will examine the Java garbage collector and garbage collection algorithms. Let's begin with a brief introduction. An executing program occupies memory. Memory is taken as new objects are created. This memory should be returned when these objects are no longer needed. If it is not, then the computer will soon run out of resources, forcing the program to a halt. In C++ we have to explicitly return an object's memory back to the system when the object is destroyed. This is also the source for many bugs in C++. Buggy code that does not return memory can very soon exhaust system resources. To eliminate such errors, and make your program more robust, Java has adopted the philosophy of automatic memory reclamation. The JVM determines when an object is no longer needed (Eg: after the variable goes out of scope), and automatically reclaims memory occupied b...

Garbage collection example

A Java program can request the underlying JVM to perform garbage collection by calling the gc() method of the System class. Note that System.gc() is a request to the underlying JVM. Garbage collection may not always happen. Hence we cannot depend on this method, however we can use it to optimize performance, with the understanding that it may not work as desired on all JVM's.   Study the program shown below. It ilustrates how we can invoke the garbage collector from a Java program. 01   /** This program creates instances of Bag objects in a loop which will run 100000 times. 02     * Whenever the program is in a loop that is a multiple of 1000, it requests the JVM 03     * to start the garbage collector. We will see that...

Adaptive algorithms

Adaptive algorithms do not use any fixed algorithm for garbage collection. They monitor the heap and choose an algorithm that is most effective for the current usage pattern. Such algorithms may also divide the heap into sub heaps and use a different algorithm for every sub heap, depending on the usage. Note: This text was originally posted on my earlier blog at http://www.adaptivelearningonline.net

Generational algorithms

Introduction: Generational algorithms make use of a property found in most Java programs. Several studies have revealed a pattern in the way objects are used. It has been found that objects that get created early in program execution usually live longer than objects that get created later. It is usually the youngest objects that are garbage collected first.   Look at the diagram below. The blue area in the diagram is a typical distribution for the lifetimes of objects. The X axis represents the bytes allocated by the JVM, and the Y axis represents the number of surviving bytes (live objects). The sharp peak at the left represents objects that can be reclaimed (i.e., have "died") shortly after being allocated. Iterator objects, for example, are often alive for the duration of a single loop.   If you notice, the distribution stretches out to the the right. This is because some objects live longer. Typically these are objects that have been created when the p...

Copying algorithms

Copying algorithms are tracing algorithms that divide the heap into multiple regions; one object region in which objects are created, and one or more free space regions [which are ignored, ie. objects are never created in these regions]. When the object space becomes full, all live objects are moved to the free space. Now the free space becomes the object space, and the old object space becomes the free space. Since objects are placed contiguously in the new region, holes [fragments] are not formed. The most commonly used copying algorithm is known as stop and copy, which uses one object region and one free space region. When the object region becomes full, the [Java] program is paused until live objects are copied into the free space. The program is restarted after the copying phase.   Copying algorithms have an advantage over compacting algorithms in that the address of object references does not change when the objects are moved. Discuss this post in the learning forum . C...