Browse DevX
Sign up for e-mail newsletters from DevX


Use a Profiler to Make Your Java Apps JVM-Friendly : Page 3

If you don't profile your large enterprise Java applications prior to releasing them to production, they can fail or render poor performance. Learn how standard JDK profiling tools can provide in-depth analysis of your application's JVM memory usage.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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).

Click to enlarge 
Figure 2. Jstat Snapshot of MyApp.java

In the jconsole display in Figure 3, you can see that the old generation space is constant.

Click to enlarge 
Figure 3. Jconsole Snapshot of Old Generation Space for MyApp.java

However, the Eden space is quite active (see Figure 4).

Click to enlarge 
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).

Click to enlarge 
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).

Click to enlarge 
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).

Click to enlarge 
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).

Click to enlarge 
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 –Xincgc, -Xloggc:<file>, -Xms<size>, -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.

Raghu Donepudi, an independent contractor, currently is a technical manager for a federal government agency. He has a master's degree in Computer Science from Lamar University in Texas. He is a Sun-certified Java developer and the author of many software design techniques.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date