最近一段时间,在公司里对java内存泄露的问题进行了调查。
问题的发现:
系统中在连续不停地、反复进行一个操作(先打开A,然后切替到画面B,点击画面履历再回到A,如此反复)。经过长时间的测试,经常会20小时,JVM的内存使用量增长30M以上。
问题的分析:
首先根据操作,找到会执行的代码,对代码进行分析。
Java会产生内存泄露的原因,经过本次调查,
<!--[if !supportLists]-->1. <!--[endif]-->对于打开的socket等资源,没有做及时的回收处理。
<!--[if !supportLists]-->2. <!--[endif]-->生存周期较长的对象,持有了生存周期较短的对象的引用,以至于那些生存周期短的对象,在无用的情况下,没有得到回收。
<!--[if !supportLists]-->3. <!--[endif]-->对于类的成员变量为集合的情况,对集合的使用应该谨慎。比如,一个专门保存用户操作履历的对象,有全局变量List来保存用户所有点击过的链接。但实际项目中,不可能保存住用户的每一次链接操作,然后显示给用户,有时候可能只是显示最新的20条。所以这时候就要对这个全局变量进行处理,不能让它无限的膨胀下去。
<!--[if !supportLists]-->4. <!--[endif]-->在类的成员变量为集合的情况,集合中的元素又是比较复杂的对象,(这个对象中可能还包含着是集合的成员变量)在不需要此类的对象的时候,应该自己来实现对类的成员的销毁。如:
Iterator itor = myMap.keySet().iterator(); while (itor.hasNext()) { MyObject selectedInfo = (MyObject) itor.next(); selectedInfo.destroy();// 假设MyObject里有destroy方法,对MyObjec中的成员进行销毁 selectedInfo = null; } myMap.clear();
<!--[if !supportLists]-->5. <!--[endif]-->对单态模式应该慎用,单例对象在被初始化后将在JVM的整个生命周期中存在,如果单态对象持有外部对象的引用,那么这个外部对象将不能被回收,如故这个外部对象很庞大,那么对内存的消耗是很大的。
虽然写java程序,有GC帮助我们管理内存,但好的编程习惯还是需要的,可以避免不必要的麻烦。
<!--[if !supportLists]-->1. <!--[endif]-->复杂的对象,在不需要的情况下,最好能实现对它的成员的销毁,然后再将其赋为null。
<!--[if !supportLists]-->2. <!--[endif]-->对于打开的流,一定要做及时的处理。另外对于HttpURLConnection对象,连接后,要调用它的disconnect(),不要对资源进行不必要的浪费。
<!--[if !supportLists]-->3. <!--[endif]-->尽量少用全局变量。
<!--[if !supportLists]-->4. <!--[endif]-->在哪里生成对象,就在哪里销毁它。
<!--[if !supportLists]-->5. <!--[endif]-->尽量避免对象之间的相互引用。
最后,记述一下我记录内存的方法。
由于对代码做好修改之后,要确认一下内存是否有明显增长。
于是写一段代码,每个5分钟对对内存进行一次记录,在连续运行20小时候,做成曲线图,以便分析。
(以下是为了方便,重新写的,原来项目中用到的,有一整套完备的定时器生成和起动的管理类,这里没有写出来。)
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MemoryCollect {
public static void main(String args[]) {
MemoryCollector mc = new MemoryCollector(300000);
mc.start();
}
}
class MemoryCollector extends java.util.TimerTask {
private static final java.text.NumberFormat nf = java.text.NumberFormat
.getPercentInstance();
private static final java.text.DateFormat df = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
private DataOutputStream dos = null;
private long period = 0;
private java.util.Timer timer = null;
MemoryCollector(long p) {
period = p;
timer = new java.util.Timer();
}
public void start() {
timer.schedule(this, 0, period);
}
public void run() {
System.gc();
Runtime imp = Runtime.getRuntime();
imp.totalMemory();
long totol = imp.totalMemory() / 1024;
long free = imp.freeMemory() / 1024;
try {
dos = new DataOutputStream(new FileOutputStream("D://memory.txt",
true));
String date = df.format(new java.util.Date());
String info = date + "/t" + totol + "/t" + free + "/t"
+ nf.format((double) free / (double) totol);
System.out.println(info);
dos.writeUTF(info + "/r");
dos.flush();
dos.close();
dos = null;
} catch (Exception e) {
System.out.println(e);
} finally {
try {
if (dos != null) {
dos.flush();
dos.close();
dos = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void stop() {
super.cancel();
if (timer != null) {
timer.cancel();
timer = null;
}
}
}