Android清理内存
Android內(nèi)存清理,利用ActivityManager獲取當(dāng)前正在運(yùn)行的進(jìn)程,清理這些進(jìn)程釋放內(nèi)存。
可以根據(jù)importance的不同來(lái)判斷前臺(tái)或后臺(tái)RunningAppProcessInfo 里面的常量IMOPORTANCE就是上面所說(shuō)的前臺(tái)后臺(tái),其實(shí)IMOPORTANCE是表示這個(gè)app進(jìn)程的重要性,因?yàn)橄到y(tǒng)回收時(shí)候,會(huì)根據(jù)IMOPORTANCE來(lái)回收進(jìn)程的。具體可以去看文檔。。
在配置文件中添加權(quán)限
<uses-permission android:name=”android.permission.KILL_BACKGROUND_PROCESSES”/>
內(nèi)存清理代碼以及獲取系統(tǒng)內(nèi)存和各個(gè)APP占用內(nèi)存代碼如下:
public class SystemInfoUtil {
? ? /**
? ? ?* 獲取可用內(nèi)存
? ? ?*
? ? ?* @param context
? ? ?* @return
? ? ?*/
? ? public static long getAvailMemory(Context context) {
? ? ? ? ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
? ? ? ? ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
? ? ? ? am.getMemoryInfo(mi);
? ? ? ? return mi.availMem / (1024 * 1024);
? ? }
? ? /**
? ? ?* 獲取總內(nèi)存
? ? ?*
? ? ?* @param context
? ? ?* @return
? ? ?*/
? ? public static long getTotalMemory(Context context) {
? ? ? ? String str1 = "/proc/meminfo";// 系統(tǒng)內(nèi)存信息文件
? ? ? ? String str2;
? ? ? ? String[] arrayOfString;
? ? ? ? long initial_memory = 0;
? ? ? ? try {
? ? ? ? ? ? FileReader localFileReader = new FileReader(str1);
? ? ? ? ? ? BufferedReader localBufferedReader = new BufferedReader(
? ? ? ? ? ? ? ? ? ? localFileReader, 8192);
? ? ? ? ? ? str2 = localBufferedReader.readLine();// 讀取meminfo第一行,系統(tǒng)總內(nèi)存大小
? ? ? ? ? ? arrayOfString = str2.split("\\s+");
? ? ? ? ? ? initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 獲得系統(tǒng)總內(nèi)存,單位是KB,乘以1024轉(zhuǎn)換為Byte
? ? ? ? ? ? localBufferedReader.close();
? ? ? ? } catch (IOException e) {
? ? ? ? }
? ? ? ? return initial_memory / (1024 * 1024);
? ? }
// ? ?public static final int IMPORTANCE_BACKGROUND = 400//后臺(tái)
// ? ?public static final int IMPORTANCE_EMPTY = 500//空進(jìn)程
// ? ?public static final int IMPORTANCE_SERVICE = 300//在服務(wù)中
// ? ?public static final int IMPORTANCE_VISIBLE = 200//在屏幕前端、獲取不到焦點(diǎn)可理解為
// ? ?public static final int IMPORTANCE_FOREGROUND = 100//在屏幕最前端、可獲取到焦點(diǎn) 可理解為Activity生命周期的OnResume();
? ? /**
? ? ?* 清理內(nèi)存
? ? ?*
? ? ?* @param context
? ? ?*/
? ? public static void clearMemory(Context context) {
? ? ? ? ActivityManager activityManger = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
? ? ? ? List<ActivityManager.RunningAppProcessInfo> list = activityManger.getRunningAppProcesses();
? ? ? ? if (list != null) {
? ? ? ? ? ? for (int i = 0; i < list.size(); i++) {
? ? ? ? ? ? ? ? ActivityManager.RunningAppProcessInfo apinfo = list.get(i);
? ? ? ? ? ? ? ? String[] pkgList = apinfo.pkgList;
? ? ? ? ? ? ? ? if (apinfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
? ? ? ? ? ? ? ? ? ? for (int j = 0; j < pkgList.length; j++) {
? ? ? ? ? ? ? ? ? ? ? ? /**清理不可用的內(nèi)容空間**/
? ? ? ? ? ? ? ? ? ? ? ? activityManger.killBackgroundProcesses(pkgList[j]);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**
? ? ?* 獲取每個(gè)APP占用的內(nèi)存
? ? ?*
? ? ?* @param context
? ? ?*/
? ? public static void getEveryAppMemory(Context context) {
? ? ? ? ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
? ? ? ? List<ActivityManager.RunningAppProcessInfo> list = am.getRunningAppProcesses();
? ? ? ? if (list != null) {
? ? ? ? ? ? for (int i = 0; i < list.size(); i++) {
? ? ? ? ? ? ? ? ActivityManager.RunningAppProcessInfo appinfo = list.get(i);
? ? ? ? ? ? ? ? int[] myMempid = new int[]{appinfo.pid};
? ? ? ? ? ? ? ? Debug.MemoryInfo[] appMem = am.getProcessMemoryInfo(myMempid);
? ? ? ? ? ? ? ? int memSize = appMem[0].dalvikPrivateDirty / 1024;
? ? ? ? ? ? ? ? Log.e("AppMemory", appinfo.processName + ":" + memSize);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**
? ? ?* 清理應(yīng)用緩存
? ? ?*
? ? ?* @param context
? ? ?*/
? ? public static void clearAppCache(Context context) {
? ? ? ? File[] dir = context.getCacheDir().listFiles();
? ? ? ? if (dir != null) {
? ? ? ? ? ? for (File f : dir) {
? ? ? ? ? ? ? ? f.delete();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
總結(jié)
以上是生活随笔為你收集整理的Android清理内存的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 信息技术学业水平考试支撑记
- 下一篇: Android 实现简单的登陆注册功能(