System类-gc()

来源:互联网 发布:笑话知乎 编辑:程序博客网 时间:2024/05/21 18:38

一 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+"被销毁");    }}

0 0