虽然我找到了很多关于这个问题的话题,但是我仍然对SDK中的api和dumpsys meminfo的使用之间的区别感到困惑。对不起。
我正在编写一个 android 演示来监控它自己的内存使用情况。首先:我会在本地层分配一些内存;其次:在我的MainActivity的java层代码中,我创建了一个独立的线程来监控内存使用情况,代码是这样的:
int[] pids = {android.os.Process.myPid()};
// For debug
while (true && !exit) {
ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
Debug.MemoryInfo[] mem_info_list = activityManager.getProcessMemoryInfo(pids);
Log.println(Log.DEBUG, "com.test.faultinject", "pids = " + Arrays.toString(pids));
Log.println(Log.DEBUG, "com.test.faultinject", "getTotalPrivateClean = " + mem_info_list[0].getTotalPrivateClean()/1024 + "MB");
Log.println(Log.DEBUG, "com.test.faultinject", "getTotalPrivateDirty = " + mem_info_list[0].getTotalPrivateDirty()/1024 + "MB");
Log.println(Log.DEBUG, "com.test.faultinject", "getTotalPss = " + mem_info_list[0].getTotalPss()/1024 + "MB");
Log.println(Log.DEBUG, "com.test.faultinject", "getTotalSharedClean = " + mem_info_list[0].getTotalSharedClean()/1024 + "MB");
Log.println(Log.DEBUG, "com.test.faultinject", "getTotalSharedDirty = " + mem_info_list[0].getTotalPrivateDirty()/1024 + "MB");
Log.println(Log.DEBUG, "com.test.faultinject", "getTotalSwappablePss = " + mem_info_list[0].getTotalSwappablePss()/1024 + "MB");
SystemClock.sleep(1000);
}
运行此应用程序时,我们可以在 logcat 中找到如下所示的日志:
2021-04-02 11:24:52.947 22273-22316/com.test.mytest D/com.test.mytest: pids = [22273]
2021-04-02 11:24:52.947 22273-22316/com.test.mytest D/com.test.mytest: getTotalPrivateClean = 2MB
2021-04-02 11:24:52.947 22273-22316/com.test.mytest D/com.test.mytest: getTotalPrivateDirty = 44MB
2021-04-02 11:24:52.947 22273-22316/com.test.mytest D/com.test.mytest: getTotalPss = 52MB
2021-04-02 11:24:52.947 22273-22316/com.test.mytest D/com.test.mytest: getTotalSharedClean = 74MB
2021-04-02 11:24:52.947 22273-22316/com.test.mytest D/com.test.mytest: getTotalSharedDirty = 44MB
2021-04-02 11:24:52.947 22273-22316/com.test.mytest D/com.test.mytest: getTotalSwappablePss = 2MB
2021-04-02 11:24:53.952 22273-22316/com.test.mytest D/com.test.mytest: pid = 22273
2021-04-02 11:24:53.952 22273-22316/com.test.mytest D/com.test.mytest: pids = [22273]
2021-04-02 11:24:53.952 22273-22316/com.test.mytest D/com.test.mytest: getTotalPrivateClean = 2MB
2021-04-02 11:24:53.952 22273-22316/com.test.mytest D/com.test.mytest: getTotalPrivateDirty = 44MB
2021-04-02 11:24:53.952 22273-22316/com.test.mytest D/com.test.mytest: getTotalPss = 52MB
2021-04-02 11:24:53.952 22273-22316/com.test.mytest D/com.test.mytest: getTotalSharedClean = 74MB
2021-04-02 11:24:53.953 22273-22316/com.test.mytest D/com.test.mytest: getTotalSharedDirty = 44MB
2021-04-02 11:24:53.953 22273-22316/com.test.mytest D/com.test.mytest: getTotalSwappablePss = 2MB
2021-04-02 11:24:54.957 22273-22316/com.test.mytest D/com.test.mytest: pid = 22273
2021-04-02 11:24:54.958 22273-22316/com.test.mytest D/com.test.mytest: pids = [22273]
2021-04-02 11:24:54.958 22273-22316/com.test.mytest D/com.test.mytest: getTotalPrivateClean = 2MB
2021-04-02 11:24:54.958 22273-22316/com.test.mytest D/com.test.mytest: getTotalPrivateDirty = 44MB
2021-04-02 11:24:54.958 22273-22316/com.test.mytest D/com.test.mytest: getTotalPss = 52MB
2021-04-02 11:24:54.958 22273-22316/com.test.mytest D/com.test.mytest: getTotalSharedClean = 74MB
2021-04-02 11:24:54.958 22273-22316/com.test.mytest D/com.test.mytest: getTotalSharedDirty = 44MB
2021-04-02 11:24:54.958 22273-22316/com.test.mytest D/com.test.mytest: getTotalSwappablePss = 2MB
我们可以看到 getTotalPss 函数总是返回 54MB。这时候我用dumpsys meminfo 22273
dump memory info in adb shell
,结果是这样的:
PD1824:/ $ dumpsys meminfo 22273
Applications Memory Usage (in Kilobytes):
Uptime: 462065977 Realtime: 546694666
** MEMINFO in pid 22273 [com.test.mytest] **
Pss Private Private SwapPss Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 133751 133684 0 40 149504 143317 6186
Dalvik Heap 2660 2640 0 24 14305 2017 12288
Dalvik Other 896 892 0 0
Stack 64 64 0 0
Ashmem 2 0 0 0
Gfx dev 2332 2332 0 0
Other dev 19 4 12 0
.so mmap 5036 216 1796 8
.jar mmap 1970 0 340 0
.apk mmap 565 0 40 0
.ttf mmap 289 0 128 0
.dex mmap 2384 2384 0 0
.oat mmap 56 0 0 0
.art mmap 6296 5924 0 35
Other mmap 495 160 0 0
Unknown 953 908 0 5
TOTAL 157880 149208 2316 112 163809 145334 18474
App Summary
Pss(KB)
------
Java Heap: 8564
Native Heap: 133684
Code: 4904
Stack: 64
Graphics: 2332
Private Other: 1976
System: 6356
TOTAL: 157880 TOTAL SWAP PSS: 112
Objects
Views: 60 ViewRootImpl: 1
AppContexts: 6 Activities: 1
Assets: 11 AssetManagers: 0
Local Binders: 16 Proxy Binders: 36
Parcel memory: 4 Parcel count: 16
Death Recipients: 2 OpenSSL Sockets: 0
WebViews: 0
SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 0
我们可以看到 TOTAL PSS 为 157880KB,与 getTotalPss 函数返回的结果不一样。实际上,实际内存更接近dumpsys的结果。
但是几分钟后,我们可以看到 getTotalPss 函数的返回值几乎接近 dumpsys 的结果:
2021-04-02 11:31:02.823 22273-22316/com.test.mytest D/com.test.mytest: pid = 22273
2021-04-02 11:31:02.823 22273-22316/com.test.mytest D/com.test.mytest: pids = [22273]
2021-04-02 11:31:02.823 22273-22316/com.test.mytest D/com.test.mytest: getTotalPrivateClean = 2MB
2021-04-02 11:31:02.823 22273-22316/com.test.mytest D/com.test.mytest: getTotalPrivateDirty = 146MB
2021-04-02 11:31:02.824 22273-22316/com.test.mytest D/com.test.mytest: getTotalPss = 155MB
2021-04-02 11:31:02.824 22273-22316/com.test.mytest D/com.test.mytest: getTotalSharedClean = 74MB
2021-04-02 11:31:02.824 22273-22316/com.test.mytest D/com.test.mytest: getTotalSharedDirty = 146MB
2021-04-02 11:31:02.824 22273-22316/com.test.mytest D/com.test.mytest: getTotalSwappablePss = 2MB
2021-04-02 11:31:03.828 22273-22316/com.test.mytest D/com.test.mytest: pid = 22273
2021-04-02 11:31:03.828 22273-22316/com.test.mytest D/com.test.mytest: pids = [22273]
2021-04-02 11:31:03.828 22273-22316/com.test.mytest D/com.test.mytest: getTotalPrivateClean = 2MB
2021-04-02 11:31:03.828 22273-22316/com.test.mytest D/com.test.mytest: getTotalPrivateDirty = 146MB
2021-04-02 11:31:03.828 22273-22316/com.test.mytest D/com.test.mytest: getTotalPss = 155MB
2021-04-02 11:31:03.828 22273-22316/com.test.mytest D/com.test.mytest: getTotalSharedClean = 74MB
2021-04-02 11:31:03.828 22273-22316/com.test.mytest D/com.test.mytest: getTotalSharedDirty = 146MB
2021-04-02 11:31:03.828 22273-22316/com.test.mytest D/com.test.mytest: getTotalSwappablePss = 2MB
谁能告诉我 Debug.MemoryInfo.getTotalPss() 函数和 dumpsys meminfo 的结果有什么不同?为什么 getTotalPss() 函数不能像 dumpsys 一样得到真正的结果?非常感谢!
2021-04-09更新
我的测试设备的android系统是Android10。我尝试阅读 ActivityManager::getProcessMemoryInfo 的源代码,它从本机获取内存信息的速率有限:
我很困惑为什么间隔这么长,如果我们可以改变间隔?