zoukankan      html  css  js  c++  java
  • ReferenceQueue

       @Test  
       public void strongReference() {   
          Object referent = new Object();   
           
          /**  
           * 通过赋值创建 StrongReference   
           */  
          Object strongReference = referent;   
           
          assertSame(referent, strongReference);   
           
          referent = null;   
          System.gc();   
           
          /**  
           * StrongReference 在 GC 后不会被回收  
           */  
          assertNotNull(strongReference);   
        }   
        
        @Test  
        public void weakReference() {   
            Object referent = new Object();   
            WeakReference<Object> weakRerference = new WeakReference<Object>(referent);   
             
            assertSame(referent, weakRerference.get());   
               
            referent = null;   
            System.gc();   
               
            /**  
    * 参考
    * http://blog.csdn.net/matrix_xu/article/details/8424038 * 一旦没有指向 referent 的强引用, weak reference 在 GC 后会被自动回收
    */ assertNull(weakRerference.get()); } @Test public void weakHashMap() throws InterruptedException { Map<Object, Object> weakHashMap = new WeakHashMap<Object, Object>(); Object key = new Object(); Object value = new Object(); weakHashMap.put(key, value); assertTrue(weakHashMap.containsValue(value)); key = null; System.gc(); /** * 等待无效 entries 进入 ReferenceQueue 以便下一次调用 getTable 时被清理 */ Thread.sleep(1000); /** * 一旦没有指向 key 的强引用, WeakHashMap 在 GC 后将自动删除相关的 entry */ assertFalse(weakHashMap.containsValue(value)); } @Test public void softReference() { Object referent = new Object(); SoftReference<Object> softRerference = new SoftReference<Object>(referent); /* * Returns this reference object's referent. If this reference object has been cleared, either by the program or by the garbage collector, then this method returns null. */ assertNotNull(softRerference.get()); referent = null; System.gc(); /** *soft references 只有在 jvm OutOfMemory 之前才会被回收, 所以它非常适合缓存应用 */ assertNotNull(softRerference.get()); } @Test public void phantomReferenceAlwaysNull() { Object referent = new Object(); PhantomReference<Object> phantomReference = new PhantomReference<Object>(referent, new ReferenceQueue<Object>()); /** * phantom reference 的 get 方法永远返回 null */ assertNull(phantomReference.get()); } /* 将一个 ReferenceQueue 传给一个 Reference的构造函数, 当对象被回收时, 虚拟机会自动将这个对象插入到 ReferenceQueue中。 WeakHashMap 就是利用 ReferenceQueue 来清除 key 已经没有强引用的 entries。 */ @Test public void referenceQueue() throws InterruptedException { Object referent = new Object(); ReferenceQueue<Object> referenceQueue = new ReferenceQueue<Object>(); WeakReference<Object> weakReference = new WeakReference<Object>(referent, referenceQueue); assertFalse(weakReference.isEnqueued()); Reference<? extends Object> polled = referenceQueue.poll(); assertNull(polled); referent = null; System.gc(); /* * 一旦没有指向 referent 的强引用, weak reference 在 GC 后会被回收 当对象被回收时, 虚拟机会自动将这个对象插入到 ReferenceQueue */ assertTrue(weakReference.isEnqueued()); Reference<? extends Object> removed = referenceQueue.remove(); assertNotNull(removed); }
  • 相关阅读:
    Docker系列
    Eclipse 安装TestNG插件,结合Maven使用
    HttpClient设置忽略SSL,实现HTTPS访问, 解决Certificates does not conform to algorithm constraints
    Jenkins Html Rport 使用frame报错解决办法
    Zend Framework1 框架入门(针对Windows,包含安装配置与数据库增删改查)
    Windows下Nginx配置SSL实现Https访问(包含证书生成)
    Windows下Nginx Virtual Host多站点配置详解
    幽灵般的存在:零宽空白
    我的公司培训讲义(2):设计模式思想精要教程
    突如其来而又必然的离职
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3573921.html
Copyright © 2011-2022 走看看