展开全部
您好,提问者:
1、Java有专门一个线程负责垃圾回收机制。
2、当一个对象为null时垃圾回收线程会不e69da5e887aa3231313335323631343130323136353331333335326132定期的进行回收。
3、我们可以在程序中使用System.gc来通知JVM进行回收,如下代码:public class Gc_ThreadDemo extends Thread{
//重写父类的垃圾回收方法
public void run(){
for(int i = 0; i
System.out.println(Thread.currentThread().getName());
}
}
//默认extends Object
@Override
public void finalize(){
System.out.println(Thread.currentThread().getName()+"....垃圾回收方法运行");
}
//主方法
public static void main(String[] args){
Gc_ThreadDemo gc = new Gc_ThreadDemo();
for(int i = 0; i
new Gc_ThreadDemo();//制造匿名对象,好让JVM执行垃圾回收方法。
System.gc();//通知JVM执行垃圾回收
}
gc.start();
System.out.println(Thread.currentThread().getName());
}
}
//我们正常编译:javac -d classes Gc_ThreadDemo.java
//如果要看到JVM线程回收情况,我们可以使用:java -verbose:gc -cp classes Gc_ThreadDemo