JAVA memory overflow and memory leak

Although the JVM can automatically reclaim useless memory through GC, there is still a risk of memory overflow if the code is bad.

1、 Why learn about memory leaks and memory overflows?

1. Memory leakage is generally caused by defects in code design. By understanding the memory leakage scenario, you can avoid unnecessary memory overflow and improve your coding level;

2. By understanding several common situations of memory overflow, you can quickly locate the problem when memory overflow occurs and shorten the time to solve the fault.

2、 Basic concepts

It is important to understand these two concepts.

Memory leak: refers to the dynamic allocation of memory to some temporary objects in the program, but the object will not be recycled by GC. It always occupies memory. That is, the allocated object is reachable but useless.

Memory overflow: refers to an error caused by the failure to apply for enough memory during program operation. Memory overflow usually occurs when there is no memory space to accommodate new Java objects after old segment or perm segment garbage collection.

From the definition, we can see that memory leakage is an inducement of memory overflow, not the only factor.

3、 Several scenarios of memory leakage:

1. Long lifecycle objects hold references to short lifecycle objects

This is the most common scenario of memory leakage and a common problem in code design.

For example, if local variables are cached in the global static map without emptying, the map will become larger and larger over time, resulting in memory leakage.

2. Modify the parameter value of the object in the HashSet, and the parameter is the field for calculating the hash value

After an object is stored in the HashSet set, the fields in the object that participate in the calculation of hash value cannot be modified. Otherwise, the modified hash value of the object is different from the hash value originally stored in the HashSet set. In this case, even if the contains method uses the current reference of the object as a parameter to retrieve the object in the HashSet set, It will also return the result that the object cannot be found, which will also lead to the failure to delete the current object from the HashSet collection, resulting in memory leakage.

3. Number of connections and shutdown time settings for the machine

Opening a very resource consuming connection for a long time will also cause memory leakage.

4、 Several situations of memory overflow:

1. Heap memory overflow (outofmemoryerror: Java heap space)

In the JVM specification, the memory in the heap is used to generate object instances and arrays.

If subdivided, the heap memory can also be divided into the younger generation and the older generation. The younger generation includes an Eden area and two survivor areas.

When a new object is generated, the memory application process is as follows:

a. The JVM first tries to allocate the memory required for the new object in the Eden area;

b. If the memory size is sufficient, the application ends, otherwise, go to the next step;

c. The JVM starts the younggc and tries to release the inactive objects in the Eden area. After the release, if the Eden space is still insufficient to put new objects, it tries to put some active objects in Eden into the survivor area;

d. The survivor area is used as the intermediate exchange area between Eden and old. When the old area has enough space, the objects in the survivor area will be moved to the old area, otherwise they will be retained in the survivor area;

e. When the space in the old area is insufficient, the JVM will perform full GC in the old area;

f. After full GC, if the survivor and old areas still cannot store some objects copied from Eden, so that the JVM cannot create a memory area for new objects in Eden area, an "out of memory error" occurs:

outOfMemoryError:java heap space

Code example:

2. Method area memory overflow (outofmemoryerror: permgem space)

In the JVM specification, the method area mainly stores class information, constants, static variables, etc.

Therefore, if the program loads too many classes, or uses the dynamic proxy generation technology such as reflection and gclib, memory overflow may occur in this area. Generally, the error message when memory overflow occurs in this area is: outofmemoryerror: permgem space

Code example:

1. JVM parameters: - XX: permsize = 2m - XX: maxpermsize = 2m

2. Set the size of the method area to be very low, and there will be insufficient memory when loading the class library

3. Thread stack overflow (Java. Lang. stackoverflowerror)

Thread stack is a unique memory structure of a thread, so the problem of thread stack must be an error caused by a thread running.

Generally, thread stack overflow is caused by too deep recursion or too many method call levels.

The error message of stack overflow is:

java. lang.StackOverflowError

Code example:

5、 To avoid memory leakage, you can refer to the following suggestions during code writing:

1. Release references to useless objects as soon as possible

2. Use string processing, avoid using string, and use StringBuffer extensively. Each string object must occupy an independent memory area

3. Use static variables as little as possible, because static variables are stored in the permanent generation (method area), and the permanent generation basically does not participate in garbage collection

4. Avoid creating objects in loops

5. Opening a large file or taking too much data from the database at one time can easily lead to memory overflow. Therefore, in these places, it is necessary to roughly calculate the maximum amount of data and set the minimum and maximum memory space required.

summary

The above is the JAVA memory overflow and memory leak introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>