1. 程式人生 > >Java弱引用(WeakReference)的理解與使用

Java弱引用(WeakReference)的理解與使用

ref.WeakReference; /** * @author wison */ public class TestWeakReference { public static void main(String[] args) { Car car = new Car(22000,"silver"); WeakReference<Car> weakCar = new WeakReference<Car>(car); int i=0; while(true){ System.out.println("here is the strong reference 'car' "
+car); if(weakCar.get()!=null){ i++; System.out.println("Object is alive for "+i+" loops - "+weakCar); }else{ System.out.println("Object has been collected."); break
; } } } }