Java Memory leakage and java Leakage
Java Memory leakage is mainly divided into two aspects:
(1) The space applied for in the heap is not released
(2) The object is no longer in use, but still exists in the memory.
Memory leakage may occur in the following cases:
(1) When a static set uses hashmap and vector, the lifecycle of the static container is the same as that of the program, so the container cannot be released before the program ends, resulting in Memory leakage;
For example:
vector v=new vector()for(int i=0;i<10;i++){object o=new object();v.add(o);}
O The object is no longer needed, but cannot be recycled
(2) Various connection problems: database connections and I/O connections need to call the close method to release connections, such as connection, statement, resultset
(3) Listener usage
(4) unreasonable scope of Variables
1) The scope of a variable definition is greater than the scope of use:
Msgd should be used in some aspects.
Class server {private string msg; public void recieveMsg () {readMsg (); // obtain msg savaDB (); // input db }}
2) the object is not set to null in time
Or set msg to null after use.
(5) Use of Singleton Mode
Class BigClass {...} classSingleton {private BigClass bc; ...... // The bc object cannot be recycled.
Garbage collection algorithms include:
(1) reference counting algorithm
(2) tracking and recycling algorithm, reference diagram
(3) compression and recycling Algorithm
(4) copy and recycle Algorithms
(5) collection by Generation Algorithm