1.代码获取当前app内存的使用情况
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
//最大分配内存
int memory = activityManager.getMemoryClass();
System.out.println("memory: "+memory);
//最大分配内存获取方法2
float maxMemory = (float) (Runtime.getRuntime().maxMemory() * 1.0/ (1024 * 1024));
//当前分配的总内存
float totalMemory = (float) (Runtime.getRuntime().totalMemory() * 1.0/ (1024 * 1024));
//剩余内存
float freeMemory = (float) (Runtime.getRuntime().freeMemory() * 1.0/ (1024 * 1024));
System.out.println("maxMemory: "+maxMemory);
System.out.println("totalMemory: "+totalMemory);
System.out.println("freeMemory: "+freeMemory);
结果
System.out: memory: 256
System.out: maxMemory: 256.0
System.out: totalMemory: 11.974937
System.out: freeMemory: 3.6257935
这说明我这个app在当前手机的最大分配内存是256m,现在已经分配了11m,这11m中有6m是空闲的
当然通过Monitors可以更直观的查看内存使用情况
2.使用dos命令
(1)打开dos窗口,执行adb shell
(2)dumpsys meminfo 包名
结果:
3.使用Monitors或者DDMS