背景
发现项目中居然没有使用largeHeap
,这不是吃大亏了?这是标配。然后看了下阳光沙滩的Android项目,我也没配置。🤡是我自己。
largeHeap
查看设备的堆限制
cupid:/ $ getprop | grep heap
[dalvik.vm.heapgrowthlimit]: [256m]
[dalvik.vm.heapmaxfree]: [16m]
[dalvik.vm.heapminfree]: [8m]
[dalvik.vm.heapsize]: [512m]
[dalvik.vm.heapstartsize]: [8m]
[dalvik.vm.heaptargetutilization]: [0.6]
[init.svc.heapprofd]: [stopped]
我的是AOSP系统,所以限制可能放宽了。有部分设备是384mb。 不设置largeHeap是拿不到heapsize的大小。也就是最多256m 在配置文件设置:
<application
android:largeHeap="true"
....
就是申请可以达到最大的堆。 可以查看现在大厂的App这个配置就是标配了。
查看App自己的内存信息
代码:
public static Debug.MemoryInfo logMemoryInfo(Context context) {
// 获取 ActivityManager
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager == null) {
Log.e(TAG, "logMemoryInfo: activityManager is null");
return null;
}
//以M为单位 当前APP最大限制内存
int memoryClass = activityManager.getMemoryClass();
//通过在Manifest <application>标签中largeHeap属性的值为"true" 为应用分配的最大的内存
int largeMemoryClass = activityManager.getLargeMemoryClass();
String stringBuilder = "memoryClass === " + memoryClass + "\n" +
"largeMemoryClass === " + largeMemoryClass;
Log.e(TAG, stringBuilder);
// 获取内存信息
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
// 获取 Java 堆内存
Debug.MemoryInfo debugMemoryInfo = new Debug.MemoryInfo();
Debug.getMemoryInfo(debugMemoryInfo);
// 获取 native 内存使用情况 (需要 API 21 以上)
long nativeMemory = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
nativeMemory = Debug.getNativeHeapAllocatedSize();
}
// 输出内存信息到 logcat
Log.d(TAG, "=== Memory Info ===");
Log.d(TAG, "Total Memory: " + memoryInfo.totalMem * 1.0f / (1024 * 1024) + " MB");
Log.d(TAG, "Available Memory: " + memoryInfo.availMem / (1024 * 1024) + " MB");
Log.d(TAG, "getTotalPss Memory: " + debugMemoryInfo.getTotalPss() * 1.0f / 1024 + " MB, " + debugMemoryInfo.getTotalPss());
Log.d(TAG, "Native Memory: " + nativeMemory * 1.0f / (1024 * 1024) + " MB, " + nativeMemory);
Log.d(TAG, "Dalvik Heap Size: " + debugMemoryInfo.dalvikPss * 1.0f / 1024 + " MB, " + debugMemoryInfo.dalvikPss);
Log.d(TAG, "Native Heap Size: " + debugMemoryInfo.nativePss * 1.0f / 1024 + " MB, " + debugMemoryInfo.nativePss);
Log.d(TAG, "Other PSS: " + debugMemoryInfo.otherPss * 1.0f / 1024 + " MB, " + debugMemoryInfo.otherPss);
Log.d(TAG, "=== End of Memory Info ===");
return debugMemoryInfo;
}
可以查看信息输出:
E/BaseInfoUtil: memoryClass === 256, largeMemoryClass === 384
D/BaseInfoUtil: === Memory Info ===
D/BaseInfoUtil: Total Memory: 1997.9492 MB
D/BaseInfoUtil: Available Memory: 1000 MB
D/BaseInfoUtil: getTotalPss Memory: 206.4209 MB, 211375
D/BaseInfoUtil: Native Memory: 40.079872 MB, 42026792
D/BaseInfoUtil: Dalvik Heap Size: 3.8789062 MB, 3972
D/BaseInfoUtil: Native Heap Size: 36.762695 MB, 37645
D/BaseInfoUtil: Other PSS: 165.70117 MB, 169678
D/BaseInfoUtil: === End of Memory Info ===
I/BaseInfoUtil: myMem:{"dalvikPrivateClean":0,"dalvikPrivateDirty":3964,"dalvikPss":3972,"dalvikRss":4288,"dalvikSharedClean":0,"dalvikSharedDirty":324,"dalvikSwappablePss":0,"dalvikSwappedOut":1572,"dalvikSwappedOutPss":30,"hasSwappedOutPss":true,"nativePrivateClean":0,"nativePrivateDirty":37632,"nativePss":37645,"nativeRss":38128,"nativeSharedClean":0,"nativeSharedDirty":496,"nativeSwappablePss":0,"nativeSwappedOut":1036,"nativeSwappedOutPss":24,"otherPrivateClean":38356,"otherPrivateDirty":113168,"otherPss":169678,"otherRss":278480,"otherSharedClean":109864,"otherSharedDirty":17092,"otherStats":[1894,0,1928,1892,36,0,0,0,0,56,0,56,56,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13266,0,20812,5728,15060,0,24,0,0,0,0,0,0,0,0,0,0,0,16,0,200,0,184,16,0,0,0,38617,33164,75920,2188,1320,33164,39248,2344,25,0,0,0,0,0,0,0,0,0,13375,1948,33816,9888,0,1948,21980,0,0,1742,140,3448,0,0,140,3308,0,0,14555,2128,37728,10392,0,2128,25208,0,0,47,0,2280,0,0,0,2280,124,0,5179,252,14788,4588,288,252,9660,24,0,2498,0,8880,4,12,708,8156,16,0,0,0,0,0,0,0,0,0,0,52668,0,52668,52668,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3148,0,3148,3148,0,0,0,0,0,412,0,428,412,16,0,0,904,13,376,0,676,368,308,0,0,668,17,36,0,36,36,0,0,0,0,0,1332,0,1332,1332,0,0,0,0,0,118,0,144,116,28,0,0,0,0,256,0,256,256,0,0,0,0,0,0,0,0,0,0,0,0,0,0,188,0,196,188,8,0,0,0,0,4148,2128,27068,0,0,2128,24940,0,0,10396,0,10476,10392,0,0,84,0,0,11,0,184,0,0,0,184,0,0,0,0,0,0,0,0,0,0,0,5179,252,14788,4588,288,252,9660,24,0],"otherSwappablePss":37632,"otherSwappedOut":2624,"otherSwappedOutPss":26}
memoryClass === 256
largeMemoryClass === 512
=== Memory Info ===
Total Memory: 3576.871 MB
Available Memory: 1910 MB
getTotalPss Memory: 77.0 MB, 78848
Native Memory: 18.434265 MB, 19329728
Dalvik Heap Size: 6.4716797 MB, 6627
Native Heap Size: 16.22168 MB, 16611
Other PSS: 54.30664 MB, 55610
=== End of Memory Info ===
onTrimMemory 20, json: {"dalvikPrivateClean":0,"dalvikPrivateDirty":5512,"dalvikPss":6627,"dalvikSharedClean":0,"dalvikSharedDirty":23268,"dalvikSwappablePss":0,"dalvikSwappedOut":0,"nativePrivateClean":0,"nativePrivateDirty":16460,"nativePss":16611,"nativeSharedClean":0,"nativeSharedDirty":3620,"nativeSwappablePss":0,"nativeSwappedOut":0,"otherPrivateClean":352,"otherPrivateDirty":48899,"otherPss":55610,"otherSharedClean":41164,"otherSharedDirty":24652,"otherStats":[8631,0,25096,7524,17572,0,0,0,0,1264,0,1280,1264,16,0,0,0,0,0,0,0,0,0,0,0,0,0,24,0,380,0,368,0,12,0,0,5104,0,5104,5104,0,0,0,0,0,16,0,208,0,192,16,0,0,0,3355,20,26144,232,4616,20,21276,0,0,1182,0,17228,0,0,0,17228,0,0,22366,68,23752,21848,1076,68,760,0,0,98,0,344,0,0,0,344,0,0,2,0,84,0,84,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,602,0,1840,16,36,248,1540,0,0,0,0,0,0,0,0,0,0,0,9335,0,9335,9335,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3224,0,3224,3224,0,0,0,0,0,197,0,1988,112,1876,0,0,0,0,2902,0,23264,1872,21392,0,0,0,0,304,0,304,304,0,0,0,0,0,7813,0,23152,7080,16072,0,0,0,0,260,0,608,244,364,0,0,0,0,18,0,452,0,452,0,0,0,0,364,0,704,24,680,0,0,0,0,0,0,0,0,0,0,0,0,0,176,0,180,176,4,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,84,0,84,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"otherSwappablePss":88,"otherSwappedOut":0}
这里有2份,上面一个是电视机的,下面是我的手机的。