Garbage Collector Example
class StudentDemo
{
int id;
String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public StudentDemo(int id, String name) {
this.id = id;
this.name = name;
}
protected void finalize()
{
System.out.println("object is removed...");
}
}
public class TestGarbegeColllector {
public static void main(String[] args) {
StudentDemo s1 = new StudentDemo(1,"istar");
StudentDemo s2 = new StudentDemo(2,"abc");
StudentDemo s3 = new StudentDemo(3,"abcrr");
StudentDemo s4 = new StudentDemo(4,"abxc");
StudentDemo s5 = s1;
System.out.println("student infor...");
s2 = null;
s3=null;
StudentDemo s6 = new StudentDemo(5,"abxc");
System.gc();
System.out.println("after gc...");
System.out.println("after gc...");System.out.println("after gc...");System.out.println("after gc...");System.out.println("after gc...");
}
}
Comments
Post a Comment