After the jvm heap memory overflows, can other threads continue to work

Reprinted from: Original link

Author: https://gosaintmrc.github.io/

Recently, a meituan interview question appeared on the Internet: "after one thread is OOM, can other threads still run?".

I think there are many unreliable answers on the Internet.

This question is actually very difficult. The knowledge points involved include jvm memory allocation, scope, gc, etc. it is not a simple yes or no problem.

Due to the OOM given in the title, there are many types of OOM in java; For example:

  • Heap overflow ("java.lang.OutOfMemoryError: Java heap space")

  • Persistent tape overflow ("java.lang.OutOfMemoryError:Permgen space")

  • Cannot create thread ("java.lang.OutOfMemoryError:Unable to create new native thread")

And so on.

This paper mainly analyzes the impact of heap overflow on applications.

Let's talk about the answer first. The answer is that it can work.

The code is as follows:

public class JvmThread {


    public static void main(String[] args) {
        new Thread(() -> {
            List<byte[]> list = new ArrayList<byte[]>();
            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();
    }
}

Result display:

Wed Nov 07 14:42:18 CST 2018Thread[Thread-1,5,main]==
Wed Nov 07 14:42:18 CST 2018Thread[Thread-0,5,main]==
Wed Nov 07 14:42:19 CST 2018Thread[Thread-1,5,main]==
Wed Nov 07 14:42:19 CST 2018Thread[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)
 at com.gosaint.util.JvmThread$$Lambda$1/521645586.run(Unknown Source)
 at java.lang.Thread.run(Thread.java:748)
Wed Nov 07 14:42:20 CST 2018Thread[Thread-1,5,main]==
Wed Nov 07 14:42:21 CST 2018Thread[Thread-1,5,main]==
Wed Nov 07 14:42:22 CST 2018Thread[Thread-1,5,main]==

JVM startup parameter settings:

2

The above figure shows the changes of JVM heap space. Let's carefully observe the curve change between 14:42:05 and 14:42:25, and you will find that the number of heaps used suddenly drops sharply!

This means that when a thread throws an OOM exception, all the memory resources it occupies will be released, so it will not affect the operation of other threads!

At this point, you should understand that the answer to this question is that after a thread overflows, other threads in the process can still run as usual.

Note that in this example, I only demonstrate heap overflow. If it is stack overflow, the conclusion is the same. You can pass the code test by yourself.

Summary: in fact, the thread with OOM will generally die, that is, it will be terminated. The heap occupied by the object held by the thread will be gc, freeing memory. Because gc is required before OOM occurs, even if other threads can work normally, frequent gc will have a great impact.

Keywords: jvm Multithreading

Added by ForumSecure on Sun, 20 Feb 2022 08:37:52 +0200