Package com.ckw.mianshi;/** * Test memory leaks * Several events in Java that cause memory leaks: * A reference to an object that is held in a container such as 1.hashmap,verctor, although the object is already set to NULL, a memory leak * 2 will still occur. The use of the Listener class, no display is set to NULL * 3. The Close method of the database connection class did not call * 4. A reference to another class in a singleton mode * @author Administrator * */import java.util.vector;public CL memoryleak {static Vector<object> v = new Vector<object> (Ten);p ublic static void Main (string[] args) {long Starttime=system.currenttimemillis (); Gets the start time for (int i=0; i<10; i++) {Object o = new Object (); V.add (o); o = null;//even if O is still holding object o in Null,v, resulting in memory leaks}// The correct practice is to use the V-space//v = Null;long endtime=system.currenttimemillis () after using V; Get End Time System.out.println ("program Run Time:" + (Endtime-starttime) + "MS");}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Memory leak Quiz