1. 程式人生 > >手動清理垃圾測試

手動清理垃圾測試

color pre () ring ide extends end main stub

手動清理垃圾測試

 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

手動清理垃圾測試