Can Other Threads Keep Running After One Thread Throws an OutOfMemoryError?
The article explains that when a Java thread encounters a heap OutOfMemoryError, the thread terminates but its allocated heap memory is reclaimed, allowing the remaining threads in the same process to continue operating, and demonstrates this with a multithreaded example.
Recently a Meituan interview question asked whether other threads can keep running after one thread experiences an OutOfMemoryError (OOM). The answer is yes, and the article analyzes this by focusing on heap OOM cases.
Java OOM can occur in several forms, such as java.lang.OutOfMemoryError: Java heap space, java.lang.OutOfMemoryError: PermGen space, and
java.lang.OutOfMemoryError: Unable to create new native thread. This discussion concentrates on the heap space variant.
The provided Java program starts two threads: the first continuously allocates 1 MiB byte arrays and stores them in a list, eventually exhausting the heap and triggering an OOM; the second thread simply prints timestamps.
public class JvmThread {
public static void main(String[] args) {
new Thread(() -> {
List<byte[]> list = new ArrayList<>();
while (true) {
System.out.println(new Date().toString() + Thread.currentThread() + "==");
byte[] b = new byte[1024 * 1024 * 1];
list.add(b);
try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); }
}
}).start();
// Thread two
new Thread(() -> {
while (true) {
System.out.println(new Date().toString() + Thread.currentThread() + "==");
try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); }
}
}).start();
}
}Running the program produces output similar to:
Wed Nov 07 14:42:18 CST 2018 Thread[Thread-1,5,main]==
Wed Nov 07 14:42:18 CST 2018 Thread[Thread-0,5,main]==
...
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
at com.gosaint.util.JvmThread.lambda$main$0(JvmThread.java:21)
...
Wed Nov 07 14:42:20 CST 2018 Thread[Thread-1,5,main]==
Wed Nov 07 14:42:21 CST 2018 Thread[Thread-1,5,main]==Observing the JVM heap usage graph (shown in the original images) reveals a sharp drop in heap consumption after the OOM occurs, indicating that the memory held by the failing thread is released, so other threads are not blocked.
The conclusion is that a thread that throws an OOM typically dies, its heap objects are reclaimed by GC, and the remaining threads can continue to run, although frequent GC may affect overall performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
