title: Estimating maximum safe JVM heap size in 64-bit Java
body: <p>In the course of profiling a 64-bit Java app that's having some issues, I notice that the profiler itself (YourKit) is using truly colossal amounts of memory. What I've got in the YourKit launch script is:</p>
  
  <pre><code>JAVA_HEAP_LIMIT="-Xmx3072m -XX:PermSize=256m -XX:MaxPermSize=768m"
  </code></pre>
  
  <p>Naively, assuming some overhead, this would lead me to guess that YourKit is going to use a max of something maybe a bit over four GB. However, what I actually see in PS is:</p>
  
  <pre><code>USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
  dmoles   31379  4.4 68.2 14440032 8321396 ?    Sl   11:47  10:42 java -Xmx3072m -XX:PermSize=256m -XX:MaxPermSize=768m -XX:+HeapDumpOnOutOfMemoryError -Dyjp.probe.table.length.limit=20000 -Xbootclasspath/a:/home/dmoles/Applications/yjp-9.5.6/bin/../lib/tools.jar -jar /home/dmoles/Applications/yjp-9.5.6/bin/../lib/yjp.jar
  </code></pre>
  
  <p>That's a virtual size of nearly 14 GB and a resident size of nearly 8 GB -- nearly 3x the Java heap.</p>
  
  <p>Now, I've got enough memory on my dev box to run this, but going back to the original memory problem I'm trying to diagnose: How do I know how much Java heap I have to play with?</p>
  
  <p>Clearly, if the customer has, say, 16 GB physical RAM, it's not a great idea for me to tell them to set <code>-Xmx</code> to 16 GB.</p>
  
  <p>So what <em>is</em> a reasonable number? 12 GB? 8 GB?</p>
  
  <p>And how do I estimate it?</p>