Skip to content

Instantly share code, notes, and snippets.

@navyxliu
Last active August 27, 2021 17:06
Show Gist options
  • Save navyxliu/8a202c9f6eef2e508f5a38b2c8a23af5 to your computer and use it in GitHub Desktop.
Save navyxliu/8a202c9f6eef2e508f5a38b2c8a23af5 to your computer and use it in GitHub Desktop.
OOMEInThread
//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