Java Profile Examples
Using two Java programs, MyApp.java and MyAppComplex.java, I started 10 different threads and created dummy objects (click here
to download the programs). MyApp.java creates objects within the method and explicitly nulls them after use, while MyAppComplex.java creates a number of class-level objects and does not null them. These programs demonstrate the activity within young and old generation JVM spaces and their effect on performance. Both programs have similar functionality, but MyApp takes less time as it is more JVM-friendly. I used the standard Java profilers jconsole and jstat to monitor the running application.
When I ran MyApp.java, it actively used the young generation space (see Figure 2).
In the jconsole display in Figure 3, you can see that the old generation space is constant.
|Figure 3. Jconsole Snapshot of Old Generation Space for MyApp.java|
However, the Eden space is quite active (see Figure 4).
|Figure 4. Jconsole Snapshot of Eden Space for MyApp.java|
When I run MyAppComplex with a large number of objects hanging around, you can see that the old generation space keeps growing (see Figure 5).
|Figure 5. Jconsole Old Generation Space Snapshot of MyComplexApp.java|
At the same time, the young generation space is extremely busy swapping objects from young generation space to old generation space. The young generation space gets filled up pretty quickly, so the JVM performs frequent minor collections (see Figure 6).
|Figure 6. Jconsole Eden Space Snapshot of MyAppComplex.java|
Jstats shows that whenever the young generation space is emptied, the old generation space increases for MyAppComplex.java (see Figure 7).
|Figure 7. Jstat Snapshot of MyAppComplex.java|
The old generation space keeps increasing until the JVM determines it has a major collection. When the JVM cannot reclaim any memory space—even after a major collection, then the application starts slowing down (see Figure 8).
|Figure 8. Jstat Snapshot of MyAppComplex.java|
JDK 1.5 provides many command-line options to control the memory allocated to JVM spaces, such as
-Xmx<size>, etc. The standard jstat and jconsole provide a lot of information about the internals of the running JVM. As noted in Figure 2, MyApp.java's old generation space memory usage is very little (16 to 20 percent). Though the default old generation space setting is 40 to 70 percent, keep it to less than 30 percent to prevent the JVM from interrupting your running application to do a major GC collection. The difference in running times between MyApp.java and MyAppComplex.java is the GC's interference of the running application.
Monitor the Performance of Your Java Application
Many profiler tools are available for monitoring the performance of your Java application (such as JProfiler and YourKitJavaProfiler), and they offer fine-grained control to the class and even method levels. This article demonstrated some internal details of JVMs with standard JDK tools.
With knowledge of JVM internals, Java developers can run their applications through the profilers and address all the bottlenecks before deploying them. This way, they can avoid OutOfMemory error failures and poor performance in production.