Android内存清理,利用ActivityManager获取当前正在运行的进程,清理这些进程释放内存。
可以根据importance的不同来判断前台或后台RunningAppProcessInfo 里面的常量IMOPORTANCE就是上面所说的前台后台,其实IMOPORTANCE是表示这个app进程的重要性,因为系统回收时候,会根据 IMOPORTANCE来回收进程的。具体可以去看文档。。
在配置文件中添加权限
<uses-permission android:name=”android.permission.KILL_BACKGROUND_PROCESSES”/>
内存清理代码以及获取系统内存和各个APP占用内存代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
/**
* Created by neo on 15/5/1.
*/
public class SystemInfoUtil {
/**
* 获取可用内存
*
* @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);
}
/**
* 获取总内存
*
* @param context
* @return
*/
public static long getTotalMemory(Context context) {
String str1 = "/proc/meminfo";// 系统内存信息文件
String str2;
String[] arrayOfString;
long initial_memory = 0;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(
localFileReader, 8192);
str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小
arrayOfString = str2.split("\\s+");
initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
localBufferedReader.close();
} catch (IOException e) {
}
return initial_memory / (1024 * 1024);
}
// public static final int IMPORTANCE_BACKGROUND = 400//后台
// public static final int IMPORTANCE_EMPTY = 500//空进程
// public static final int IMPORTANCE_SERVICE = 300//在服务中
// public static final int IMPORTANCE_VISIBLE = 200//在屏幕前端、获取不到焦点可理解为
// public static final int IMPORTANCE_FOREGROUND = 100//在屏幕最前端、可获取到焦点 可理解为Activity生命周期的OnResume();
/**
* 清理内存
*
* @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++) {
/**清理不可用的内容空间**/
activityManger.killBackgroundProcesses(pkgList[j]);
}
}
}
}
}
/**
* 获取每个APP占用的内存
*
* @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);
}
}
}
/**
* 清理应用缓存
*
* @param context
*/
public static void clearAppCache(Context context) {
File[] dir = context.getCacheDir().listFiles();
if (dir != null) {
for (File f : dir) {
f.delete();
}
}
}
}
|
原文:http://www.cnblogs.com/Free-Thinker/p/5107860.html