日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android清理内存

發(fā)布時(shí)間:2024/1/1 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android清理内存 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。