Skip to content

Instantly share code, notes, and snippets.

@thbar
Last active July 11, 2018 11:48
Show Gist options
  • Save thbar/969f14afa24cafdbe77cda6a081b1cf3 to your computer and use it in GitHub Desktop.
Save thbar/969f14afa24cafdbe77cda6a081b1cf3 to your computer and use it in GitHub Desktop.
Little things about JRuby heap size

Verifying the current JRuby max heap size

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"

Tweaking & measuring

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

Default values

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 behaviour when dealing with OutOfMemoryError

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.

@headius
Copy link

headius commented Jul 10, 2018

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:

$ JRUBY_OPTS="-J-Xmx500M" jruby -v -e "puts java.lang.management.ManagementFactory.memory_mx_bean.heap_memory_usage.max"
jruby 9.2.1.0-SNAPSHOT (2.5.0) 2018-07-09 046ed6e GraalVM 1.0.0-rc3 25.71-b01-internal-jvmci-0.45 on 1.8.0_172-b11 +jit [darwin-x86_64]
466092032

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment