I remember the last time I went to the XX Company for an interview. An interviewer asked me if Java was leaked. I said yes, but then I suddenly paused. The interviewer immediately corrected it for me with certainty, there is no memory leakage in Java. I dare not argue any more. I know the situation at that time, which is also unquestionable. After I came back, I checked the information and sorted out several memory leaks to avoid mistakes. I 'd like to share them with you now.
A big difference between Java and C ++Java has a garbage collection mechanism. GC automatically manages memory collection. Therefore, developers do not need to call methods to release the memory, while C ++ does not have a mechanism to automatically recycle the memory, therefore, we need to pay attention to the memory recovery situation everywhere. If you do not pay attention to it, memory leakage may occur.
But does this mean that there is no memory leakage in Java? The answer is no. In some cases, it may also lead to Java writing.ProgramMemory leakage occurs.
Java memory reclaim Mechanism
In either language, the actual address of the allocated memory must be returned, that is, the first address from the pointer to the memory block. In Java, objects are created using the new or reflection method. The creation of these objects is allocated in heap, all objects are recycled by the Java Virtual Machine through the garbage collection mechanism. In order to release objects correctly, GC monitors the running status of each object, including their application, reference, reference, and assignment, java uses the directed graph method to manage the memory. If the real-time monitoring object can be reached, it will be recycled if it cannot be reached. This can also eliminate the issue of reference loops. In Java, two methods are used to determine whether a memory space meets the garbage collection standard: one is to assign null values to the object, and the following are not called, the other is to assign a new value to the object, which re-allocates the memory space.
Causes of Java Memory leakage
1. The use of static collection classes such as hashmap and vector is most prone to memory leakage. The lifecycle of these static variables is the same as that of the application, and all objects cannot be released, because they will also be applied by vector and so on.
2. Internal and external class references are prone to memory leakage.
3. The Listener is often used in Java. when an object is released and no listener is deleted, memory leakage may occur.
4. When a large number of temporary variables are used, if the object is not set to null in time, memory leakage may occur.
5. The database connection is not closed, including connection pool methods to connect to the database. If resultset is not closed, memory leakage may also occur.
Here is an example of Memory leakage:
Vector v = new vector (10 );
For (INT I = 1; I <100; I ++ ){
Object o = new object ();
V. Add (O );
O = NULL;
}
Do not think that the O object has been recycled. In fact, V references these objects and the referenced object GC cannot be recycled, which may cause memory leakage.
I hope you can give me some advice on what is wrong or what needs to be supplemented. Thank you.