Search in sources :

Example 1 with SoftReference

use of java.lang.ref.SoftReference in project groovy by apache.

the class JO method getStaticMetaClass.

MetaClass getStaticMetaClass(Object obj) {
    MetaClass mc;
    if (staticMetaClass == null || (mc = (MetaClass) staticMetaClass.get()) == null) {
        mc = InvokerHelper.getMetaClass(obj);
        staticMetaClass = new SoftReference(mc);
    }
    return mc;
}
Also used : MetaClass(groovy.lang.MetaClass) SoftReference(java.lang.ref.SoftReference)

Example 2 with SoftReference

use of java.lang.ref.SoftReference in project tomcat by apache.

the class SoftReferenceObjectPool method borrowObject.

/**
     * Borrow an object from the pool. If there are no idle instances available
     * in the pool, the configured factory's
     * {@link PooledObjectFactory#makeObject()} method is invoked to create a
     * new instance.
     * <p>
     * All instances are {@link PooledObjectFactory#activateObject(
     * org.apache.tomcat.dbcp.pool2.PooledObject) activated}
     * and {@link PooledObjectFactory#validateObject(
     * org.apache.tomcat.dbcp.pool2.PooledObject)
     * validated} before being returned by this method. If validation fails or
     * an exception occurs activating or validating an idle instance, the
     * failing instance is {@link PooledObjectFactory#destroyObject(
     * org.apache.tomcat.dbcp.pool2.PooledObject)
     * destroyed} and another instance is retrieved from the pool, validated and
     * activated. This process continues until either the pool is empty or an
     * instance passes validation. If the pool is empty on activation or it does
     * not contain any valid instances, the factory's <code>makeObject</code>
     * method is used to create a new instance. If the created instance either
     * raises an exception on activation or fails validation,
     * <code>NoSuchElementException</code> is thrown. Exceptions thrown by
     * <code>MakeObject</code> are propagated to the caller; but other than
     * <code>ThreadDeath</code> or <code>VirtualMachineError</code>, exceptions
     * generated by activation, validation or destroy methods are swallowed
     * silently.
     *
     * @throws NoSuchElementException
     *             if a valid object cannot be provided
     * @throws IllegalStateException
     *             if invoked on a {@link #close() closed} pool
     * @throws Exception
     *             if an exception occurs creating a new instance
     * @return a valid, activated object instance
     */
// ref cannot be null
@SuppressWarnings("null")
@Override
public synchronized T borrowObject() throws Exception {
    assertOpen();
    T obj = null;
    boolean newlyCreated = false;
    PooledSoftReference<T> ref = null;
    while (null == obj) {
        if (idleReferences.isEmpty()) {
            if (null == factory) {
                throw new NoSuchElementException();
            }
            newlyCreated = true;
            obj = factory.makeObject().getObject();
            createCount++;
            // Do not register with the queue
            ref = new PooledSoftReference<>(new SoftReference<>(obj));
            allReferences.add(ref);
        } else {
            ref = idleReferences.pollFirst();
            obj = ref.getObject();
            // Clear the reference so it will not be queued, but replace with a
            // a new, non-registered reference so we can still track this object
            // in allReferences
            ref.getReference().clear();
            ref.setReference(new SoftReference<>(obj));
        }
        if (null != factory && null != obj) {
            try {
                factory.activateObject(ref);
                if (!factory.validateObject(ref)) {
                    throw new Exception("ValidateObject failed");
                }
            } catch (final Throwable t) {
                PoolUtils.checkRethrow(t);
                try {
                    destroy(ref);
                } catch (final Throwable t2) {
                    PoolUtils.checkRethrow(t2);
                // Swallowed
                } finally {
                    obj = null;
                }
                if (newlyCreated) {
                    throw new NoSuchElementException("Could not create a validated object, cause: " + t.getMessage());
                }
            }
        }
    }
    numActive++;
    ref.allocate();
    return obj;
}
Also used : SoftReference(java.lang.ref.SoftReference) NoSuchElementException(java.util.NoSuchElementException) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with SoftReference

use of java.lang.ref.SoftReference in project joda-time-android by dlew.

the class ResourceZoneInfoProvider method getZone.

//-----------------------------------------------------------------------
/**
     * If an error is thrown while loading zone data, the exception is logged
     * to system error and null is returned for this and all future requests.
     *
     * @param id  the id to load
     * @return the loaded zone
     */
public DateTimeZone getZone(String id) {
    if (id == null) {
        return null;
    }
    Object obj = iZoneInfoMap.get(id);
    if (obj == null) {
        return null;
    }
    if (id.equals(obj)) {
        // Load zone data for the first time.
        return loadZoneData(id);
    }
    if (obj instanceof SoftReference<?>) {
        @SuppressWarnings("unchecked") SoftReference<DateTimeZone> ref = (SoftReference<DateTimeZone>) obj;
        DateTimeZone tz = ref.get();
        if (tz != null) {
            return tz;
        }
        // Reference cleared; load data again.
        return loadZoneData(id);
    }
    // If this point is reached, mapping must link to another.
    return getZone((String) obj);
}
Also used : SoftReference(java.lang.ref.SoftReference) DateTimeZone(org.joda.time.DateTimeZone)

Example 4 with SoftReference

use of java.lang.ref.SoftReference in project clojure by clojure.

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 5 with SoftReference

use of java.lang.ref.SoftReference in project android_frameworks_base by ParanoidAndroid.

the class HeapTest method xxtestSoftRefPartialClean.

public void xxtestSoftRefPartialClean() throws Exception {
    final int NUM_REFS = 128;
    Object[] objects = new Object[NUM_REFS];
    SoftReference<Object>[] refs = new SoftReference[objects.length];
    /* Create a bunch of objects and a parallel array
        * of SoftReferences.
        */
    makeRefs(objects, refs);
    Runtime.getRuntime().gc();
    /* Let go of the hard references to the objects so that
        * the references can be cleared.
        */
    clearRefs(objects);
    /* Start creating a bunch of temporary and permanent objects
        * to drive GC.
        */
    final int NUM_OBJECTS = 64 * 1024;
    Object[] junk = new Object[NUM_OBJECTS];
    Random random = new Random();
    int i = 0;
    int mod = 0;
    int totalSize = 0;
    int cleared = -1;
    while (i < junk.length && totalSize < 8 * 1024 * 1024) {
        int r = random.nextInt(64 * 1024) + 128;
        Object o = (Object) new byte[r];
        if (++mod % 16 == 0) {
            junk[i++] = o;
            totalSize += r * 4;
        }
        cleared = checkRefs(refs, cleared);
    }
}
Also used : SoftReference(java.lang.ref.SoftReference) Random(java.util.Random)

Aggregations

SoftReference (java.lang.ref.SoftReference)53 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