Search in sources :

Example 6 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)

Example 7 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 8 with SoftReference

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

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)

Example 9 with SoftReference

use of java.lang.ref.SoftReference in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class PersonalizationHelper method removeAllUserHistoryDictionaries.

public static void removeAllUserHistoryDictionaries(final Context context) {
    synchronized (sLangUserHistoryDictCache) {
        for (final ConcurrentHashMap.Entry<String, SoftReference<UserHistoryDictionary>> entry : sLangUserHistoryDictCache.entrySet()) {
            if (entry.getValue() != null) {
                final UserHistoryDictionary dict = entry.getValue().get();
                if (dict != null) {
                    dict.clear();
                }
            }
        }
        sLangUserHistoryDictCache.clear();
        final File filesDir = context.getFilesDir();
        if (filesDir == null) {
            Log.e(TAG, "context.getFilesDir() returned null.");
            return;
        }
        final boolean filesDeleted = FileUtils.deleteFilteredFiles(filesDir, new DictFilter(UserHistoryDictionary.NAME));
        if (!filesDeleted) {
            Log.e(TAG, "Cannot remove dictionary files. filesDir: " + filesDir.getAbsolutePath() + ", dictNamePrefix: " + UserHistoryDictionary.NAME);
        }
    }
}
Also used : SoftReference(java.lang.ref.SoftReference) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) File(java.io.File)

Example 10 with SoftReference

use of java.lang.ref.SoftReference in project DanmakuFlameMaster by Bilibili.

the class SpannedCacheStuffer method drawText.

@Override
public void drawText(BaseDanmaku danmaku, String lineText, Canvas canvas, float left, float top, TextPaint paint, boolean fromWorkerThread) {
    if (danmaku.obj == null) {
        super.drawText(danmaku, lineText, canvas, left, top, paint, fromWorkerThread);
        return;
    }
    SoftReference<StaticLayout> reference = (SoftReference<StaticLayout>) danmaku.obj;
    StaticLayout staticLayout = reference.get();
    boolean requestRemeasure = 0 != (danmaku.requestFlags & BaseDanmaku.FLAG_REQUEST_REMEASURE);
    boolean requestInvalidate = 0 != (danmaku.requestFlags & BaseDanmaku.FLAG_REQUEST_INVALIDATE);
    if (requestInvalidate || staticLayout == null) {
        if (requestInvalidate) {
            danmaku.requestFlags &= ~BaseDanmaku.FLAG_REQUEST_INVALIDATE;
        }
        CharSequence text = danmaku.text;
        if (text != null) {
            if (requestRemeasure) {
                staticLayout = new StaticLayout(text, paint, (int) Math.ceil(StaticLayout.getDesiredWidth(danmaku.text, paint)), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
                danmaku.paintWidth = staticLayout.getWidth();
                danmaku.paintHeight = staticLayout.getHeight();
                danmaku.requestFlags &= ~BaseDanmaku.FLAG_REQUEST_REMEASURE;
            } else {
                staticLayout = new StaticLayout(text, paint, (int) danmaku.paintWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
            }
            danmaku.obj = new SoftReference<>(staticLayout);
        } else {
            return;
        }
    }
    boolean needRestore = false;
    if (left != 0 && top != 0) {
        canvas.save();
        canvas.translate(left, top + paint.ascent());
        needRestore = true;
    }
    staticLayout.draw(canvas);
    if (needRestore) {
        canvas.restore();
    }
}
Also used : SoftReference(java.lang.ref.SoftReference) StaticLayout(android.text.StaticLayout)

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