<p>I've been running into a peculiar issue with certain Java applications in the HP-UX environment.  </p>

<p>The heap is set to -mx512, yet, looking at the memory regions for this java process using gpm, it shows it using upwards of 1.6GBs of RSS memory, with 1.1GB allocated to the DATA region.  Grows quite rapidly over a 24-48hour period and then slows down substantially, still growing 2MB every few hours. However, the Java heap shows no sign of leakage.</p>

<p>Curious how this was possible I researched a bit and found this HP write-up on memory leaks in java heap and c heap: <a href="http://docs.hp.com/en/JAVAPERFTUNE/Memory-Management.pdf" rel="nofollow noreferrer">http://docs.hp.com/en/JAVAPERFTUNE/Memory-Management.pdf</a></p>

<p>My question is what determines what is ran in the C heap vs the java heap, and for things that do not run through the java heap, how would you identify those objects being run on the C heap? Additionally does the java heap sit inside the C heap?</p>

[{'answer_id': 78472, 'author': 'Bill K', 'author_id': 12943, 'author_profile': 'https://Stackoverflow.com/users/12943', 'pm_score': 1, 'selected': False, 'text': "<p>My only guess with the figures you have given is a memory leak in the Java VM.  You might want to try one of the other VMs they listed in the paper you referred.  Another (much more difficult) alternative might be to compile the open java on the HP platform.</p>\n\n<p>Sun's Java isn't 100% open yet, they are working on it, but I believe that there is one in sourceforge that is.</p>\n\n<p>Java also thrashes memory by the way.  Sometimes it confuses OS memory management a little (you see it when windows runs out of memory and asks Java to free some up, Java touches all it's objects causing them to be loaded in from the swapfile, windows screams in agony and dies), but I don't think that's what you are seeing.</p>\n"}, {'answer_id': 78533, 'author': 'Mike Tunnicliffe', 'author_id': 13956, 'author_profile': 'https://Stackoverflow.com/users/13956', 'pm_score': 2, 'selected': False, 'text': '<p>In general, only the data in Java objects is stored on the Java heap, all other memory required by the Java VM is allocated from the "native" or "C" heap (in fact, the Java heap itself is just one contiguous chunk allocated from the C heap).</p>\n\n<p>Since the JVM requires the Java heap (or heaps if generational garbage collection is in use) to be a contiguous piece of memory, the whole maximum heap size (-mx value) is usually allocated at JVM start time.  In practice, the Java VM will attempt to minimise its use of this space so that the Operating System doesn\'t need to reserve any real memory to it (the OS is canny enough to know when a piece of storage has never been written to).</p>\n\n<p>The Java heap, therefore, will occupy a certain amount of space in memory.</p>\n\n<p>The rest of the storage will be used by the Java VM and any JNI code in use.  For example, the JVM requires memory to store Java bytecode and constant pools from loaded classes, the result of JIT compiled code, work areas for compiling JIT code, native thread stacks and other such sundries.  </p>\n\n<p>JNI code is just platform-specific (compiled) C code that can be bound to a Java object in the form of a "native" method.  When this method is executed the bound code is executed and can allocate memory using standard C routines (eg malloc) which will consume memory on the C heap.</p>\n'}, {'answer_id': 78634, 'author': 'Will Hartung', 'author_id': 13663, 'author_profile': 'https://Stackoverflow.com/users/13663', 'pm_score': 3, 'selected': False, 'text': '<p>Consider what makes up a Java process.</p>\n\n<p>You have:</p>\n\n<ul>\n<li>the JVM (a C program) </li>\n<li>JNI Data</li>\n<li>Java byte codes</li>\n<li>Java data </li>\n</ul>\n\n<p>Notably, they ALL live in the C heap (the JVM Heap is part of the C heap, naturally).</p>\n\n<p>In the Java heap is simply Java byte codes and the Java data. But what is also in the Java heap is "free space".</p>\n\n<p>The typical (i.e. Sun) JVM only grows it Java Heap as necessary, but never shrinks it. Once it reaches its defined maximum (-Xmx512M), it stops growing and deals with whatever is left. When that maximum heap is exhausted, you get the OutOfMemory exception.</p>\n\n<p>What that Xmx512M option DOES NOT do, is limit the overall size of the process. It limits only the Java Heap part of the process.</p>\n\n<p>For example, you could have a contrived Java program that uses 10mb of Java heap, but calls a JNI call that allocates 500MB of C Heap. You can see how your process size is large, even though the Java heap is small. Also, with the new NIO libraries, you can attach memory outside of the heap as well.</p>\n\n<p>The other aspect that you must consider is that the Java GC is typically a "Copying Collector". Which means it takes the "live" data from memory it\'s collecting, and copies it to a different section of memory. This empty space that is copies to IS NOT PART OF THE HEAP, at least, not in terms of the Xmx parameter. It\'s, like, "the new Heap", and becomes part of the heap after the copy (the old space is used for the next GC). If you have a 512MB heap, and it\'s at 510MB, Java is going to copy the live data someplace. The naive thought would be to another large open space (like 500+MB). If all of your data were "live", then it would need a large chunk like that to copy into.</p>\n\n<p>So, you can see that in the most extreme edge case, you need at least double the free memory on your system to handle a specific heap size. At least 1GB for a 512MB heap.</p>\n\n<p>Turns out that not the case in practice, and memory allocation and such is more complicated than that, but you do need a large chunk of free memory to handle the heap copies, and this impacts the overall process size.</p>\n\n<p>Finally, note that the JVM does fun things like mapping in the rt.jar classes in to the VM to ease startup. They\'re mapped in a read only block, and can be shared across other Java processes. These shared pages will "count" against all Java processes, even though it is really only consuming physical memory once (the magic of virtual memory).</p>\n\n<p>Now as to why your process continues to grow, if you never hit the Java OOM message, that means that your leak is NOT in the Java heap, but that doesn\'t mean it may not be in something else (the JRE runtime, a 3rd party JNI librariy, a native JDBC driver, etc.).</p>\n'}]
['https://Stackoverflow.com/questions/78352', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9628/']