您的位置:首页 > 其它

System类-gc()

2017-01-07 16:12 148 查看

一 gc()

运行垃圾回收器。

二 代码

1.构造一个Person类(重写finalize)
2.实例化一个Person类
3.回收Person类
/**
* Created by hanshan on 2017/1/7 0007.
*/
public class gcDemo {
public static void main(String[] args){
//实例化Person类
Person p=new Person("小明",14);
//回收Person
p=null;
System.gc();

}
}


package gcDemo;

/**
* Created by hanshan on 2017/1/7 0007.
Person类
*/
public class Person {
String name;
int age;

public Person(String name) {
this.name = name;
}

public Person(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
protected void finalize() throws Throwable {
super.finalize();
System.out.println(this.name+"被销毁");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: