Search in sources :

Example 11 with SoftReference

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");
}
Also used : SoftReference(java.lang.ref.SoftReference)

Example 12 with SoftReference

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;
}
Also used : SoftReference(java.lang.ref.SoftReference) Document(com.intellij.openapi.editor.Document)

Example 13 with SoftReference

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;
}
Also used : SoftReference(java.lang.ref.SoftReference)

Example 14 with SoftReference

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());
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) SoftReference(java.lang.ref.SoftReference) PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference)

Example 15 with SoftReference

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);
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) SoftReference(java.lang.ref.SoftReference)

Aggregations

SoftReference (java.lang.ref.SoftReference)56 ReferenceQueue (java.lang.ref.ReferenceQueue)7 Reference (java.lang.ref.Reference)6 WeakReference (java.lang.ref.WeakReference)6 PhantomReference (java.lang.ref.PhantomReference)5 Random (java.util.Random)5 IOException (java.io.IOException)4 Bitmap (android.graphics.Bitmap)3 Context (android.content.Context)2 BitmapFactory (android.graphics.BitmapFactory)2 Handler (android.os.Handler)2 Message (android.os.Message)2 MetaClass (groovy.lang.MetaClass)2 Shape (java.awt.Shape)2 File (java.io.File)2 InputStream (java.io.InputStream)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 PrivilegedAction (java.security.PrivilegedAction)2 PrivilegedActionException (java.security.PrivilegedActionException)2 SecureClassLoader (java.security.SecureClassLoader)2