|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Features Anatomy of a Java Finalizer
Experimenting on finalizer implementations in various Java Virtual Machines
By: Jinwoo Hwang
Jul. 16, 2009 11:45 PM
A couple of patterns that could cause Java heap exhaustion were identified from years of research at IBM. One interesting scenario was observed when Java applications generated an excessive amount of finalizable objects whose classes had non-trivial Java finalizers. What Is a Java Finalizer? protected void finalize() throws Throwable When a Java object is no longer needed, the space occupied by the object is supposed to be recycled automatically by the Java garbage collector. This is one of the significant differences in Java and is not found in most structural programming languages like C. If an instance of a class implements the finalize() method, its space cannot be recycled by the garbage collector in a timely fashion. Worst case, it may not be recycled at all. Any instances of classes that implement the finalize() method are often called finalizable objects. They will not be immediately reclaimed by the Java garbage collector when they are no longer referenced. Instead, the Java garbage collector appends the objects to a special queue for the finalization process. Usually it's performed by a special thread called a "Reference Handler" on some Java Virtual Machines. During this finalization process, the "Finalizer" thread will execute each finalize() method of the objects. Only after successful completion of the finalize() method will an object be handed over for Java garbage collection to get its space reclaimed by "future" garbage collection. I did not say "current," which means at least two garbage collection cycles are required to reclaim the finalizable object. Sounds like it has some overhead? You got it. We need several shots to get the space recycled. Finalizer threads are not given maximum priorities on systems. If a "Finalizer" thread cannot keep up with the rate at which higher priority threads cause finalizable objects to be queued, the finalizer queue will keep growing and cause the Java heap to fill up. Eventually the Java heap will get exhausted and a java.lang.OutOfMemoryError will be thrown. A Java Virtual Machine will never invoke the finalize() method more than once for any object. If there's any exception thrown by the finalize() method, the finalization of the object is halted. You are free to do virtually anything in the finalize() method of your class. When you do that, please do not expect the memory space occupied by each and every object to be reclaimed by the Java garbage collector when the object is no longer referenced or no longer needed. Why? It is not guaranteed that the finalize() method will complete the execution in timely manner. Worst case, it may not be even invoked even when there are no more references to the object. That means it's not guaranteed that any objects that have a finalize() method are garbage collected. That's a potential hazard from a memory management perspective and, needless to say, there is considerable overhead for queuing, dequeuing, running the finalize() method, and rescanning the object in the next garbage collection cycle. If you want to run cleanup tasks on objects, consider finalizers as a last resort and implement your own cleanup method, which will be more predictable. It's very risky to rely on finalizers for postmortem cleanup tasks, especially if your finalizable objects have references to native resources. Hands-on Experience with Java Finalizer Let's build a couple of Java classes to recreate typical scenarios. In ObjectWYieldFinalizer, we can implement the method finalize() with Thread.yield() so that finalize() cannot complete its execution (see Listing 1) (Listings 1-7 can be downloaded here.) The Thread.yield() method prevents the currently executing thread from running and allows other threads to execute. If the Finalizer thread calls this finalize() method, it will pause its execution. In ObjectWExceptionFinalizer, the finalizer() method immediately throws a java.lang.IndexOutOfBoundsException. If the Finalizer thread calls this finalize() method, the object finalization won't be completed because of the exception and there's no second chance to run the finalize() method (see Listing 2). In ObjectWEmptyFinalizer, we don't implement any code in the finalize() method (see Listing 3). ObjectWOFinalizer doesn't have any finalize() method (see Listing 4). Let's run each of the classes to see what happens. Sun's Java Virtual Machine (JVM) can be used with the following options for the class TestObjectWYieldFinalizer. /sun1.6.0_10/bin/java -Xmx50m -verbosegc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+HeapDumpOnOutOfMemoryError TestObjectWYieldFinalizer > verbosegc.txt It looks very complicated. Why do we need those command-line options? Well, without those options, we only get a little information about Java garbage collection activities like this: [Full GC 50815K->45644K(50816K), 0.2276940 secs] We only get a type of garbage collection (Full GC), total Java heap usage before garbage collection (50,815K), total Java heap usage after garbage collection (45,644K), a high watermark of total Java heap (50,816K), and time spent in the garbage collection (0.2276940 secs). High watermark is the size of the current Java heap. The Java heap can expand its size up to a maximum size or contract to manage Java heap effectively. The -XX:+PrintGCDetails option lets the JVM provide information on each generation in the Java heap. We can see Java heap usage and the time (in seconds) spent in Java garbage collection of each new generation, tenured generation, and permanent generation with the -XX:+PrintGCDetails option in the following example: [GC [DefNew: 3520K->3520K(3520K), 0.0001143 secs][Tenured: 43897K->47295K(47296K), 0.2110999 secs] 47417K->47415K(50816K), [Perm : 17K->17K(12288K)], 0.2115593 secs] In tenured generation, Java heap usage was 43,897KB before this Java garbage collection. Java heap usage reached 47,295KB after Java garbage collection; 47296K is the size of the high watermark of the tenured generation; 0.2110999 second was spent to collect the tenured generation. Unfortunately we do not see the maximum size of each generation with the -XX:+PrintGCDetails option alone. With -XX:+PrintGCTimeStamps, we can get a timestamp for each garbage collection like this: 1.393: [GC 1002K->106K(5056K), 0.0001036 secs] This garbage collection started 1.393 seconds after the JVM started. The -XX:+PrintHeapAtGC option provides extensive information about each Java garbage collection (see Listing 5). We can even see the address range of each generation. Unfortunately there's no timestamp for each garbage collection with -XX:+PrintHeapAtGC option alone. The last option, -XX:-HeapDumpOnOutOfMemoryError allows the JVM to dump the Java heap to a file when an allocation from the Java heap cannot be satisfied or a java.lang.OutOfMemoryError is thrown. This option was introduced in Sun Java 1.4.2 update 12 and Sun Java 5.0 update 7. If you want to experiment with IBM's Java Virtual Machine, all you need is a -verbosegc command-line option. You do not need any of these: -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC, or -XX:+HeapDumpOnOutOfMemoryError. Let's take a look at what happened to the class while we talked about Sun JVM's command-line option. We've got java.lang.OutOfMemoryError from TestObjectWYieldFinalizer: Exception in the thread "main" java.lang.OutOfMemoryError: Java heap space "java.lang.OutOfMemoryError: Java heap space." That means we got Java heap space exhaustion. The JVM could not allocate any more Java heap while running the method java.lang.ref.Finalizer.register(), which is on line 72 of Finalizer.java. Finalizer.java? Of course, we did not write Finalizer.java. That's a part of the JVM. We can also check verbosegc.txt to which we redirected the garbage collection trace (see Listing 6). About 3.711 seconds after the JVM started, we got java.lang.OutOfMemoryError. The Java heap dump is written to pid124.hprof. Analysis of Java Garbage Collection Traces PMAT parses verbose garbage collection (GC) trace, analyzes Java heap usage, and recommends possible solutions based on pattern modeling of Java heap usage. The following features are included:
The tool can also parse Sun's Java garbage collector traces, including the following:
We can start version 3.2 of the tool with a jar file in the download package. (V3.2 was the latest version when this article was written.) # /usr/java5/bin/java -Xmx200m -jar ga32.jar The tool provides a headless mode but let's bring up the graphical user interface mode for easy demonstration. We can click on the "N" icon to open Sun's trace file. We can click on "I" for IBM's trace file as seen in Figure 1. We can see in Figure 2 the analysis result and recommendation virtually instantly. Reader Feedback: Page 1 of 1
Your Feedback
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
||||||||||||||||||||||||||||||||||||||||||||||||||||