Skip to content

Instantly share code, notes, and snippets.

@hossman
Last active January 22, 2020 22:38
Show Gist options
  • Select an option

  • Save hossman/e6a9c12d40251ef6e2437f91d417671e to your computer and use it in GitHub Desktop.

Select an option

Save hossman/e6a9c12d40251ef6e2437f91d417671e to your computer and use it in GitHub Desktop.
OOM Handling w/ThreadPools
import java.util.concurrent.*;
public final class Temp {
public static void triggerOOM(boolean real) {
if (real) {
final long[][] trash = new long[Integer.MAX_VALUE][Integer.MAX_VALUE];
// if we make it this far complain
System.err.println("Your JVM heap is too big for this test");
System.exit(-1);
}
throw new OutOfMemoryError("Fake OOM from thread " + Thread.currentThread().getId());
}
public static void main(String[] args) throws Exception {
final boolean real = 1 <= args.length;
System.out.println("All OOMs will be " + (real ? "REAL" : "FAKE"));
final ExecutorService exec = Executors.newFixedThreadPool(3);
for (int i = 0; i < 5; i++) {
final String id = "R" + i;
exec.submit(() -> {
System.out.println(Thread.currentThread().getId() + "# " + id + " Starting...");
try {
Thread.sleep(3000);
} catch (InterruptedException ignored) {
System.out.println(Thread.currentThread().getId() + "# " + id + " ...Interupted");
return;
}
try {
System.out.println(Thread.currentThread().getId() + "# " + id + " Throwing...");
triggerOOM(real);
} catch (OutOfMemoryError oome) {
System.out.println(Thread.currentThread().getId() + "# " + id + " ...caught & re-thrown...");
throw oome;
}
});
}
System.out.println("All Tasks submitted, begining shutdown...");
exec.shutdown();
exec.awaitTermination(5000, TimeUnit.HOURS);
System.out.println("... terminated cleanly, trigging main OOM...");
triggerOOM(real);
}
}
/bin/sh -c 'echo oom.sh called'
#!/bin/sh
set -x
javac Temp.java
java -XX:OnOutOfMemoryError=/home/hossman/tmp/oom.sh Temp
java -XX:OnOutOfMemoryError=/home/hossman/tmp/oom.sh Temp REAL
java -XX:+CrashOnOutOfMemoryError Temp
java -XX:+CrashOnOutOfMemoryError Temp REAL
java -XX:+ExitOnOutOfMemoryError Temp
java -XX:+ExitOnOutOfMemoryError Temp REAL
hossman@slate:~/tmp$ ./do_stuff.sh
+ javac Temp.java
+ java -XX:OnOutOfMemoryError=/home/hossman/tmp/oom.sh Temp
All OOMs will be FAKE
All Tasks submitted, begining shutdown...
14# R2 Starting...
12# R0 Starting...
13# R1 Starting...
14# R2 Throwing...
12# R0 Throwing...
13# R1 Throwing...
12# R0 ...caught & re-thrown...
14# R2 ...caught & re-thrown...
12# R3 Starting...
13# R1 ...caught & re-thrown...
14# R4 Starting...
12# R3 Throwing...
12# R3 ...caught & re-thrown...
14# R4 Throwing...
14# R4 ...caught & re-thrown...
... terminated cleanly, trigging main OOM...
Exception in thread "main" java.lang.OutOfMemoryError: Fake OOM from thread 1
at Temp.triggerOOM(Temp.java:12)
at Temp.main(Temp.java:43)
+ java -XX:OnOutOfMemoryError=/home/hossman/tmp/oom.sh Temp REAL
All OOMs will be REAL
All Tasks submitted, begining shutdown...
12# R0 Starting...
14# R2 Starting...
13# R1 Starting...
13# R1 Throwing...
14# R2 Throwing...#
# java.lang.OutOfMemoryError: Requested array size exceeds VM limit
# -XX:OnOutOfMemoryError="/home/hossman/tmp/oom.sh"
# Executing /bin/sh -c "/home/hossman/tmp/oom.sh"...
oom.sh called
12# R0 Throwing...
13# R1 ...caught & re-thrown...
14# R2 ...caught & re-thrown...
12# R0 ...caught & re-thrown...
13# R3 Starting...
12# R4 Starting...
13# R3 Throwing...
12# R4 Throwing...
13# R3 ...caught & re-thrown...
12# R4 ...caught & re-thrown...
... terminated cleanly, trigging main OOM...
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
+ java -XX:+CrashOnOutOfMemoryError Temp
All OOMs will be FAKE
All Tasks submitted, begining shutdown...
12# R0 Starting...
14# R2 Starting...
13# R1 Starting...
13# R1 Throwing...
14# R2 Throwing...
12# R0 Throwing...
13# R1 ...caught & re-thrown...
12# R0 ...caught & re-thrown...
14# R2 ...caught & re-thrown...
13# R3 Starting...
12# R4 Starting...
13# R3 Throwing...
12# R4 Throwing...
13# R3 ...caught & re-thrown...
12# R4 ...caught & re-thrown...
... terminated cleanly, trigging main OOM...
Exception in thread "main" java.lang.OutOfMemoryError: Fake OOM from thread 1
at Temp.triggerOOM(Temp.java:12)
at Temp.main(Temp.java:43)
+ java -XX:+CrashOnOutOfMemoryError Temp REAL
All OOMs will be REAL
All Tasks submitted, begining shutdown...
14# R2 Starting...
13# R1 Starting...
12# R0 Starting...
13# R1 Throwing...
Aborting due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit
14# R2 Throwing...#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (debug.cpp:321)
, pid=18669, tid=18687
# fatal error: OutOfMemory encountered: Requested array size exceeds VM limit
#
# JRE version: OpenJDK Runtime Environment (11.0.4+11) (build 11.0.4+11)
# Java VM: OpenJDK 64-Bit Server VM (11.0.4+11, mixed mode, tiered, compressed oops, g1 gc, linux-amd64)
# Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport %p %s %c %d %P" (or dumping to /home/hossman/tmp/core.18669)
#
12# R0 Throwing...# An error report file with more information is saved as:
#
/home/hossman/tmp/hs_err_pid18669.log
12# R0 ...caught & re-thrown...
14# R2 ...caught & re-thrown...
12# R3 Starting...
14# R4 Starting...
#
# If you would like to submit a bug report, please visit:
# https://github.com/AdoptOpenJDK/openjdk-build/issues
#
Aborted (core dumped)
+ java -XX:+ExitOnOutOfMemoryError Temp
All OOMs will be FAKE
All Tasks submitted, begining shutdown...
13# R1 Starting...
14# R2 Starting...
12# R0 Starting...
14# R2 Throwing...
12# R0 Throwing...
13# R1 Throwing...
12# R0 ...caught & re-thrown...
13# R1 ...caught & re-thrown...
13# R3 Starting...
14# R2 ...caught & re-thrown...
12# R4 Starting...
13# R3 Throwing...
12# R4 Throwing...
13# R3 ...caught & re-thrown...
12# R4 ...caught & re-thrown...
... terminated cleanly, trigging main OOM...
Exception in thread "main" java.lang.OutOfMemoryError: Fake OOM from thread 1
at Temp.triggerOOM(Temp.java:12)
at Temp.main(Temp.java:43)
+ java -XX:+ExitOnOutOfMemoryError Temp REAL
All OOMs will be REAL
All Tasks submitted, begining shutdown...
12# R0 Starting...
14# R2 Starting...
13# R1 Starting...
12# R0 Throwing...
Terminating due to java.lang.OutOfMemoryError: Requested array size exceeds VM limit
14# R2 Throwing...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment