8

I have encountered the infamous OutOfMemoryException in my application and instead of simply increasing the amount of Heap Space available I tried to look into what the problem was, just in case, there was some sort of leak from my application.

I added the JVM parameter -XX:+HeapDumpOnOutOfMemoryError which creates a Heap Dump when the OutOfMemory Error is encountered. I then analysed the dump file produced with different profiling tools. I then started playing around with the -Xmx parameter and observing patterns.

What puzzled me is the following. Why is it that on analyzing the dump I found that the total size of all objects was much less than the total that I set using the -Xmx parameter? For example, say I set the -Xmx to '2048m'. When I analyzed the dump file I found a total of 400Mb of objects on the Heap. I was expecting to find 2GB. Am I missing something?

trincot
  • 317,000
  • 35
  • 244
  • 286
Kros
  • 848
  • 1
  • 10
  • 24
  • Did you check that your OOME actually mentions the heap memory? There are a few (relatively uncommon) cases where OOMEs are thrown, but it's NOT the heap that's full. – Joachim Sauer Aug 24 '11 at 16:26
  • 1
    Right, @Joachim, too many threads in JVM x86 crashes too with OOM exception – aalku Aug 24 '11 at 16:31

2 Answers2

5

My guess is that since modern GCs partition the heap into separate memory areas (young / tenured / permanent generations), it is enough for the permanent generation space to fill up completely for an out of memory error to occur. You can configure the ratio of different generation spaces using various JVM command line options.

Here is a good article about Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine (I couldn't find a more recent one but I think the basics still apply in newer VMs).

Péter Török
  • 114,404
  • 31
  • 268
  • 329
3

Re-read your error message - it may tell you which type of memory you ran out of. I'd guess that it was PermGen space. Permgen is used for class definitions (amongst other things). You can tune the space for PermGen via -XX:MaxPermSize PermGen isn't part of the heap, so isn't included in heap dump.

See this answer for more information on looking at PermGen.

If this isn't the issue, then try setting your initial heap size (-Xms) to the same as the maximum. Doing this means that the heap won't grow which should make understanding what's going on easier.

I recommend using jvisualvm (part of the JDK) to look at your memory utilisation as your program runs.

Community
  • 1
  • 1
Paul M
  • 357
  • 1
  • 8
  • Might be interesting .... Yes the error I got was this one. Trying this out know. Will post back with the results. – Kros Aug 25 '11 at 07:43
  • Used the -XX:MaxPermSize and having been having problems since then. I think that the latest changes in my application have led to a threshold being surpassed which was causing the exception. Thanks for your help. – Kros Aug 25 '11 at 09:01