I think I'm able to measure the max heap using this code:
# size in bytes
jruby -e "puts java.lang.management.ManagementFactory.memory_mx_bean.heap_memory_usage.max"
For instance, if I change the max heap size to 10MB, I will get a bit less in practice:
JRUBY_OPTS="-J-Xmx10M" jruby -e "puts java.lang.management.ManagementFactory.memory_mx_bean.heap_memory_usage.max"
9_437_184 # underscores mine
On Ubuntu, the documented default of 500MB seems to apply:
jruby -e "puts java.lang.management.ManagementFactory.memory_mx_bean.heap_memory_usage.max"
466_092_032
On my Mac though, for some reason the output is different:
$ jruby -e "puts java.lang.management.ManagementFactory.memory_mx_bean.heap_memory_usage.max"
3_817_865_216
I'm not sure yet why I'm seeing this on my Mac.
Rollbar does not currently report this type of errors properly, but I issued a PR and hope they will integrate it to cover this case properly.
JRuby had, for some time, a default limit of 500MB. We did this because the JVM had its own very low default limit (64MB) but also was too aggressive about taking more memory. As of Java 8, I think, they upped the default limit to something like 1/2 available RAM, and we started removing the limitation from our launch scripts/binaries.
Your code for reading heap size is just fine. However I get the expected result with your
-J-Xmx500M
command line on Mac: