Android系统开发之内存可用空间计算

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

猜你喜欢

转载自dengzhangtao.iteye.com/blog/1768681