What is the best way to deal with low memory in Java?

We have an application that generates a new JVM and executes code on behalf of our users Sometimes, there is not enough memory. In this case, the behavior is very different Sometimes they throw an outofmemoryerror, and sometimes they freeze I can detect the latter through a very lightweight background thread. When the memory is insufficient, it stops sending heartbeat signals In this case, we killed the JVM, but we can never be absolutely sure what the real reason for not receiving the heartbeat is (it may also be a network problem or segmentation error.)

What is the best way to reliably detect out of memory in a JVM?

>Theoretically, the - XX: onoutofmemoryerror option looks promising, but it is actually unavailable due to this error: https://bugs.openjdk.java.net/browse/JDK-8027434 >Capturing outofmemoryerror is actually not a good choice because of well-known reasons (for example, you will never know where it happened), although it works in many cases. > The remaining cases are those in which the JVM crashes and does not throw outofmemoryerror I'm still sure memory is the cause of the problem

Are there any alternatives or solutions? Garbage collection settings cause the JVM to terminate itself rather than freeze?

Editor: I have complete control over the forked and forked JVM, and the code being executed. They all run on Linux. If it is helpful, you can use the operating system specific utility

Solution

The only real option is (unfortunately) to terminate the virtual machine as soon as possible

Because you may not be able to change all code to catch errors and respond If you don't trust onoutofmemoryerror (I don't know why it shouldn't use VFORK used by Java 8, and it runs on Windows), you can trigger at least one heapdump and monitor these files from the outside:

java .... -XX:+HeapDumpOnOutOfMemoryError "-XX:OnOutOfMemeoryError=kill %p"
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>