How to use ulimit with java correctly?

Viewed 7774

My java program must run in an environment where memory is constrained to a specified amount. When I run my java service it runs out of memory during startup.

This is an example of the commands I'm using and values I'm setting:

ulimit -Sv 1500000
java \
    -Xmx1000m -Xms1000m \
    -XX:MaxMetaspaceSize=500m \
    -XX:CompressedClassSpaceSize=500m \
    -XX:+ExitOnOutOfMemoryError \
    MyClass

In theory, I've accounted for everything I could find documentation on. There's the heap (1000m), and the metaspace (500m). But it still runs out of memory on startup initializing the JVM. This happens when until I set the ulimit about 600mib larger than heap+metaspace.

What category of memory am I missing such that I can set ulimit appropriately?

Use case: I am running a task in a Docker container with limited memory. That means that linux cgroups is doing the limiting. When memory limits are exceeded, cgroups can only either pause or kill the process that exceeds it's bounds. I really want the java process to gracefully fail if something goes wrong and it uses too much memory so that the wrapping bash script can report the error to the task initiator.

We are using java 8 so we need to worry about metaspace instead of permgen.

Update: It does not die with an OutOfMemoryError. This is the error:

Error occurred during initialization of VM
Could not allocate metaspace: 524288000 bytes
1 Answers
Related