Java Code Examples for android.content.ComponentCallbacks2#TRIM_MEMORY_BACKGROUND

The following examples show how to use android.content.ComponentCallbacks2#TRIM_MEMORY_BACKGROUND . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VRBrowserActivity.java    From FirefoxReality with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void onTrimMemory(int level) {

    // Determine which lifecycle or system event was raised.
    switch (level) {

        case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
        case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
        case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
        case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
            // Curently ignore these levels. They are handled somewhere else.
            break;
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:
            // It looks like these come in all at the same time so just always suspend inactive Sessions.
            Log.d(LOGTAG, "Memory pressure, suspending inactive sessions.");
            SessionStore.get().suspendAllInactiveSessions();
            break;
        default:
            Log.e(LOGTAG, "onTrimMemory unknown level: " + level);
            break;
    }
}
 
Example 2
Source File: DefaultImageCache.java    From CrossBow with Apache License 2.0 6 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void trimMemory(int level) {

    if(level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
        emptyCache(); //dump the cache
    }
    else if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE){
        trimCache(0.5f); // trim to half the max size
    }
    else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND){
        trimCache(0.7f); // trim to one seventh max size
    }
    else if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL){
        trimCache(0.2f); // trim to one fifth max size
    }
}
 
Example 3
Source File: SketchUtils.java    From sketch with Apache License 2.0 6 votes vote down vote up
/**
 * 获取修剪级别的名称
 */
@NonNull
public static String getTrimLevelName(int level) {
    switch (level) {
        case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
            return "COMPLETE";
        case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
            return "MODERATE";
        case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
            return "BACKGROUND";
        case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
            return "UI_HIDDEN";
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:
            return "RUNNING_CRITICAL";
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
            return "RUNNING_LOW";
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
            return "RUNNING_MODERATE";
        default:
            return "UNKNOWN";
    }
}
 
Example 4
Source File: MemoryPressureListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
public static void maybeNotifyMemoryPresure(int level) {
    if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
        nativeOnMemoryPressure(MemoryPressureLevel.CRITICAL);
    } else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND
            || level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
        // Don't notifiy on TRIM_MEMORY_UI_HIDDEN, since this class only
        // dispatches actionable memory pressure signals to native.
        nativeOnMemoryPressure(MemoryPressureLevel.MODERATE);
    }
}
 
Example 5
Source File: MemoryPressureListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void maybeNotifyMemoryPresure(int level) {
    if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
        nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_CRITICAL);
    } else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND ||
            level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
        // Don't notifiy on TRIM_MEMORY_UI_HIDDEN, since this class only
        // dispatches actionable memory pressure signals to native.
        nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_MODERATE);
    }
}
 
Example 6
Source File: MemoryPressureListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void maybeNotifyMemoryPresure(int level) {
    if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
        nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_CRITICAL);
    } else if (level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND ||
            level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
        // Don't notifiy on TRIM_MEMORY_UI_HIDDEN, since this class only
        // dispatches actionable memory pressure signals to native.
        nativeOnMemoryPressure(MemoryPressureLevelList.MEMORY_PRESSURE_MODERATE);
    }
}
 
Example 7
Source File: ActivityManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
int runSendTrimMemory(PrintWriter pw) throws RemoteException {
    int userId = UserHandle.USER_CURRENT;
    String opt;
    while ((opt = getNextOption()) != null) {
        if (opt.equals("--user")) {
            userId = UserHandle.parseUserArg(getNextArgRequired());
            if (userId == UserHandle.USER_ALL) {
                getErrPrintWriter().println("Error: Can't use user 'all'");
                return -1;
            }
        } else {
            getErrPrintWriter().println("Error: Unknown option: " + opt);
            return -1;
        }
    }

    String proc = getNextArgRequired();
    String levelArg = getNextArgRequired();
    int level;
    switch (levelArg) {
        case "HIDDEN":
            level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
            break;
        case "RUNNING_MODERATE":
            level = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE;
            break;
        case "BACKGROUND":
            level = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
            break;
        case "RUNNING_LOW":
            level = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
            break;
        case "MODERATE":
            level = ComponentCallbacks2.TRIM_MEMORY_MODERATE;
            break;
        case "RUNNING_CRITICAL":
            level = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
            break;
        case "COMPLETE":
            level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
            break;
        default:
            try {
                level = Integer.parseInt(levelArg);
            } catch (NumberFormatException e) {
                getErrPrintWriter().println("Error: Unknown level option: " + levelArg);
                return -1;
            }
    }
    if (!mInterface.setProcessMemoryTrimLevel(proc, userId, level)) {
        getErrPrintWriter().println("Unknown error: failed to set trim level");
        return -1;
    }
    return 0;
}
 
Example 8
Source File: MainApplication.java    From NFC-EMV-Reader with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onTrimMemory(int level) {
    super.onTrimMemory(level);
    LogUtil.d(TAG, "\"" + TAG + "\": Application trim memory");

    switch (level) {
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
            LogUtil.d(TAG, "Trim memory: Running moderate");

            break;
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
            LogUtil.d(TAG, "Trim memory: Running low");

            break;
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:
            LogUtil.d(TAG, "Trim memory: Running critical");

            break;

        case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
            LogUtil.d(TAG, "Trim memory: UI hidden");

            break;

        case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
            LogUtil.d(TAG, "Trim memory: Background");

            break;
        case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
            LogUtil.d(TAG, "Trim memory: Moderate");

            break;
        case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
            LogUtil.d(TAG, "Trim memory: Complete");

            if (sPackageName == null) {
                sPackageName = getPackageName();
            }

            if (sFilesDirMemory == null) {
                sFilesDirMemory = getFilesDir();
            }
            if (sFilesDirPathMemory == null) {
                sFilesDirPathMemory = getFilesDir().getPath();
            }

            if (sCacheDirMemory == null) {
                sCacheDirMemory = getCacheDir();
            }
            if (sCacheDirPathMemory == null) {
                sCacheDirPathMemory = getCacheDir().getPath();
            }

            break;
    }
}
 
Example 9
Source File: AppClass.java    From intra42 with Apache License 2.0 4 votes vote down vote up
/**
 * Release memory when the UI becomes hidden or when system resources become low.
 *
 * @param level the memory-related event that was raised.
 */
public void onTrimMemory(int level) {
    super.onTrimMemory(level);

    // Determine which lifecycle or system event was raised.
    switch (level) {

        case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:

            /*
               Release any UI objects that currently hold memory.

               "release your UI resources" is actually about things like caches.
               You usually don't have to worry about managing views or UI components because the OS
               already does that, and that's why there are all those callbacks for creating, starting,
               pausing, stopping and destroying an activity.
               The user interface has moved to the background.
            */

            break;

        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:

            picassoCache.clear();

            /*
               Release any memory that your app doesn't need to run.

               The device is running low on memory while the app is running.
               The event raised indicates the severity of the memory-related event.
               If the event is TRIM_MEMORY_RUNNING_CRITICAL, then the system will
               begin killing background processes.
            */

            break;

        case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
        case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
        case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:

            picassoCache.clear();

            /*
               Release as much memory as the process can.
               The app is on the LRU list and the system is running low on memory.
               The event raised indicates where the app sits within the LRU list.
               If the event is TRIM_MEMORY_COMPLETE, the process will be one of
               the first to be terminated.
            */


            break;

        default:
            /*
              Release any non-critical data structures.

              The app received an unrecognized memory level value
              from the system. Treat this as a generic low-memory message.
            */
            break;
    }
}