Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Whether other threads can continue to work after JVM heap memory overflow

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people are at a loss about whether other threads can continue to work after JVM heap memory overflow. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Reprint note: http://dwz.win/gHc recently appeared a Meituan interview question online: "after a thread OOM, can other threads still run?" . I think there are a lot of unreliable answers on the Internet. This question is actually very difficult, involving knowledge points such as jvm memory allocation, scope, gc, etc., it is not a simple question of yes or no.

Because there are many types of OOM in the OOM,java given in the title; for example: heap overflow ("java.lang.OutOfMemoryError: Java heap space"), permanent band overflow ("java.lang.OutOfMemoryError:Permgen space"), unable to create thread ("java.lang.OutOfMemoryError:Unable to create new native thread"), and so on.

The main purpose of this paper is to analyze the impact of stack overflow on the application.

To talk about the answer first, the answer is that it still works.

The code is as follows

Public class JvmThread {public static void main (String [] args) {new Thread (()-> {List 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 2 new Thread (()-> {while (true) {System.out.println (new Date (). ToString () + Thread.currentThread () + "= ="); try {Thread.sleep (1000);} catch (Exception e) {e.printStackTrace () ). Start ();}}

The results show:

Wed Nov 07 14:42:18 CST 2018 Thread [Thread-1Power5 main] = = Wed Nov 07 14:42:18 CST 2018Thread [Thread-0Power5] = = Wed Nov 07 14:42:19 CST 2018Thread [Thread-1Power5] = = Wed Nov 07 14:42:19 CST 2018Thread [Thread-0L5] 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 2018 Thread [Thread-1 main] = = Wed Nov 07 14:42:21 CST 2018 ThreadThreadThread-1 main] = = Wed Nov 07 14:42:22 CST 2018 ThreadThread-1 Thread 5 main] = =

JVM startup parameter settings: the above figure shows the change of JVM heap space. If we take a closer look at the changes in the curve between 14-42-14-42-25, you will find that the number of heaps used has suddenly dropped sharply! This means that when a thread throws an OOM exception, all the memory resources it occupies are freed so that it does not affect the running 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 run as usual. Note that I only demonstrated the case of heap overflow in this example. If it is a stack overflow, the conclusion is the same, everyone can pass the code test on their own.

Summary: in fact, the thread that occurs OOM will usually die, that is, it will be terminated, and the heap occupied by the object held by the thread will be gc, freeing up memory. Because gc needs to be done before OOM occurs, even if other threads can work properly, it will have a greater impact because of frequent gc.

After reading the above, do you know how other threads can continue to work after the JVM heap memory overflow? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report