/** Constructs a new finalizer thread. */ private Finalizer( Class<?> finalizableReferenceClass, ReferenceQueue<Object> queue, PhantomReference<Object> frqReference) { this.queue = queue; this.finalizableReferenceClassReference = new WeakReference<Class<?>>(finalizableReferenceClass); // Keep track of the FRQ that started us so we know when to stop. this.frqReference = frqReference; }
public CleanupMapTask(MapMakerInternalMap<?, ?, ?, ?> map) { this.mapReference = new WeakReference<MapMakerInternalMap<?, ?, ?, ?>>(map); }
protected T wrapCachedClass(Class klass) { return (T) new WeakReference(klass); }
/** * Specify an id for serialization purposes, allowing this BeanFactory to be * deserialized from this id back into the BeanFactory object, if needed. */ public void setSerializationId(@Nullable String serializationId) { if (serializationId != null) { serializableFactories.put(serializationId, new WeakReference<>(this)); } else if (this.serializationId != null) { serializableFactories.remove(this.serializationId); } this.serializationId = serializationId; }
@GwtIncompatible // java.lang.ref.WeakReference private static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> populateCache( Class<T> enumClass) { Map<String, WeakReference<? extends Enum<?>>> result = new HashMap<>(); for (T enumInstance : EnumSet.allOf(enumClass)) { result.put(enumInstance.name(), new WeakReference<Enum<?>>(enumInstance)); } enumConstantCache.put(enumClass, result); return result; }
public ClassLoaderData(ClassLoader classLoader) { if (classLoader == null) { throw new IllegalArgumentException("classLoader == null is not yet supported"); } this.classLoader = new WeakReference<ClassLoader>(classLoader); Function<AbstractClassGenerator, Object> load = new Function<AbstractClassGenerator, Object>() { public Object apply(AbstractClassGenerator gen) { Class klass = gen.generate(ClassLoaderData.this); return gen.wrapCachedClass(klass); } }; generatedClasses = new LoadingCache<AbstractClassGenerator, Object, Object>(GET_KEY, load); }
@Override public synchronized String toString() { String s; if (toString != null) { s = toString.get(); if (s != null) { return s; } } s = Integer.toString(value); toString = new WeakReference<>(s); return s; } }
public void testAwaitClear() { final WeakReference<Object> ref = new WeakReference<>(new Object()); GcFinalization.awaitClear(ref); assertNull(ref.get()); }
public void testStrongImplementations() { for (Striped<?> striped : strongImplementations()) { WeakReference<Object> weakRef = new WeakReference<>(striped.get(new Object())); WeakReference<Object> garbage = new WeakReference<>(new Object()); GcFinalization.awaitClear(garbage); assertNotNull(weakRef.get()); } }
@GwtIncompatible // weak references public void testFreesNextReference() { Iterator<Object> itr = new AbstractIterator<Object>() { @Override public Object computeNext() { return new Object(); } }; WeakReference<Object> ref = new WeakReference<>(itr.next()); GcFinalization.awaitClear(ref); }
@GwtIncompatible // weak references public void testFreesNextReference() { Iterator<Object> itr = new AbstractIterator<Object>() { @Override public Object computeNext() { return new Object(); } }; WeakReference<Object> ref = new WeakReference<>(itr.next()); GcFinalization.awaitClear(ref); }
public void testWeakReadWrite() { Striped<ReadWriteLock> striped = Striped.lazyWeakReadWriteLock(1000); Object key = new Object(); Lock readLock = striped.get(key).readLock(); WeakReference<Object> garbage = new WeakReference<>(new Object()); GcFinalization.awaitClear(garbage); Lock writeLock = striped.get(key).writeLock(); readLock.lock(); assertFalse(writeLock.tryLock()); readLock.unlock(); }
public void testWeakImplementations() { for (Striped<?> striped : weakImplementations()) { WeakReference<Object> weakRef = new WeakReference<>(striped.get(new Object())); GcFinalization.awaitClear(weakRef); } }
@Test public void justUnsubscribed() throws Exception { o = new Object(); WeakReference<Object> wr = new WeakReference<Object>(o); TestObserver<Long> to = Observable.just(o).count().toObservable().onTerminateDetach().test(); o = null; to.cancel(); System.gc(); Thread.sleep(200); Assert.assertNull("Object retained!", wr.get()); }
@Test public void just() throws Exception { o = new Object(); WeakReference<Object> wr = new WeakReference<Object>(o); TestObserver<Object> to = new TestObserver<Object>(); Observable.just(o).count().toObservable().onTerminateDetach().subscribe(to); to.assertValue(1L); to.assertComplete(); to.assertNoErrors(); o = null; System.gc(); Thread.sleep(200); Assert.assertNull("Object retained!", wr.get()); }
@Test public void justUnsubscribed() throws Exception { o = new Object(); WeakReference<Object> wr = new WeakReference<Object>(o); TestSubscriber<Object> ts = new TestSubscriber<Object>(0); Flowable.just(o).count().toFlowable().onTerminateDetach().subscribe(ts); ts.cancel(); o = null; System.gc(); Thread.sleep(200); Assert.assertNull("Object retained!", wr.get()); }
public void testWeak_afterGC() throws InterruptedException { Integer canonical = new Integer(5); Integer not = new Integer(5); Interner<Integer> pool = Interners.newWeakInterner(); assertSame(canonical, pool.intern(canonical)); WeakReference<Integer> signal = new WeakReference<>(canonical); canonical = null; // Hint to the JIT that canonical is unreachable GcFinalization.awaitClear(signal); assertSame(not, pool.intern(not)); }
@Test @Ignore("Intended for use during development only") public void shouldBeFasterThanSynchronizedMap() throws InterruptedException { Map<Integer, WeakReference<String>> synchronizedMap = Collections.synchronizedMap(new WeakHashMap<Integer, WeakReference<String>>()); StopWatch mapTime = timeMultiThreaded("SynchronizedMap", synchronizedMap, v -> new WeakReference<>(String.valueOf(v))); System.out.println(mapTime.prettyPrint()); this.map.setDisableTestHooks(true); StopWatch cacheTime = timeMultiThreaded("WeakConcurrentCache", this.map, String::valueOf); System.out.println(cacheTime.prettyPrint()); // We should be at least 4 time faster assertThat(cacheTime.getTotalTimeSeconds(), is(lessThan(mapTime.getTotalTimeSeconds() / 4.0))); }
@Test public void just() throws Exception { o = new Object(); WeakReference<Object> wr = new WeakReference<Object>(o); TestSubscriber<Object> ts = new TestSubscriber<Object>(); Flowable.just(o).count().toFlowable().onTerminateDetach().subscribe(ts); ts.assertValue(1L); ts.assertComplete(); ts.assertNoErrors(); o = null; System.gc(); Thread.sleep(200); Assert.assertNull("Object retained!", wr.get()); }
@Test public void backpressured() throws Exception { o = new Object(); WeakReference<Object> wr = new WeakReference<Object>(o); TestSubscriber<Object> ts = new TestSubscriber<Object>(0L); Flowable.just(o).count().toFlowable().onTerminateDetach().subscribe(ts); ts.assertNoValues(); ts.request(1); ts.assertValue(1L); ts.assertComplete(); ts.assertNoErrors(); o = null; System.gc(); Thread.sleep(200); Assert.assertNull("Object retained!", wr.get()); }