Fundamentals 5 min read

Can Other Threads Keep Running After a Java Thread Hits OOM?

This article analyzes a Meituan interview question about whether other threads can continue when one thread throws a Java OutOfMemoryError, explains different OOM types, provides a sample program that triggers heap OOM, and demonstrates that remaining threads keep running while memory is reclaimed.

Programmer DD
Programmer DD
Programmer DD
Can Other Threads Keep Running After a Java Thread Hits OOM?

Recently a Meituan interview question asked: "If one thread throws an OutOfMemoryError, can the other threads continue to run?" The article explains that Java OOM can occur in many forms (heap, permgen, native thread creation, etc.) and focuses on the impact of heap OOM.

It provides a Java program that creates two threads: one continuously allocates 1 MB byte arrays until a heap OOM occurs, the other simply prints timestamps. The program demonstrates that after the first thread throws OOM, the second thread keeps running.

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 shows both threads printing timestamps until the heap OOM occurs in the first thread; after the exception the second thread continues to output.

Wed Nov 07 14:42:18 CST 2018Thread[Thread-1,5,main]==
Wed Nov 07 14:42:18 CST 2018Thread[Thread-0,5,main]==
...
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
...

The accompanying JVM startup parameters (shown in the image) configure a small heap to trigger the OOM quickly.

Observing the heap usage graph (second image) reveals that after the OOM the heap memory is released, so other threads are not affected.

In summary, a thread that encounters an OOM typically terminates, its heap objects are reclaimed by GC, and other threads can continue, though frequent GC may impact performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

multithreadingOutOfMemoryErrormemory-management
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.