手动清理垃圾测试
1 package com.fry.FinalizeTest; 2 3 public class FinalizeTest extends java.lang.Object{ 4 @Override 5 protected void finalize() throws Throwable { 6 // TODO Auto-generated method stub 7 super.finalize(); 8 System.out.println("The object is gone!"); 9 } 10 public static void main(String[] args) { 11 for(int i=1;i<=100;i++) { 12 new FinalizeTest(); 13 } 14 System.gc(); 15 System.out.println("The program is ending"); 16 } 17 }
当有14行的System.gc();时的运行效果:
The program is ending
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
The object is gone!
当没有14行的System.gc();时的运行效果:
The program is ending
时间: 2024-10-13 14:12:54