package com.wuxiaolong.androidsamples.appmemory; import android.app.ActivityManager; import android.content.ComponentCallbacks2; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.wuxiaolong.androidsamples.R; import com.wuxiaolong.androidutils.library.LogUtil; public class AppMemoryActivity extends AppCompatActivity implements ComponentCallbacks2 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_app_memory); getMemoryInfo(); } private void getMemoryInfo() { ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); activityManager.getMemoryInfo(memoryInfo); LogUtil.d("totalMem=" + memoryInfo.totalMem + ",availMem=" + memoryInfo.availMem); if (!memoryInfo.lowMemory) { // Do memory intensive work ... } } @Override public void onTrimMemory(int level) { LogUtil.d("level=" + level); // Determine which lifecycle or system event was raised. switch (level) { //用户点击了Home键或者Back键退出应用,所有UI界面被隐藏 case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN: /* Release any UI objects that currently hold memory. The user interface has moved to the background. */ break; //当程序正在前台运行的时候,可能会接收到从onTrimMemory()中返回的下面的值: case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE: case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW: case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL: /* 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; //当应用进程退到后台正在被Cached的时候,可能会接收到从onTrimMemory()中返回的下面的值之一: case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND: case ComponentCallbacks2.TRIM_MEMORY_MODERATE: case ComponentCallbacks2.TRIM_MEMORY_COMPLETE: /* 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; } } }