强引用:


软引用:


		User user = new User();
		SoftReference<Object> softReference  = new SoftReference<Object>(user);
		softReference.get();
登录后复制

弱引用


                User user = new User();
		WeakReference<User> weakReference = new WeakReference<User>(user);
		weakReference.get();
		
		ReferenceQueue<User> referenceQueue = new ReferenceQueue<User>();
		WeakReference<User> weakReference2 = new WeakReference<User>(user, referenceQueue);
		//当引用对象被标识为可回收时  返回true,  即当user对象标识为可回收时,返回true
		weakReference.isEnqueued();
登录后复制

虚引用


		User user = new User();
		ReferenceQueue<User> referenceQueue = new ReferenceQueue<User>();
		PhantomReference<User>  phantomReference = new PhantomReference<User>(user, referenceQueue);
		//即当user对象标识为可回收时,返回true
		System.out.println(phantomReference.isEnqueued());
		//永远返回null
		System.out.println(phantomReference.get());
登录后复制

其他相关类:


		WeakCache weakCache  = new WeakCache();
		SoftCache softCache = new SoftCache();
		WeakHashMap weakHashMap  = new WeakHashMap();
登录后复制


以上就是Java虚拟机学习 - 对象引用强度的内容,更多相关内容请关注Work网(www.php.cn)!


09-14 16:31