TRIM_MEMORY_COMPLETE源码实例Demo

android.content.ComponentCallbacks2#TRIM_MEMORY_COMPLETE源码实例Demo

下面列出了android.content.ComponentCallbacks2#TRIM_MEMORY_COMPLETE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: android_9.0.0_r45   文件: WindowManagerGlobal.java
public void trimMemory(int level) {
    if (ThreadedRenderer.isAvailable()) {
        if (shouldDestroyEglContext(level)) {
            // Destroy all hardware surfaces and resources associated to
            // known windows
            synchronized (mLock) {
                for (int i = mRoots.size() - 1; i >= 0; --i) {
                    mRoots.get(i).destroyHardwareResources();
                }
            }
            // Force a full memory flush
            level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
        }

        ThreadedRenderer.trimMemory(level);

        if (ThreadedRenderer.sTrimForeground) {
            doTrimForeground();
        }
    }
}
 
源代码2 项目: FirefoxReality   文件: VRBrowserActivity.java
@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;
    }
}
 
@Override
public void onTrimMemory(int level) {
    switch(level) {
        // Trims memory when it reaches a moderate level and the session is inactive
        case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
            if(session.isActive()) break;

            // Trims memory when it reaches a critical level
        case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
        case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:

            Log.w(TAG, "Control resources are being removed due to system's low memory (Level: " + level + ")");
            destroy();
            break;
    }
}
 
源代码4 项目: Popeens-DSub   文件: DownloadService.java
@Override
public void onTrimMemory(int level) {
	ImageLoader imageLoader = SubsonicActivity.getStaticImageLoader(this);
	if(imageLoader != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
		Log.i(TAG, "Memory Trim Level: " + level);
		if (level < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
			if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
				imageLoader.onLowMemory(0.75f);
			} else if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW) {
				imageLoader.onLowMemory(0.50f);
			} else if(level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE) {
				imageLoader.onLowMemory(0.25f);
			}
		} else if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
			imageLoader.onLowMemory(0.25f);
		} else if(level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
			imageLoader.onLowMemory(0.75f);
		}
	}
}
 
源代码5 项目: CrossBow   文件: DefaultImageCache.java
/**
 * @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
    }
}
 
源代码6 项目: sketch   文件: SketchUtils.java
/**
 * 获取修剪级别的名称
 */
@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";
    }
}
 
源代码7 项目: android_9.0.0_r45   文件: WindowManagerGlobal.java
public static boolean shouldDestroyEglContext(int trimLevel) {
    // On low-end gfx devices we trim when memory is moderate;
    // on high-end devices we do this when low.
    if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
        return true;
    }
    if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_MODERATE
            && !ActivityManager.isHighEndGfx()) {
        return true;
    }
    return false;
}
 
源代码8 项目: 365browser   文件: MemoryPressureListener.java
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);
    }
}
 
源代码9 项目: android-chromium   文件: MemoryPressureListener.java
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);
    }
}
 
源代码10 项目: android-chromium   文件: MemoryPressureListener.java
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);
    }
}
 
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;
}
 
源代码12 项目: NFC-EMV-Reader   文件: MainApplication.java
@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;
    }
}
 
源代码13 项目: intra42   文件: AppClass.java
/**
 * 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;
    }
}