Recently, I have investigated Java Memory leakage in my company.
Problem Discovery:
The system continuously and repeatedly performs an operation (open a first, switch to B, click the image resume, and then return to ). After a long period of testing, the JVM memory usage often increases by more than 30 mb.
Problem Analysis:
First, find the code that will be executed according to the operation and analyze the code.
The cause of Memory leakage in Java. After this investigation,
1.
The open socket and other resources are not recycled in a timely manner.
2.
Objects with long lifecycles hold references of objects with short lifecycles, so that objects with short lifecycles are not recycled if they are useless.
3.
Exercise caution when class member variables are set. For example, an object that stores user operation records has a global variable list to save all the links you have clicked. However, in a project, it is impossible to save each link operation of the user and display it to the user. Sometimes it may only display the latest 20 links. Therefore, the global variable must be processed at this time, so it cannot be expanded infinitely.
4.
When the member variables of a class are set, the elements in the set are complex objects (this object may also contain member variables of the set) when you do not need such objects, destroy the class members by yourself. For example:
Iterator itor = mymap. keyset (). iterator (); <br/> while (itor. hasnext () {<br/> myobject selectedinfo = (myobject) itor. next (); <br/> selectedinfo. destroy (); // assume that the destroy method exists in myobject and the members in myobjec are destroyed. <br/> selectedinfo = NULL; <br/>}< br/> mymap. clear ();
5.
The single-State mode should be used with caution. The single-State object will exist throughout the life cycle of JVM after initialization. If the single-State object holds the reference of the external object, therefore, this external object cannot be recycled. For example, if this external object is large, the memory consumption is very high.
Although we write Java programs and have GC to help us manage the memory, good programming habits are still needed to avoid unnecessary troubles.
1.
It is recommended that you destroy a complex object without any need, and then assign it to null.
2.
You must promptly process the opened stream. In addition, for the httpurlconnection object, do not waste resources by calling its disconnect () after the connection.
3.
Use as few global variables as possible.
4.
Where an object is generated, it is destroyed.
5.
Avoid mutual reference between objects as much as possible.
At last, I will describe how to record the memory.
After modifying the code, check whether the memory has increased significantly.
Therefore, write a piece of code, record the memory every five minutes, and create a graph for analysis during 20 consecutive hours.
(The following is for convenience and re-writing. A complete set of timer generation and startup management classes are used in the original project. It is not written here .)
Import Java. io. dataoutputstream; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; <br/> public class memorycollect {<br/> Public static void main (string ARGs []) {<br/> memorycollector MC = new memorycollector (300000 ); <br/> MC. start (); <br/>}< br/> class memorycollector extends Java. util. timertask {<br/> Private Static final Java. text. numberformat NF = Java. text. numberform At <br/>. getpercentinstance (); <br/> Private Static final Java. text. dateformat df = new Java. text. simpledateformat (<br/> "yyyy-mm-dd hh: mm: SS"); <br/> private dataoutputstream dos = NULL; <br/> private long period = 0; <br/> private Java. util. timer timer = NULL; <br/> memorycollector (long p) {<br/> period = P; <br/> timer = new Java. util. timer (); <br/>}< br/> Public void start () {<br/> timer. schedule (thi S, 0, period); <br/>}< br/> Public void run () {<br/> system. GC (); <br/> runtime imp = runtime. getruntime (); <br/> imp. totalmemory (); <br/> long totol = imp. totalmemory ()/1024; <br/> long free = imp. freememory ()/1024; <br/> try {<br/> dos = new dataoutputstream (New fileoutputstream ("D: // memory.txt", <br/> true )); <br/> string date = DF. format (New Java. util. date (); <br/> string info = date + "/t" + Toto L + "/t" + free + "/t" <br/> + NF. format (double) Free/(double) totol); <br/> system. out. println (Info); <br/> dos. writeutf (Info + "/R"); <br/> dos. flush (); <br/> dos. close (); <br/> dos = NULL; <br/>} catch (exception e) {<br/> system. out. println (E); <br/>}finally {<br/> try {<br/> If (dos! = NULL) {<br/> dos. flush (); <br/> dos. close (); <br/> dos = NULL; <br/>}< br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> Public void stop () {<br/> super. cancel (); <br/> If (timer! = NULL) {<br/> timer. Cancel (); <br/> timer = NULL; <br/>}< br/>}