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

[size=medium]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[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值