In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about Java memory overflow scenarios and solutions. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Java memory overflow means that when the program requests memory, there is not enough space for it to use, resulting in out of memory. It is common in four situations: stack overflow (StackOverflowError), heap overflow (OutOfMemoryError:java heap space), permanent generation overflow (OutOfMemoryError: PermGen space), and OutOfMemoryError:unable to create native thread.
1. Stack overflow
First figure out what is stored in the java stack space. The java stack space is thread-private and is the memory model for java method execution. When each method is executed, it will generate a stack frame in the java stack space, store the variable table of the method, return value and other information. The end of the execution of the method is a stack frame into the stack to the stack process.
Therefore, the reason for stack overflow is that the number of stack frames increases and the stack depth increases due to the circular call of the method, resulting in no memory to be allocated, resulting in StackOverflowError, such as the following:
Public class stack {
Public void test () {
This.test ()
}
Public static void main (String [] args) {
For (;;)
New stack () test
}
}
Stack memory overflow is generally caused by program errors, such as recursive dead loops and so on.
2. Heap overflow
Java heap is a common area of threads, which is mainly used to store object instances. Almost all java objects allocate memory here, and it is also the largest area of JVM memory management. Java heap memory is divided into the young generation and the old generation, and the heap memory overflow is usually the old generation overflow. When a program continues to create a large number of object instances and is not recycled by GC, it is easy to produce memory overflows. When an object is generated, the main process is as follows:
JVM first allocates memory for it in the Eden area of the younger generation
If the allocation is successful, it ends, otherwise JVM will trigger a Young GC in an attempt to release inactive objects in the Eden area.
If there is not enough memory space after the release, some active objects in the Eden area will be transferred to the Suvivor area, and the long-term living objects in the Suvivor area will be transferred to the old age.
When there is not enough space in the old days, Full GC will be triggered to carry out complete garbage collection for the elderly.
After recycling, if the Suvivor and the old still do not have enough space to receive the copied objects from the Eden, so that the Eden area can not allocate memory for the newly generated objects, that is, overflow.
Thus, when the program continues to create a large number of object instances and is not recycled by GC, it is easy to produce memory overflow. As follows:
Public class heap {
Public static void main (String [] args) {
ArrayList list = new ArrayList ()
While (true) {
List.add (new heap ())
}
}
}
Heap memory overflow is likely to be accompanied by memory leaks, so you should first troubleshoot objects that may be leaked, and then check the GC roots reference chain through tools to find out what kind of reference relationship makes GC unable to recycle them; if there is no memory leak, in other words, objects in memory still need to survive, you can increase the heap memory by modifying the heap parameters of the virtual machine.
3. Permanent generation overflow
Permanent generation is also a part of java heap memory, which is mainly used to store Class-related information, such as class names, access modifiers, and so on. The general reason for permanent generation overflow is that a large number of Class is dynamically loaded and not reclaimed by GC in time. It can only be solved by adjusting the permanent memory parameters.
4. Unable to create local thread
We know that the operating system has certain limits on the memory of each process, and when heap memory and non-heap memory allocation are too large, the remaining memory is not enough to create enough thread stacks, OutOfMemoryError will be generated. So we can solve the problem by increasing the total memory occupied by the process or reducing the heap memory.
Summary
Stack memory overflow: the stack depth required by the program is too large.
Heap memory overflow: distinguish between memory leaks or insufficient memory capacity. Disclosure depends on how the object is referenced by GC Root. The deficiency is achieved by increasing the-Xms,-Xmx parameter.
Persistent memory overflow: Class objects are not released, Class objects take up too much information, and there are too many Class objects.
Unable to create local thread: the total capacity remains the same, heap memory, non-heap memory setting is too large, will result in insufficient memory for the thread.
The above is the Java memory overflow scenario and solutions shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.