java memory leak problem

1. What is a memory leak in java

内存泄漏定义:无用对象持续占有内存或无用对象的内存得不到释放,从而造成内存空间的浪费。
满足的条件:
1.对象与GCROOT的节点相连,不是垃圾对象;
2.对象是无用的
这些对象不会被GC所回收,然而它却占用内存
内存泄漏的根本原因:长生命周期的对象持有短生命周期对象的引用就很可能发生内存泄漏,尽管短生命周期对象
已经不再需要,但是因为长生命周期持有它的引用而导致不能被回收,这就是Java中内存泄漏的发生场景。

2. classic example

//1.静态集合类引起内存泄漏:
Static  Vector v = new Vector(10);
for (int i = 1; i < 100; i++) {
Object o = new Object();
v.add(o);
o = null;
}

//代码中虽然o的引用为null,但是仅仅释放了引用本身,依然被Vector 引用,所有gc不能回收。
  //如果需要gc回收,必须释放Vector对象中的引用。
//2.当集合里面的对象属性被修改后,再调用remove()方法时不起作用。
public static void main(String[] args)
{
	Set<Person> set = new HashSet<Person>();
	Person p1 = new Person("唐僧","pwd1",25);
	Person p2 = new Person("孙悟空","pwd2",26);
	Person p3 = new Person("猪八戒","pwd3",27);
	set.add(p1);
	set.add(p2);
	set.add(p3);
	System.out.println("总共有:"+set.size()+" 个元素!"); //结果:总共有:3 个元素!
	p3.setAge(2); //修改p3的年龄,此时p3元素对应的hashcode值发生改变
	set.remove(p3); //此时remove不掉,造成内存泄漏
	set.add(p3); //重新添加,居然添加成功
	System.out.println("总共有:"+set.size()+" 个元素!"); //结果:总共有:4 个元素!
	
	for (Person person : set){
		System.out.println(person);
	}
}

3. Listeners: when releasing the object did not remember to remove these listeners, thus increasing the chance of a memory leak.
4. Database Connectivity (dataSourse.getConnection ()), network connection (Socket) and io connection, unless explicitly call its close () method to which the connection is closed, otherwise it is not automatically recovered GC.

Published 77 original articles · won praise 39 · views 80000 +

Guess you like

Origin blog.csdn.net/qq_33824312/article/details/105126035