use of java.lang.ref.SoftReference in project jdk8u_jdk by JetBrains.
the class Pin method main.
public static void main(String[] args) throws Exception {
SoftReference[] blocks = new SoftReference[NUM_BLOCKS];
byte[] block;
System.err.println("Filling array with " + NUM_BLOCKS + " SoftReferences to blocks of " + BLOCK_SIZE + " bytes.");
for (int i = 0; i < NUM_BLOCKS; ++i) {
block = new byte[BLOCK_SIZE];
SoftReference ref = new SoftReference(block);
blocks[i] = ref;
}
block = null;
System.err.println("Allowing SoftReferences to be enqueued.");
System.gc();
Thread.sleep(1000);
/* -- Commenting out the following section will hide the bug -- */
System.err.println("Invoking get() on SoftReferences.");
for (int i = 0; i < NUM_BLOCKS; ++i) {
block = (byte[]) blocks[i].get();
}
block = null;
/* -- end -- */
System.err.println("Forcing desperate garbage collection...");
java.util.Vector chain = new java.util.Vector();
try {
while (true) {
System.gc();
int[] hungry = new int[65536];
chain.addElement(hungry);
// yield, for what it's worth
Thread.sleep(100);
}
} catch (OutOfMemoryError e) {
System.err.println("Got OutOfMemoryError, as expected.");
}
int emptyCount = 0, fullCount = 0;
System.err.print("Examining contents of array:");
for (int i = 0; i < NUM_BLOCKS; ++i) {
block = (byte[]) blocks[i].get();
if (block == null) {
emptyCount++;
} else {
fullCount++;
}
}
System.err.println(" " + emptyCount + " empty, " + fullCount + " full.");
if (emptyCount == 0)
throw new Exception("No SoftReference instances were cleared");
}
use of java.lang.ref.SoftReference in project intellij-community by JetBrains.
the class SingleRootFileViewProvider method getDocument.
@Override
public Document getDocument() {
Document document = com.intellij.reference.SoftReference.dereference(myDocument);
if (document == null) /* TODO[ik] make this change && isEventSystemEnabled()*/
{
document = FileDocumentManager.getInstance().getDocument(getVirtualFile());
myDocument = document == null ? null : new SoftReference<>(document);
}
return document;
}
use of java.lang.ref.SoftReference in project clochure by videlalvaro.
the class DynamicClassLoader method defineClass.
public Class defineClass(String name, byte[] bytes, Object srcForm) {
Util.clearCache(rq, classCache);
Class c = defineClass(name, bytes, 0, bytes.length);
classCache.put(name, new SoftReference(c, rq));
return c;
}
use of java.lang.ref.SoftReference 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.SoftReference 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