android 内存可用空间计算

来源:互联网 发布:国家自主根域名服务器 编辑:程序博客网 时间:2024/06/05 22:54
android里,activity后台运行是可以被系统关闭的,当空间不够时,所以计算可用空间
包括两正在运行的除了service的所有进程。 这句理解了 ,才看的懂下面的公式。

经过查看android4.0系统的管理应用程序的源码,获得。


思路:
可用空间 = 闲置空间 + 缓存 + 所有后台非service进程
代码:
//通过读/proc/meminfo得到内存总大小
memInfoReader = new MemInfoReader();
        memInfoReader.readMemInfo();
        mAm = (ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE);
        //
        ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
        mAm. getMemoryInfo(memInfo);
long lFree = memInfoReader.getFreeSize() +
+ memInfoReader.getCachedSize()
+ memInfo.hiddenAppThreshold;
        String free = Formatter.formatShortFileSize(getContext(), lFree);
        String uesd = Formatter.formatShortFileSize(getContext(),memInfoReader.getTotalSize()-lFree);

参考:
    ActivityManager.MemoryInfo: 系统可用内存信息
    ActivityManager.RecentTaskInfo: 最近的任务信息
    ActivityManager.RunningAppProcessInfo: 正在运行的进程信息
    ActivityManager.RunningServiceInfo: 正在运行的服务信息
    ActivityManager.RunningTaskInfo: 正在运行的任务信息

•VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
•RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)
•PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
•USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)
一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= USS
原创粉丝点击