天天看點

JAVA之軟,弱引用

A obj = new A(); 

    SoftRefenrence sr = new SoftReference(obj); 


     if(sr!=null){ 

        obj = sr.get(); 

    }else{ 

        obj = new A(); 

        sr = new SoftReference(obj); 

    }      
A obj = new A();  


   WeakReference wr = new WeakReference(obj);  

    obj = null;  


     //等待一段時間,obj對象就會被垃圾回收  

   


  if (wr.get()==null) {   

    


  System.out.println("obj 已經被清除了 ");   


    

  } else {   


 System.out.println("obj 尚未被清除,其資訊是 "+obj.toString());  


  }