Hashset 使用不当引起的内存泄漏

2/10/2017来源:ASP.NET技巧人气:866

修改hashset中对象的属性值,且属性值是计算哈希值的字段,这时会引起内存泄漏

即:当一个对象被存储进HashSet集合中以后,就不能修改该对象的参与计算哈希值的属性值了 ,否则对象修改后的哈希值与最初存储进HashSet集合中时的哈希值就不同了,在这种情况下,即使在contains方法使用该对象的当前引用作为参数去HashSet集合中检索对象,也将返回找不到对象的结果,这也会导致无法从HashSet集合中删除当前对象,造成内存泄露。

举例说明:

public class HashSetTest { 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); } } } public class Person { private String username; private String passWord; private int age; public Person(String username, String password, int age) { this.username = username; this.password = password; this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((username == null) ? 0 : username.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; Person other = (Person) obj; if (age != other.age) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (username == null) { if (other.username != null) return false; } else if (!username.equals(other.username)) return false; return true; } @Override public String toString() { return this.username + "-->" + this.password + "-->" + this.age; } }