android getMemoryClass()的使用

来源:互联网 发布:中国需要几艘航母 知乎 编辑:程序博客网 时间:2024/06/05 15:23
今天,我简单记录

getMemoryClass()的使用,之前看到这个,不知道有什么作用,后来在网上查找资料,明白用途了。


Android基本概念: 应用, 任务, 进程, 和线程


开始我们的ActivityManager学习吧!


ActivityManager的内部类:

ActivityManager.MemoryInfo : 系统内存使用情况的信息 ,可以通过getMemoryInfo(ActivityManager.MemoryInfo). 来获得该类对象

ActivityManager.ProcessErrorStateInfo: 错误状态的进程

ActivityManager.RecentTaskInfo:近期用户打开的任务信息

ActivityManager.RunningAppProcessInfo: 正在运行的应用程序进程

ActivityManager.RunningServiceInfo: 正在运行的服务信息

ActivityManager.RunningTaskInfo: 正在运行的任务信息


ActivityManager类对象的获取: 

ActivityManager mActivityManager =  (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);

ActivityManager类中的方法: 

public int getLargeMemoryClass ()说明: 如果在AndroidManifest.xml中的<application >标签下将属性android:largeHeap=“true”

 这个属性决定你的应用进程是否应当在更大的Dalvik堆中创建,为了在运行时查看可用内存,

可用getLargeMemoryClass()或者 getMemoryClass()



 通过代码查看每个进程可用的最大内存,即heapgrowthlimit值:
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memClass = activityManager.getMemoryClass();//64,以m为单位

注意:在设置了heapgrowthlimit的情况下,单个进程可用最大内存为heapgrowthlimit值。在android开发中,如果要使用大堆,需要在manifest中指定android:largeHeap为true,这样dvm heap最大可达heapsize。


其实就是查询手机对于app的最大使用内存,这个可以判断手机的性能,从而确定自己的app该注重性能还是注重内存的使用。


android getMemoryClass()的使用就讲完了、


就这么简单。






原创粉丝点击