On memory leakage in Java programming
Must first understand 1. C / C + + is for programmers to manage their own memory, and JAVA memory is automatically recycled by GC. Although I am not very familiar with C + +, I should not make common sense mistakes. 2。 What is a memory leak? Memory leak refers to the existence of unrecoverable memory in the system, which sometimes leads to insufficient memory or system crash. A memory leak occurs when the memory allocated in C / C + + is not released. 3。 Java has a memory leak. We must admit this before we can continue the discussion. Although Java has a memory leak, you basically don't need to care about it, especially those who don't pay attention to the code itself. Memory leaks in Java, of course, refer to objects that are useless but cannot be recycled by the garbage collector. And even if there is a memory leak problem, it may not show up. 4。 Parameters in Java are passed by value. We basically have no objection to basic types, but we can't have objection to reference types.
JAVA memory leak 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 application process of the memory is as follows: a. the JVM first attempts 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:
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 technology of dynamic proxy generation such as reflection and gclib, memory overflow can occur in this area. Generally, the error information when memory overflow occurs in this area is:
3. Thread stack overflow (Java. Lang. stackoverflowerror) thread stack is a memory structure unique to threads, 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 information of stack overflow is:
Several scenarios of memory leakage: 1. Long life cycle objects hold references to short life cycle objects, which 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. When an object is stored in the HashSet set, those fields in the object that participate in calculating the 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 collection, it will 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. The number of connections and closing time settings of the machine. Opening a very resource-consuming connection for a long time will also cause memory leakage.
Consider an example of a memory leak:
The above principle should be very simple. If 10 elements are added to the stack and all pop up, although the stack is empty and there is nothing we want, this is an object that cannot be recycled, which meets the two conditions of memory leakage: useless and unrecoverable.
However, even the existence of such things does not necessarily lead to any consequences. If this stack is used less, it will waste a few K memory. Anyway, our memory is on G. what will be the impact? Besides, this thing will be recycled soon. What does it matter. Here are two examples. Example 1
Because it is static, it will exist until the program exits, but we can also see that it has a self-healing function. That is, if your stack has at most 100 objects, only 100 objects can not be recycled. In fact, it should be easy to understand that there are 100 references in the stack. The worst case is that they are useless, because once we put new objects, Previous references disappear naturally! Example 2
The above two examples are just a trifle, but the memory leak in C / C + + is not bad, but worst. If they do not recycle, they will never recycle. If they call this method frequently, the memory will not be used up! Because Java also has self-healing function (my own name, which has not been patented), the memory leakage problem of Java can be almost ignored, but those who know it should not make it.
In order to avoid memory leakage, the following suggestions can be referred to in the process of writing code: 1. Release the references of useless objects as soon as possible; 2. Use string processing to avoid using string. Use StringBuffer extensively. Each string object must occupy a separate memory area; 3. Minimize the use of static variables, Because static variables are stored in a permanent generation (method area), the permanent generation basically does not participate in garbage collection; 4. Avoid creating objects in the loop; 5. It is easy to cause memory overflow by opening large files or taking too much data from the database at one time. Therefore, in these places, roughly calculate the maximum value of the data volume, and set the minimum and maximum memory space required.