内存泄漏

定义 不用的对象,资源不能被释放

场景

    1 数据库连接,文件连接没有关掉

    2 hash集合修改结合中的元素可能导致内存泄漏,因为修改元素后,hash值有可能修改,就找不到原来对象,所以不能删除了

public class Mytest {

    public static void main(String[] args) {
        Set<Point> p = new HashSet<>();
        Point p1 = new Point(2, 2);
        Point p2 = new Point(2, 3);
        p.add(p1);
        p.add(p2);
        System.out.println(p.size());
        p1.x = 10;
        p.remove(p1);
        System.out.println(p.size());
    }
}
class Point {
    int x;
    int y;
    public Point(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Point other = (Point) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }
    
}
View Code

 

posted on 2017-11-22 13:17  wheleetcode  阅读(211)  评论(0编辑  收藏  举报