如何正确使用ulimit与java?(How to use ulimit with java correctly?)
我的java程序必须在内存受限于指定数量的环境中运行。 当我运行我的java服务时,它在启动期间耗尽了内存。
这是我正在使用的命令和我正在设置的值的示例:
ulimit -Sv 1500000 java \ -Xmx1000m -Xms1000m \ -XX:MaxMetaspaceSize=500m \ -XX:CompressedClassSpaceSize=500m \ -XX:+ExitOnOutOfMemoryError \ MyClass
从理论上讲,我已经考虑了所有可以找到文档的内容。 堆(1000m)和元空间(500m)。 但是在初始化JVM时它仍然会耗尽内存。 直到我将ulimit设置为大于堆+ metaspace大约600mib时才会发生这种情况。
我错过了什么类型的记忆,以便我可以适当地设置ulimit?
使用案例:我在内存有限的Docker容器中运行任务。 这意味着linux cgroups正在进行限制。 当超出内存限制时,cgroup只能暂停或终止超出其边界的进程。 我真的希望java进程在出现问题时优雅地失败并且它使用太多内存以便包装bash脚本可以向任务启动器报告错误。
我们使用java 8所以我们需要担心metaspace而不是permgen。
更新:它不会因
OutOfMemoryError
而死亡。 这是错误:Error occurred during initialization of VM Could not allocate metaspace: 524288000 bytes
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
原文:https://stackoverflow.com/questions/44273737