Garbage Collector Pitfalls

Posted 2009-03-04 in Java by Johann.

Garbage

Garbage by Editor B, some rights reserved.

If your Java VM crashes, your application might leak memory or the memory settings might not be optimal.

Whatever the cause might be, the following tips will help you fix the problem quickly.

Don’t increase Maximum Memory

Many developers’ first reaction if the Java VM isn’t stable is to increase the maximum memory with the -Xmx switch. The problems of this are:

  • Garbage collections take longer since more memory needs to be cleaned, leading to a lower application performance.
  • Memory leaks take longer to crash the VM. If there is a memory leak, having a larger Java heap means it will take longer until the VM crashes, making testing more difficult.

Enable GC logging

Keeping an eye on what the garbage collector is doing is important. In my experience, the runtime overhead is small so I have this enabled at all times. If logging is not enabled and a crash happens, it might take a long time to reproduce it.

For the Sun VM, add the following switches:

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

You can analyze the garbage collector log file using GCViewer or a variety of other tools.

For the IBM VM, add the following switches:

-Xverbosegclog:<file>

The output of the IBM VM is written in an XML format. Pay attention to the nursery and tenured elements and their freebytes attribute. A complete description is available in the Diagnosis documentation.

Adapt the ratio of “New” to “Old” space

Without diving too deep into the Java memory model, let me just say there is a “new” space for newly created objects and an “old” space for objects that have been used for a while.

Web applications usually create many objects that are only used for a short time during a request. If the ratio of “new” to “old” is too low, many garbage collections will occur, decreasing the application performance.

An easy way to test this is to watch the garbage collector log file (tail –F <log file>) and click on a button. If you see more than one garbage collection, there might not be enough “new” space. In this case, increasing the ratio of “new” to “old” might help a lot.

For the Sun VM, the switch is:

-XX:NewRatio=<ratio>

I have this set on this server to -XX:NewRatio=2, making “new” one third the size of “old.”

For the IBM VM, the switches are:

-Xmnx=<maximum new size> -Xmx=<maximum overall Java heap size>

Did you have problems with garbage collection? Post a comment how you solved them!

5 comments

#1 2009-03-06 by William Louth

Not all out of memory errors or jvm crashes are memory leaks. In production environments with high degree of concurrent request processing the more natural (and obvious) issue is a memory capacity problem caused by long service times, excessive temporary object alloc transiently referenced during the complete processing cycle.

William

#2 2009-03-06 by Johann

William,

exactly. Excessive temporary object creation can cause problems, especially if there isn't enough room for them in the new space.

#3 2009-03-07 by kirk

I hate to agree with William ;-) but I do. I would say that the first reaction to an OOME is to hit the -Xmx flag. Longer GC is no where near as fatal as an OOME :-)

Kirk

#4 2009-03-08 by Johann

kirk,

I'd rather understand what is happening first, but there you go.

#5 2009-03-08 by kirk

I have a term that I use which is "tactical hack". You do what ever you need to do, not matter how ugly it is, to keep the patient alive. IME, adding more memory is a tactical hack that I use as I've found it often keeps the VM alive for a long enough time that it runs without being too disruptive to operations. Once you've got things in that state, the next step is to figure out what is happening so you can back out of the tactical hack with a longer term more appropriate solution. In a number of cases this has made the difference between winning or losing.

Subscribe

RSS 2.0, Atom or subscribe by Email.

Top Posts

  1. DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
  2. 6 fast jQuery Tips: More basic Snippets
  3. xslt.js version 3.2 released
  4. xslt.js version 3.0 released XML XSLT now with jQuery plugin
  5. Forum Scanners - prevent forum abuse
  6. Automate JavaScript compression with YUI Compressor and /packer/

Navigation