Java内存泄露模拟及分析解决具体办法.docxVIP

  • 4
  • 0
  • 约5.84千字
  • 约 9页
  • 2018-11-16 发布于江苏
  • 举报

Java内存泄露模拟及分析解决具体办法.docx

Java内存泄露模拟及分析解决具体办法

e-mail:derwee@ derwee Java内存泄露模拟及分析解决方法 实践目标: 1、使用JAVA代码实现模拟内存溢出 2、分析JDK内存溢出的原因 3、总结存在bug的JAVA编码实践 4、总结JVM优化的方法 模拟内存溢出: 为了方便模拟内存,特意把JVM的内存参数指定为更小(我的本本内存是8G的)。修改eclipse参数文件eclipse.ini调用JVM参数: -vmargs -Xms40m(原始是-Xms40m) -Xmx100m(原始是-Xmx384m) 演示JAVA小程序实现原理:使用集合类对象装载大量的Persion对象,每次把new出来的对象加入集合类对象后,更改对象的属性,再从集合类对象中删除该对象。会出现该删除的对象没有被删掉,Persion类对象不断占用内存,导致分配给JVM的内存被耗光。 package com.derwee.collection.memory; import java.util.*; /** * * @ClassName: OutOfMemory * @Description: 内存溢出模拟,提出解决方法 * @author yangdw * @date 2012-3-25 下午6:58:49 */ public class OutOfMemory { public static void main(String[] args) { Collection collection = new HashSet(); for(int i=0;i900000000;i++) { Persion per = new Persion(i,yangdw); collection.add(per);//把new出来的对象加到集合里去 per.setName(hundsun);//把刚new出来的对象的名字改为 hundsun collection.remove(per); //把刚加到集合里的对象删除 //System.gc();//手工调用垃圾回收器 System.out.println(请注意,现在集合对有persion对象数--+collection.size()); } } } package com.derwee.collection.memory; /** * * @ClassName: Persion * @Description: TODO(这里用一句话描述这个类的作用) * @author yangdw * @date 2012-3-25 下午8:16:54 * */ public class Persion { public Persion(int id, String name) { super(); this.id = id; = name; } private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Persion other = (Persion) obj; if (id != other.id) return false; if (name == null) { if ( != null) return false; } else if (!name.equals())

文档评论(0)

1亿VIP精品文档

相关文档