Skip to content

Instantly share code, notes, and snippets.

@vikasverma787
Last active January 18, 2018 16:56
Show Gist options
  • Save vikasverma787/5e0d7a876bf7fe31f47cad97d5f41c30 to your computer and use it in GitHub Desktop.
Save vikasverma787/5e0d7a876bf7fe31f47cad97d5f41c30 to your computer and use it in GitHub Desktop.
Objects eligible for Garbage collector
1. Isolation:
class A{
A r;
A(int i){
//something
}
}
A a1 = new A(100);
a1.r = new A(101);
a1.r.r = new A(102);
a1.r.r.r = a1;
a1 = null //all ojects are eligible to garbage collector
2. A a = new A(100);
a =new A(200); //new A(100) eligible for GC
-------------------------------------------ex weak reference and weakhashmap-------------
package com.gc.refrences;
import static com.jayway.awaitility.Awaitility.await;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;
class Employee {
private String name;
private int id;
public Employee(String name, int id) {
super();
this.name = name;
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.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;
Employee other = (Employee) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public class WeakRefrencesDemo {
public static void main(String[] args) {
HashMap<Employee, String> hm = new HashMap<>();
Employee e1 = new Employee("vikas", 1);
Employee e2 = new Employee("Ram", 2);
hm.put(e1, "vikas");
hm.put(e2, "Ram");
e1 = null;
System.gc();
// try {
// await().atMost(10, TimeUnit.SECONDS).until(hm::isEmpty);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
for (Employee key : hm.keySet()) {
System.out.println(key.getName());
}
System.out.println("-----------");
WeakHashMap<Employee, WeakReference<Employee>> whm = new WeakHashMap();
Employee e11 = new Employee("vikas", 1);
Employee e22 = new Employee("Ram", 2);
WeakReference<Employee> reference = new WeakReference(e11);
whm.put(e11,reference);
whm.put(e22, new WeakReference(e22));
System.out.println(reference.get().getName() +":::");
e22.setName("khk");// by changing this weak refrence will become null
e11 = null;
System.out.println(reference.get().getName() +":::::");
//System.gc();
System.out.println(reference.get().getName() +":::>::");
// try {
// await().atMost(10, TimeUnit.SECONDS).until(whm::isEmpty);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
for (Employee key : whm.keySet()) {
System.out.println(key.getName() +"**");
System.out.println(whm.get(key).get().getName());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment