use of java.lang.ref.ReferenceQueue in project jdk8u_jdk by JetBrains.
the class JpegWriterLeakTest method main.
public static void main(String[] args) {
final ReferenceQueue<ImageWriter> queue = new ReferenceQueue<>();
final ArrayList<Reference<? extends ImageWriter>> refs = new ArrayList<>();
int count = 2;
do {
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
final WeakReference<? extends ImageWriter> ref = new WeakReference<>(writer, queue);
refs.add(ref);
try {
final ImageOutputStream os = ImageIO.createImageOutputStream(new ByteArrayOutputStream());
writer.setOutput(os);
writer.write(getImage());
// NB: dispose() or reset() workarounds the problem.
} catch (IOException e) {
} finally {
writer = null;
}
count--;
} while (count > 0);
System.out.println("Wait for GC...");
final long testTimeOut = 60000L;
final long startTime = System.currentTimeMillis();
while (!refs.isEmpty()) {
// check for the test timeout
final long now = System.currentTimeMillis();
if (now - startTime > testTimeOut) {
System.out.println();
throw new RuntimeException("Test FAILED.");
}
System.gc();
try {
System.out.print(".");
Thread.sleep(1000);
} catch (InterruptedException e) {
}
;
Reference<? extends ImageWriter> r = queue.poll();
if (r != null) {
System.out.println("Got reference: " + r);
refs.remove(r);
}
}
System.out.println("Test PASSED.");
}
use of java.lang.ref.ReferenceQueue in project jackrabbit by apache.
the class SessionGarbageCollectedTest method testSessionsGetGarbageCollected.
public void testSessionsGetGarbageCollected() throws RepositoryException {
ArrayList<WeakReference<Session>> list = new ArrayList<WeakReference<Session>>();
ReferenceQueue<Session> detect = new ReferenceQueue<Session>();
Error error = null;
try {
for (int i = 0; ; i++) {
Session s = getHelper().getReadWriteSession();
// eat a lot of memory so it gets garbage collected quickly
// (or quickly runs out of memory)
Node n = s.getRootNode().addNode("n" + i);
n.setProperty("x", new String(new char[1000000]));
list.add(new WeakReference<Session>(s, detect));
if (detect.poll() != null) {
break;
}
}
} catch (OutOfMemoryError e) {
error = e;
}
for (int i = 0; i < list.size(); i++) {
Reference<Session> ref = list.get(i);
Session s = ref.get();
if (s != null) {
s.logout();
}
}
if (error != null) {
throw error;
}
}
use of java.lang.ref.ReferenceQueue in project robovm by robovm.
the class ReferenceTest method test_isEnqueued.
/**
* java.lang.ref.Reference#isEnqueued()
*/
public void test_isEnqueued() {
ReferenceQueue rq = new ReferenceQueue();
obj = new Object();
Reference ref = new SoftReference(obj, rq);
assertTrue("Should start off not enqueued.", !ref.isEnqueued());
ref.enqueue();
assertTrue("Should now be enqueued.", ref.isEnqueued());
ref.enqueue();
assertTrue("Should still be enqueued.", ref.isEnqueued());
rq.poll();
// This fails ...
assertTrue("Should now be not enqueued.", !ref.isEnqueued());
}
use of java.lang.ref.ReferenceQueue in project robovm by robovm.
the class ReferenceTest method test_get_WeakReference.
public void test_get_WeakReference() throws Exception {
// Test the general/overall functionality of Reference.
ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
r = newWeakReference(queue);
FinalizationTester.induceFinalization();
// RoboVM note: Don't wait indefinitely.
Reference ref = queue.remove(10 * 1000);
assertNotNull("Object not enqueued.", ref);
assertSame("Unexpected ref1", ref, r);
assertNull("Object could not be reclaimed1.", r.get());
r = newWeakReference(queue);
FinalizationTester.induceFinalization();
// wait for the reference queue thread to enqueue the newly-finalized object
Thread.yield();
Thread.sleep(200);
ref = queue.poll();
assertNotNull("Object not enqueued.", ref);
assertSame("Unexpected ref2", ref, r);
assertNull("Object could not be reclaimed.", ref.get());
assertNull("Object could not be reclaimed.", r.get());
}
use of java.lang.ref.ReferenceQueue in project robovm by robovm.
the class SoftReferenceTest method test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue.
/**
* java.lang.ref.SoftReference#SoftReference(java.lang.Object,
* java.lang.ref.ReferenceQueue)
*/
public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() {
ReferenceQueue rq = new ReferenceQueue();
bool = new Boolean(true);
try {
SoftReference sr = new SoftReference(bool, rq);
assertTrue("Initialization failed.", ((Boolean) sr.get()).booleanValue());
} catch (Exception e) {
fail("Exception during test : " + e.getMessage());
}
boolean exception = false;
try {
new SoftReference(bool, null);
} catch (NullPointerException e) {
exception = true;
}
assertTrue("Should not throw NullPointerException", !exception);
}
Aggregations