Java基础:垃圾回收机制演示

Java垃圾回收机制gc,自动回收没有引用的对象,释放内存。

示例代码:

  1. class Finalize{
  2.     public static void main(String args[]){
  3.         int count;
  4.         FDemo ob=new FDemo(0);
  5.         for(count=1;count<10000000;count++){         
  6.             ob.generator(count);                 //创建大量对象
  7.             }
  8.         }
  9.     }
  10. class FDemo{
  11.     int x;
  12.     FDemo(int i){
  13.         x=i;
  14.         }
  15.     protected void finalize(){        //finalize()方法,在对象被回收之前调用,确保对象完全终止,
  16.         System.out.println("finalize "+x);
  17.         }    
  18.     void generator(int i){
  19.         FDemo o=new FDemo(i);
  20.         }    
  21.     }    

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/87925171