Last active
August 27, 2021 17:06
-
-
Save navyxliu/8a202c9f6eef2e508f5a38b2c8a23af5 to your computer and use it in GitHub Desktop.
OOMEInThread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//jdk9+ java -XX:+UnlockDiagnosticVMOptions -XX:AbortVMOnException=java.lang.OutOfMemoryError OOMEInThread | |
// OutOfMemoryError is an unchecked exception. please note that even it keeps throwing, | |
// it only terminates a thread but not the java process. the following command shows that OnOutOfMemoryError | |
// fails to react OOME from java and get stuck forever. | |
// | |
public class OOMEInThread { | |
public static void main(String[] args) { | |
String msg = "a long long message."; | |
// write your code here | |
Runnable runnable = () -> { | |
int cnt = Integer.MAX_VALUE / msg.length() + 1; | |
//it will throw a OutOfMemoryError. | |
msg.repeat(cnt); | |
}; | |
Thread thread = new Thread(runnable); | |
thread.start(); | |
while(true) {} // this simulates the main loop of event handling | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment