1. What is the nature of memory leaks?
A long life cycle object that references a short life cycle object, and the long life cycle always holds a reference to the life cycle of a segment resulting in the inability to reclaim short life cycle objects
2. Static collection classes cause memory leaks
Because static objects are the same as the life cycle of an application
Static vector v = new vector, for (int i = 1; I< }//In this example, a loop applies to the Object object and puts the requested objects into a vector, and if you just release the reference itself (o Still references the object, the object is not recyclable to the GC.) Therefore, if the object has to be removed from the vector after it has been added to the vector, the simplest way is to set the vector object to null.
3. The remove operation of the collection does not work
Public Static voidMain (string[] args) {Set<Person> set =NewHashset<person>(); Person P1=NewPerson ("Tang priest", "Pwd1", 25); Person P2=NewPerson ("Monkey King", "Pwd2", 26); Person P3=NewPerson ("Pig", "pwd3", 27); Set.add (p1); Set.add (p2); Set.add (p3); System.out.println ("A total of:" +set.size () + "Elements!");//results: Total: 3 elements!P3.setage (2);//modifies the age of P3, at which point the P3 element corresponds to a hashcode value that changesSet.remove (p3);//The Remove does not drop at this time, causing a memory leakSet.add (p3);//re-add, incredibly add successSystem.out.println ("A total of:" +set.size () + "Elements!");//results: Total: 4 Elements! for(person Person:set) {System.out.println (person);}}
4. Internal classes and external modules
The reference to the inner class is a relatively easy one to forget, and once it is not released it can cause a series of subsequent class objects not to be released. In addition, the programmer should be careful of the external module inadvertently referenced, such as programmer A is responsible for a module, a method called the B module, such as:
public void registermsg (Object b);
This call will be very careful, passing in an object, it is likely that module B will maintain a reference to the object, it is important to note that Module B provides the appropriate action to remove the reference.
5. Single-Case mode
Improper use of singleton mode is a common problem that causes memory leaks, where a singleton object will exist in the JVM's lifetime (in the form of a static variable) after it is initialized, and if the Singleton object holds a reference to an external object, the external object will not be properly reclaimed by the JVM, causing a memory leak. Consider the following example:classa{ PublicA () {b.getinstance (). SetA ( This); } .... } //Class B with single-case modeclassb{PrivateA;Private StaticB instance=NewB (); PublicB () {} Public StaticB getinstance () {returninstance;} Public voidSetA (A a) { This. a=A;} //getter ...It is obvious that B is in singleton mode, which holds a reference to an A object, and this class A object cannot be recycled. Imagine what happens if a is a more complex object or a collection type. The principle mechanism of the JVM loading class file The next Java stack differs from the heap top
Understand the nature of memory leaks