java实现内存泄漏的例子

来源:互联网 发布:火车票照片制作软件 编辑:程序博客网 时间:2024/05/16 07:00
public class ReflectTest2 {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
        //Collection collections = (Collection)Class.forName(className).newInstance();
        
        Collection collections = new HashSet();
        ReflectPoint pt1 = new ReflectPoint(3,3);
        ReflectPoint pt2 = new ReflectPoint(5,5);
        ReflectPoint pt3 = new ReflectPoint(3,3);    

        collections.add(pt1);
        collections.add(pt2);
        collections.add(pt3);
        collections.add(pt1);    

       

//以下一行注释结果为1 不注释 结果为1  也就说明 你改了成员属性 而属性参与了计算hashcode 实体放到hashset数组中别的位置 删不了了、

//remove实际是失败的 多了 就为慢慢内存溢出

        pt1.y = 7;        
        collections.remove(pt1);
        
        System.out.println(collections.size());
    }

}