Saturday, April 25, 2009

With J2SE 5.0 onwards, Ergonomics is defined as the capability of the JVM to

Automatically select Heap Size, Garbage collector & type of Virtual Machine (server or client) based on the platform on which your application is running.

Allowing us to Tune the behaviour of the Garbage Collector using various command line options to achieve the desired effect.

But, the aim of Ergonomics is to improve performance & minimize command line tuning as it believes in selecting the optimum configuration for the application which is running. The values are chosen based on the type of machine and the operating system (the platform) on which the application is running.

Machines are divided into 2 categories:

Server class machines

Non-server class machines

According to J2SE 5.0, a server class machine is the one which has

At least 2 CPU's (processors)

At least 2 GB of physical memory

All other machines with less no of processors and memory are treated as non-server class machines. As visible from the configuration, the server class machines are mostly used to run large applications (typically web applications using some server framework)

Automatic Selections

For a server class machine, the default values chosen are

Type of JVM : Server JVM

Type of Garbage Collector : Parallel Garbage Collector

Heap Size :

1/64th of Physical Mem Avail <= Min Heap Size <= 1 GB

1/4th of Physical Mem Avail <= Max Heap Size <= 1 GB

For a non-server class machine, the default values chosen are

Type of JVM : Client JVM

Type of Garbage Collector : Serial Collector

Heap Size :

Min Heap Size : 4 MB

Max Heap Size : 64 MB

To explicitly make JVM treat your machine as server machine, one can do it using any of the 2 ways:

1. Use the -server command line option with java command

2. Change the first two lines of jvm.cfg file in your JRE_installation_directory/lib/

Change

-client KNOWN

-server KNOWN

to

-server KNOWN

-client KNOWN

A server JVM differs from client JVM in the sense that it might take more time (approx. 10% more time) in start-up, but eventually it increases the performance & the throughput of the application in the long run.

While client JVM is sufficient for normal standalone applications, server JVM is best suited for large-scale applications like web applications using a server framework running on some application server.

Behavioral Tuning

If you really know that these automatic settings are not suitable for your application, then JVM gives you the flexibility of changing them as well.

Changing the type of VM

Use -client for using Client VM

Use -server for using Server VM

Changing the type of Garbage Collector

Use

-XX:UseSerialGCSerial Collector

-XX:UseParallelGCParallel Collector

-XX:UseParallelOldGCParallel Compacting Collector

-XX:UseConcMarkSweepGCCMS Collector

Changing the Heap Size

Use -Xms to set Minimum heap size. Ex: -Xms512 will set 512MB to be minimum heap size

Use -Xmx to set Maximum heap size. Ex: -Xmx724 will set 724MB to be maximum heap size.

Setting maximum & minimum heap size to be same takes away the flexibility away from JVM of changing the heap size according to the requirements as they arise.

Certain Goals

With these command line options, you can set certain goals for the Garbage Collector to achieve and their relative priorities. The Garbage Collector works to achieve these goals and can change the heap size and other related parameters

Maximum Pause Time Goal

This goal can be used to set Maximum pause time that a Garbage Collector is allowed i.e. we can specify -XX:MaxGCPauseMillis=n meaning that Garbage Collector is allowed a maximum pause time of n milliseconds for each generation. And if it is not able to limit itself to n milliseconds then it must take some measures to achieve this goal. For example, it can reduce the size of the heap in order to reduce the maximum pause time.

Throughput Goal

It can be used to specify the time spend in doing Garbage Collection using -XX:GCTimeRatio=n

The ratio of garbage collection time to application time is 1/(1+n)

So, that means if we set -XX:GCTimeRatio=19, it means that a maximum of 5% of total time can be spend in Garbage Collection.

Default value is n=99 which means that 1% of total time is allowed for garbage collection

If this goal is not met, then the size of heap is increased so that it takes more time to fill the heap and thus garbage collection occurs less frequently.

Footprint Goal

This is an interesting one. It specifies that if Maximum Pause time goal and Throughput goal have been met, then keep on decreasing the size of the heap until one of the above mentioned Goals cannot be met. Then try to meet that goal. :-)

Goal Priorities

This goal specifies that priority of above mentioned goals. The order of importance is

1. Max Pause Time goal

2. Throughout goal

3. Footprint goal

Tools to evaluate GC performace

In an ideal condition, you need not worry about Garbage Collection. This is precisely the goal of Ergonomics; selecting the best possible configuration according to the machine on which the application is running. But if your application runs into memory troubles like memory leaks and OutOfMemoryError, then there exists a plethora of tools to choose from.

Writing about each of them is out of scope of this document. So, I'm just giving out the names and the required links that would help you analyze the application and take proper steps