Android释放内存
来源:互联网 发布:淘宝退款率多少会封店 编辑:程序博客网 时间:2024/06/07 03:43
这两个方法是网上拷别人的,分别用来得到可用内存和内存总数,这样我们就可用得到了已用内存数。
View Code在Service里面清理内存
自己写的小方法,通过判断进程信息来判断哪些进程是无用的可用杀掉
View Code
解析如下:
RunningAppProcessInfo 得到正在运行的进程信息
pkgList:得到该进程下运行的包名
importance:该进程的重要程度
importance分为几个级别,数值越低就越重要。数值对应的重要性请看api,我判断是大于ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE的都杀掉,一般数值大于ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE的进程都长时间没用或者空进程了。
最后通过for循环把包名对应进程杀掉:
2.2版本以下的用activityManger.restartPackage(pkgList[j]); 对应权限<uses-permission android:name="android.permission.RESTART_PACKAGES" />
2.2以上的用killBackgroundProcesses(包名); 对应权限<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
之前我以为可用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权限的用户来说的)
转:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3655384
- Android释放内存
- android释放内存提示
- android内存释放处理
- android 释放webview内存
- android imageView释放内存
- android内存释放监控
- android imageView释放内存
- android图片的内存优化 释放内存
- Android 释放内存中的图片资源
- Android 释放内存中的图片资源
- Android 释放内存中的图片资源
- Android释放内存(结束进程)
- android释放内存的一个办法
- Android释放内存(结束进程)
- android图片优化,内存释放处理
- android释放内存的一个办法
- 双击退出Android 软件,释放驻留内存
- 关于Android程序优化内存释放
- From LOW to PWNED [11] Honorable Mention: Open NFS
- structs2下的第一个helloworld!
- 【设计模式】--C++设计模式类库 Loki介绍与用法
- MTK点阵字库介绍
- java中时间比较
- Android释放内存
- 在运行中打开程序
- oracle客户端数据库管理工具乱码(pl/sql、dba)
- linux web内网能访问,外网不可以
- UESTC 1558 Charitable Exchange
- submit方法与onsubmit事件
- KPCR
- rsync算法及其优化算法
- android crash log