为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

内存清理

2017-10-23 4页 doc 17KB 22阅读

用户头像

is_215732

暂无简介

举报
内存清理内存清理 android 内存清理 这两天在搞个内存清理小插件,网上很少这方面资料,之前找过清理缓存的例子测试,结果 不成功。后来自己思考该怎么清理内存,于是成功搞出来了。 这两个方法是网上拷别人的,分别用来得到可用内存和内存总数,这样我们就可用得到了已 用内存数。 View Code private long getAvailMemory(Context context) { // 获取android当前 可用内存大小 ActivityManager am = (ActivityManager) context....
内存清理
内存清理 android 内存清理 这两天在搞个内存清理小插件,网上很少这方面资料,之前找过清理缓存的例子测试,结果 不成功。后来自己思考该怎么清理内存,于是成功搞出来了。 这两个方法是网上拷别人的,分别用来得到可用内存和内存总数,这样我们就可用得到了已 用内存数。 View Code private long getAvailMemory(Context context) { // 获取android当前 可用内存大小 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); MemoryInfo mi = new MemoryInfo(); am.getMemoryInfo(mi); //mi.availMem; 当前系统的可用 内存 //return Formatter.formatFileSize(context, mi.availMem);// 将获取的 内存大小规格化 return mi.availMem/(1024*1024); } private 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+"); for (String num : arrayOfString) { Log.i(str2, num + "\t"); } initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB, 乘以1024转换为Byte localBufferedReader.close(); } catch (IOException e) { } //return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB,内存大小规格化 return initial_memory/(1024*1024); } 在Service里面清理内存 自己写的小方法,通过判断进程信息来判断哪些进程是无用的可用杀掉 View Code ActivityManager activityManger=(ActivityManager) this.getSystemService(ACTIVITY_SERVICE); List list=activityManger.getRunningAppProcesses(); if(list!=null) for(int i=0;iActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE) { // Process.killProcess(apinfo.pid); for(int j=0;j 2.2以上的用killBackgroundProcesses(包名); 对应权限 之前我以为可用android.os.Process.killProcess(pid);来杀掉其他进程,后来发觉不行 网上找到的解析是 对于这个方法,需要详细解释一下。在SDK的文档中,解释如下: Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes. 英语不好,不能准确翻译,所以希望大家自己理解,免得对大家造成误解。我个人对这句话 的解释就是,这个方法使用是有条件的: a、将被杀掉的进程 和 当前进程 处于同一个包或者应用程序中; b、将被杀掉的进程 是由当前应用程序所创建的附加进程; c、将被杀掉的进程 和 当前进程 共享了普通用户的UID。(这里的普通用户,是相对于Root权限的用户来说的)
/
本文档为【内存清理】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索