Search code examples
jruby

Output JRuby heap space from application code


Is there a way of outputting the Java heap space, the JRuby application was started with (like -J-Xms2048m), from the application code?


Solution

  • The value passed in at the JVM command line is available in the Runtime JMX bean provided by the JDK, as @stephanlindauer pointed out.

    Another way that's less sensitive to how the JVM was launched is to use the Memory bean, like so:

    membean = java.lang.management.ManagementFactory.memory_mx_bean
    heap = membean.heap_memory_usage
    puts heap.max
    

    On my system with no special JRuby flags, this outputs "466092032" (roughly reflecting our default max of 500MB), and when specifying a 2GB maximum heap, (jruby -J-Xmx2g) it outputs "1908932608".