当前位置 : 主页 > 编程语言 > c++ >

Java代码内存泄漏Demo

来源:互联网 收集:自由互联 发布时间:2021-06-30
JavaMemoryLeak public class JavaMemoryLeak {public static void main(String[] args) {People p1 = new People("libai", 11);People p2 = new People("dufu", 22);People p3 = new People("qingzhao", 33);HashSet set = new HashSet (); set.add(p1); set
JavaMemoryLeak
public class JavaMemoryLeak {
	public static void main(String[] args) {
		People p1 = new People("libai", 11);
		People p2 = new People("dufu", 22);
		People p3 = new People("qingzhao", 33);
		HashSet
 
   set = new HashSet
  
   (); set.add(p1); set.add(p2); set.add(p3); System.out.println(set.size()); p3.setAge(999); //set.add(p3); set.remove(p3); System.out.println(set.size()); } } class People { private String name; private int age; public People(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { return (this.name + this.age).hashCode(); } }
  
 
网友评论