<< 顶级java/j2ee开发者应具有什么素质? | 首页 | 我收藏的链接(8) >>

Java垃圾收集调优-Java Garbage Collection Tuning

 Author: greg, Created on: Mar 7, 2005 4:01 PM


Introduction

Correctly tuning Java garbage collection is important for high traffic web applications. This document provides some general guidelines as well as links to additional resources.

General Guidelines

Server Mode

For server-side applications the JVM should be run with the "-server" flag. This enables the server mode for garbage collection which will reduce the number of garbage collections to increase overall application throughput. There is also a difference between minor and full garbage collections in this mode. Minor garbage collections clean up more volatile objects and take a short amount of time. Full garbage collections take longer but run over the entire heap to clean up more memory.

Minimum and Maximum Heap Size

The heap size for the JVM should be specified based on the application's expected memory usage. The minimum heap size is less important as it is only useful at startup and may save a small amount of time due to fewer garbage collection/memory allocation cycles needing to be processed. Use -Xms(memorySize) to set the minimum heap size. For example -Xms512m sets the minimum heap size to 512MB.

The maximum heap size is very important. If the maximum heap size is not large enough the application can cause OutOfMemoryExceptions or there may be an excessive number of garbage collections which will decrease application throughput and responsiveness. However, the maximum heap should not be set to more than 1 GB of memory unless testing has shown the JVM will work well with a specific setting. Experience shows that the garbage collector has difficulty with larger heap sizes. Use -Xmx(memorySize) to set the maximum heap size. For example -Xmx1024m sets the maximum heap size to 1024MB or 1GB.

For specific recommendations on heap sizes for Jive Software products, the general rule is that the maximum heap size should be two to three times the total size of the application caches.

MaxPermSize

The JVM stores class metadata such as Method and Class objects in memory in the permanent generation. The default setting for -server mode in most environments is 64 MB (in other environments it may be 32 MB). This setting does not generally need to be changed, but if there are unexplained OutOfMemory exceptions after properly sizing the maximum heap size then increasing the MaxPermSize to 128 MB is recommended. This configuration can be done by including -XX:MaxPermSize=128m in the JVM startup command. Note that the increased capacity will only be used if it is needed by the JVM.

Other Garbage Collection Settings

There are other garbage collector settings which can make a difference depending on the computing environment. Please see the resources section for additional information.

Garbage Collection Logging

When a garbage collection issue is suspected there are several tools which can help confirm the possibility. To log garbage collection information, use the following two JVM command-line switches:

Enable Garbage Collector Logging

-verbose:gc

This setting enables garbage collector logging.

Log File

-Xloggc:<file>

This setting will cause the garbage collector log information to go into the file specified by <file>. Note that you should copy the garbage collection log file before restarting or else the file will get overridden.

Garbage Collection Details

Using these two settings will increase the amount of helpful information in the garbage collection logs.

-XX:+PrintGCTimeStamps -XX:+PrintGCDetails

Garbage Collection Logging Summary

Add all of these parameters to the JVM startup command:

-verbose:gc -Xloggc:<file> -XX:+PrintGCTimeStamps -XX:+PrintGCDetails

Analyzing the Garbage Collector Log Files

Analyzing the garbage collector log files can be done visually by reading the logs or with a tool such as HP's HPjtune (see the Resources section). The format of the garbage collector logs is:

<timestamp>: [<allocation change>(<total heap allocated>), <time for garbage collection>]

For example, this line shows that 60.5 seconds after JVM startup a minor garbage collection was done which tool 0.0116410 seconds and decreased heap memory usage from 98255K to 88403K. The total heap size allocated from the operating system is 162676K.

60.500: [98255K->88403K(162676K), 0.0116410 secs]

Another example shows a full garbage collection. Full garbage collections should happen much less frequently than minor garbage collections. This example shows a full garbage collection that occurred 304.528 seconds after JVM startup and took 1.4657250 seconds. Heap usage went from 47707K to 29607K out of a total allocation of 76800K.

304.528: [Full GC 47707K->29607K(76800K), 1.4657250 secs]

Resources

Tuning Garbage Collection with the 1.4.2 Java[tm] Virtual Machine
Sun's documentation on garbage collector tuning.

Tuning Garbage Collection OutlineA summary of the Sun document above.

HP's HPjtune
A tool from HP for visualizing garbage collection logs.

Eye on performance: Tuning garbage collection in the HotSpot JVM
An IBM article on garbage collector tuning.

Setting up a HotSpot server or client mode on a Java 2 SDK
Configuring the JVM Server Mode on IBM WebSphere Application Server

标签 : ,



发表评论 发送引用通报