title: Setting a smaller JVM heap size within a JNI client application
body: <p>I'm attempting to debug a problem with pl/java, a procedural language for PostgreSQL. I'm running this stack on a Linux server.</p>
  
  <p>Essentially, each Postgres backend (connection process) must start its own JVM, and does so using the JNI. This is generally a major limitation of pl/java, but it has one particularly nasty manifestation.</p>
  
  <p>If native memory runs out (I realise that this may not actually be due to malloc() returning NULL, but the effect is about the same), this failure is handled rather poorly. It results in an OutOfMemoryError due to "native memory exhaustion". This results in a segfault of the Postgres backend, originating from within libjvm.so, and a javacore file that says something like:</p>
  
  <pre><code>0SECTION       TITLE subcomponent dump routine
  NULL           ===============================
  1TISIGINFO     Dump Event "systhrow" (00040000) Detail "java/lang/OutOfMemoryError" "Failed to create a thread: retVal -1073741830, errno 11" received
  1TIDATETIME    Date:                 2012/09/13 at 16:36:01
  1TIFILENAME    Javacore filename:    /var/lib/PostgreSQL/9.1/data/javacore.20120913.104611.24742.0002.txt
  ***SNIP***
  </code></pre>
  
  <p>Now, there are reasonably well-defined ways of ameliorating these types of problems with Java, described here:</p>
  
  <p><a href="http://www.ibm.com/developerworks/java/library/j-nativememory-linux/" rel="nofollow">http://www.ibm.com/developerworks/java/library/j-nativememory-linux/</a></p>
  
  <p>I think that it would be particularly effective if I could set the maximum heap size to a value that is far lower than the default. Ordinarily, it is possible to do something along these lines:</p>
  
  <blockquote>
    <p>The heap's size is controlled from the Java command line using the -Xmx and -Xms options (mx is the maximum size of the heap, ms is the initial size). Although the logical heap (the area of memory that is actively used) can grow and shrink according to the number of objects on the heap and the amount of time spent in GC, the amount of native memory used remains constant and is dictated by the -Xmx value: the maximum heap size. Most GC algorithms rely on the heap being allocated as a contiguous slab of memory, so it's impossible to allocate more native memory when the heap needs to expand. All heap memory must be reserved up front.</p>
  </blockquote>
  
  <p>However, it is not apparent how I can follow these steps such that pl/java's JNI initialisation initialises a JVM with a smaller heap; I can't very well pass these command line arguments to Postgres. So, my question is, how can I set the maximum heap size or otherwise control these problems in this context specifically? This appears to be a general problem with pl/java, so I expect to be able to share whatever solution I eventually arrive at with the Postgres community.</p>
  
  <p>Please note that I am not experienced with JVM internals, and am not generally familiar with Java.</p>
  
  <p>Thanks</p>