Java Code Examples for java.lang.ref.Reference#clear()

The following examples show how to use java.lang.ref.Reference#clear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FinalizableReferenceQueue.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Repeatedly dequeues references from the queue and invokes
 * {@link FinalizableReference#finalizeReferent()} on them until the queue is empty. This method
 * is a no-op if the background thread was created successfully.
 */

void cleanUp() {
  if (threadStarted) {
    return;
  }
  Reference<?> reference;
  while ((reference = queue.poll()) != null) {
    /*
     * This is for the benefit of phantom references. Weak and soft references will have already
     * been cleared by this point.
     */
    reference.clear();
    try {
      ((FinalizableReference) reference).finalizeReferent();
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }
  }
}
 
Example 2
Source File: FinalizableReferenceQueue.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Repeatedly dequeues references from the queue and invokes
 * {@link FinalizableReference#finalizeReferent()} on them until the queue is empty. This method
 * is a no-op if the background thread was created successfully.
 */

void cleanUp() {
  if (threadStarted) {
    return;
  }
  Reference<?> reference;
  while ((reference = queue.poll()) != null) {
    /*
     * This is for the benefit of phantom references. Weak and soft references will have already
     * been cleared by this point.
     */
    reference.clear();
    try {
      ((FinalizableReference) reference).finalizeReferent();
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }
  }
}
 
Example 3
Source File: WeakArrayList.java    From hasor with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public T remove(final int index) {
    Object oldValue;
    Reference<T> ref;
    do {
        this.assertRange(index, false);
        ref = (Reference<T>) this.data[index];
        oldValue = ref.get();
    } while (oldValue == null);
    ref.clear();
    System.arraycopy(this.data, index + 1, this.data, index, this.size - index - 1);
    this.data[this.size - 1] = null;
    this.size -= 1;
    this.modCount += 1;
    return (T) WeakArrayList.unmaskNull(oldValue);
}
 
Example 4
Source File: FinalizableReferenceQueue.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Repeatedly dequeues references from the queue and invokes
 * {@link FinalizableReference#finalizeReferent()} on them until the queue is empty. This method
 * is a no-op if the background thread was created successfully.
 */

void cleanUp() {
  if (threadStarted) {
    return;
  }
  Reference<?> reference;
  while ((reference = queue.poll()) != null) {
    /*
     * This is for the benefit of phantom references. Weak and soft references will have already
     * been cleared by this point.
     */
    reference.clear();
    try {
      ((FinalizableReference) reference).finalizeReferent();
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }
  }
}
 
Example 5
Source File: FinalizableReferenceQueue.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
* Repeatedly dequeues references from the queue and invokes
* {@link FinalizableReference#finalizeReferent()} on them until the queue is empty. This method
* is a no-op if the background thread was created successfully.
*/


 void cleanUp() {
 if (threadStarted) {
     return;
 }
 Reference<?> reference;
 while ((reference = queue.poll()) != null) {
   /*
    * This is for the benefit of phantom references. Weak and soft references will have already
    * been cleared by this point.
    */
     reference.clear();
     try {
     ((FinalizableReference) reference).finalizeReferent();
     } catch (Throwable t) {
       logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
     }
 }
 }
 
Example 6
Source File: FinalizableReferenceQueue.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Repeatedly dequeues references from the queue and invokes
 * {@link FinalizableReference#finalizeReferent()} on them until the queue is empty. This method
 * is a no-op if the background thread was created successfully.
 */

void cleanUp() {
  if (threadStarted) {
    return;
  }
  Reference<?> reference;
  while ((reference = queue.poll()) != null) {
    /*
     * This is for the benefit of phantom references. Weak and soft references will have already
     * been cleared by this point.
     */
    reference.clear();
    try {
      ((FinalizableReference) reference).finalizeReferent();
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }
  }
}
 
Example 7
Source File: FinalizableReferenceQueue.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Repeatedly dequeues references from the queue and invokes
 * {@link FinalizableReference#finalizeReferent()} on them until the queue is empty. This method
 * is a no-op if the background thread was created successfully.
 */
void cleanUp() {
  if (threadStarted) {
    return;
  }

  Reference<?> reference;
  while ((reference = queue.poll()) != null) {
    /*
     * This is for the benefit of phantom references. Weak and soft references will have already
     * been cleared by this point.
     */
    reference.clear();
    try {
      ((FinalizableReference) reference).finalizeReferent();
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }
  }
}
 
Example 8
Source File: WeakValueHashMap.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public Object put(Object key, Object value) {
diddleReferenceQueue();
Reference oldRef = (Reference) keyToRefMap.put(key, new KeyedWeakReference(key, value, queue));
if (oldRef != null) {
	Object oldRefVal = oldRef.get();
	oldRef.clear();
	return oldRefVal;
} else {
	return null;
}
}
 
Example 9
Source File: ConstantPoolInfoGC.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/********** test core **********/

    protected void runTests() throws Exception {
        targetClass = startToMain("ConstantPoolGCTarg").location().declaringType();

        if (vm().canGetConstantPool()) {
            byte[] cpbytes = targetClass.constantPool();

            // imitate SoftReference cleared
            Field constantPoolBytesRef = ReferenceTypeImpl.class.getDeclaredField("constantPoolBytesRef");
            constantPoolBytesRef.setAccessible(true);
            Reference softRef = (Reference) constantPoolBytesRef.get(targetClass);
            softRef.clear();

            byte[] cpbytes2 = targetClass.constantPool();
            if (!Arrays.equals(cpbytes, cpbytes2)) {
                failure("Consequent constantPool results vary, first was : " + cpbytes + ", now: " + cpbytes2);
            };

        } else {
            System.out.println("can get constant pool version not supported");
        }


        /*
         * resume until end
         */
        listenUntilVMDisconnect();

        /*
         * deal with results of test
         * if anything has called failure("foo") testFailed will be true
         */
        if (!testFailed) {
            println("ConstantPoolInfoGC: passed");
        } else {
            throw new Exception("ConstantPoolInfoGC: failed");
        }
    }
 
Example 10
Source File: Finalizer.java    From reflectutils with Apache License 2.0 5 votes vote down vote up
/**
 * Cleans up a single reference. Catches and logs all throwables.
 */
private void cleanUp(Reference<?> reference) throws ShutDown {
  Method finalizeReferentMethod = getFinalizeReferentMethod();
  do {
    /*
     * This is for the benefit of phantom references. Weak and soft
     * references will have already been cleared by this point.
     */
    reference.clear();

    if (reference == frqReference) {
      /*
       * The client no longer has a reference to the
       * FinalizableReferenceQueue. We can stop.
       */
      throw new ShutDown();
    }

    try {
      finalizeReferentMethod.invoke(reference);
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }

    /*
     * Loop as long as we have references available so as not to waste
     * CPU looking up the Method over and over again.
     */
  } while ((reference = queue.poll()) != null);
}
 
Example 11
Source File: WeakArrayList.java    From hasor with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public T set(final int index, final T element) {
    Object oldValue;
    Reference<T> ref;
    do {
        this.assertRange(index, false);
        ref = (Reference<T>) this.data[index];
        oldValue = ref.get();
    } while (oldValue == null);
    ref.clear();
    this.data[index] = this.createRef(element);
    this.modCount += 1;
    return (T) WeakArrayList.unmaskNull(oldValue);
}
 
Example 12
Source File: WeakArrayList.java    From hasor with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public int expurge() {
    int j;
    while (this.queue.poll() != null) {
        this.enquedElement = true;
    }
    if (this.enquedElement) {
        j = 0;
        for (int i = 0; i < this.size; ++i) {
            Reference<T> ref = (Reference<T>) this.data[i];
            if (ref == null || ref.isEnqueued() || ref.get() == null) {
                if (ref != null) {
                    ref.clear();
                }
                this.data[i] = null;
            } else {
                if (i != j) {
                    this.data[j] = this.data[i];
                    this.data[i] = null;
                }
                ++j;
            }
        }
        this.enquedElement = false;
    } else {
        j = this.size;
    }
    while (this.queue.poll() != null) {
        this.enquedElement = true;
    }
    this.size = j;
    return this.size;
}
 
Example 13
Source File: ReferenceCache.java    From beanshell with Apache License 2.0 5 votes vote down vote up
/** Uses the parent's remove method which is blocking until a cleared
 * reference is added to the queue.
 * Calls the overwritten clear method of the associated reference which
 * will remove the key from the cache.
 *  {@inheritDoc} */
@Override
public void run() {
    for (;;) try {
        Reference<? extends T> ref = super.remove();
        if (ref != null) ref.clear();
    } catch (InterruptedException e) { /* ignore try again */ System.out.println(e+" ooops");}
}
 
Example 14
Source File: Finalizer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Cleans up a single reference. Catches and logs all throwables.
 *
 * @return true if the caller should continue, false if the associated FinalizableReferenceQueue
 *     is no longer referenced.
 */
private boolean cleanUp(Reference<?> reference) {
  Method finalizeReferentMethod = getFinalizeReferentMethod();
  if (finalizeReferentMethod == null) {
    return false;
  }
  do {
    /*
     * This is for the benefit of phantom references. Weak and soft references will have already
     * been cleared by this point.
     */
    reference.clear();

    if (reference == frqReference) {
      /*
       * The client no longer has a reference to the FinalizableReferenceQueue. We can stop.
       */
      return false;
    }

    try {
      finalizeReferentMethod.invoke(reference);
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }

    /*
     * Loop as long as we have references available so as not to waste CPU looking up the Method
     * over and over again.
     */
  } while ((reference = queue.poll()) != null);
  return true;
}
 
Example 15
Source File: Finalizer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Cleans up a single reference. Catches and logs all throwables.
 *
 * @return true if the caller should continue, false if the associated FinalizableReferenceQueue
 *     is no longer referenced.
 */

private boolean cleanUp(Reference<?> reference) {
  Method finalizeReferentMethod = getFinalizeReferentMethod();
  if (finalizeReferentMethod == null) {
    return false;
  }
  do {
    /*
     * This is for the benefit of phantom references. Weak and soft references will have already
     * been cleared by this point.
     */
    reference.clear();
    if (reference == frqReference) {
      /*
       * The client no longer has a reference to the FinalizableReferenceQueue. We can stop.
       */
      return false;
    }
    try {
      finalizeReferentMethod.invoke(reference);
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }

    /*
     * Loop as long as we have references available so as not to waste CPU looking up the Method
     * over and over again.
     */
  } while ((reference = queue.poll()) != null);
  return true;
}
 
Example 16
Source File: Cache.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Invoked in a background thread after a value has been set in the map.
 * This method computes a cost estimation of the new value. If the total cost is greater
 * than the cost limit, then oldest strong references are replaced by weak references.
 */
final void adjustReferences(final K key, final V value) {
    int cost = (value != null) ? cost(value) : 0;
    synchronized (costs) {
        final Integer old = costs.put(key, cost);
        if (old != null) {
            cost -= old;
        }
        if ((totalCost += cost) > costLimit) {
            final Iterator<Map.Entry<K,Integer>> it = costs.entrySet().iterator();
            while (it.hasNext()) {
                /*
                 * Converts the current entry from strong reference to weak/soft reference.
                 * We perform this conversion even if the entry is for the value just added
                 * to the cache, if it happen that the cost is higher than the maximal one.
                 * That entry should not be garbage collected too early anyway because the
                 * caller should still have a strong reference to the value he just created.
                 */
                final Map.Entry<K,Integer> entry = it.next();
                final K oldKey = entry.getKey();
                final Object oldValue = map.get(oldKey);
                if (oldValue != null && !isReservedType(oldValue)) {
                    @SuppressWarnings("unchecked")
                    final Reference<V> ref = soft ? new Soft<>(map, oldKey, (V) oldValue)
                                                  : new Weak<>(map, oldKey, (V) oldValue);
                    if (!map.replace(oldKey, oldValue, ref)) {
                        ref.clear();                // Prevents the reference to be enqueued.
                    }
                }
                it.remove();
                if ((totalCost -= entry.getValue()) <= costLimit) {
                    break;
                }
            }
        }
    }
}
 
Example 17
Source File: Finalizer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Cleans up a single reference. Catches and logs all throwables.
 *
 * @return true if the caller should continue, false if the associated FinalizableReferenceQueue
 *     is no longer referenced.
 */

private boolean cleanUp(Reference<?> reference) {
  Method finalizeReferentMethod = getFinalizeReferentMethod();
  if (finalizeReferentMethod == null) {
    return false;
  }
  do {
    /*
     * This is for the benefit of phantom references. Weak and soft references will have already
     * been cleared by this point.
     */
    reference.clear();
    if (reference == frqReference) {
      /*
       * The client no longer has a reference to the FinalizableReferenceQueue. We can stop.
       */
      return false;
    }
    try {
      finalizeReferentMethod.invoke(reference);
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }

    /*
     * Loop as long as we have references available so as not to waste CPU looking up the Method
     * over and over again.
     */
  } while ((reference = queue.poll()) != null);
  return true;
}
 
Example 18
Source File: Finalizer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Cleans up a single reference. Catches and logs all throwables.
 *
 * @return true if the caller should continue, false if the associated FinalizableReferenceQueue
 *     is no longer referenced.
 */

private boolean cleanUp(Reference<?> reference) {
  Method finalizeReferentMethod = getFinalizeReferentMethod();
  if (finalizeReferentMethod == null) {
    return false;
  }
  do {
    /*
     * This is for the benefit of phantom references. Weak and soft references will have already
     * been cleared by this point.
     */
    reference.clear();
    if (reference == frqReference) {
      /*
       * The client no longer has a reference to the FinalizableReferenceQueue. We can stop.
       */
      return false;
    }
    try {
      finalizeReferentMethod.invoke(reference);
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }

    /*
     * Loop as long as we have references available so as not to waste CPU looking up the Method
     * over and over again.
     */
  } while ((reference = queue.poll()) != null);
  return true;
}
 
Example 19
Source File: Finalizer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Cleans up a single reference. Catches and logs all throwables.
 *
 * @return true if the caller should continue, false if the associated FinalizableReferenceQueue
 *     is no longer referenced.
 */

private boolean cleanUp(Reference<?> reference) {
  Method finalizeReferentMethod = getFinalizeReferentMethod();
  if (finalizeReferentMethod == null) {
    return false;
  }
  do {
    /*
     * This is for the benefit of phantom references. Weak and soft references will have already
     * been cleared by this point.
     */
    reference.clear();
    if (reference == frqReference) {
      /*
       * The client no longer has a reference to the FinalizableReferenceQueue. We can stop.
       */
      return false;
    }
    try {
      finalizeReferentMethod.invoke(reference);
    } catch (Throwable t) {
      logger.log(Level.SEVERE, "Error cleaning up after reference.", t);
    }

    /*
     * Loop as long as we have references available so as not to waste CPU looking up the Method
     * over and over again.
     */
  } while ((reference = queue.poll()) != null);
  return true;
}
 
Example 20
Source File: ConstantPoolInfoGC.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/********** test core **********/

    protected void runTests() throws Exception {
        targetClass = startToMain("ConstantPoolGCTarg").location().declaringType();

        if (vm().canGetConstantPool()) {
            byte[] cpbytes = targetClass.constantPool();

            // imitate SoftReference cleared
            Field constantPoolBytesRef = ReferenceTypeImpl.class.getDeclaredField("constantPoolBytesRef");
            constantPoolBytesRef.setAccessible(true);
            Reference softRef = (Reference) constantPoolBytesRef.get(targetClass);
            softRef.clear();

            byte[] cpbytes2 = targetClass.constantPool();
            if (!Arrays.equals(cpbytes, cpbytes2)) {
                failure("Consequent constantPool results vary, first was : " + cpbytes + ", now: " + cpbytes2);
            };

        } else {
            System.out.println("can get constant pool version not supported");
        }


        /*
         * resume until end
         */
        listenUntilVMDisconnect();

        /*
         * deal with results of test
         * if anything has called failure("foo") testFailed will be true
         */
        if (!testFailed) {
            println("ConstantPoolInfoGC: passed");
        } else {
            throw new Exception("ConstantPoolInfoGC: failed");
        }
    }