2

Although I find a lot topies about this question, but I still feel confused about the difference between the api in SDK and usage of dumpsys meminfo. I am sorry.

I'am writting an android demo to monitor its own memory usage. Firstly: I will malloc some memory in native layer; secondly: In my java layer code in MainActivity, I create an independent thread to monitor memory usage, the code is like this:

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);
        }

when running this application, we can found the log in logcat like below:

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

we can see that getTotalPss function always return 54MB. At this time, I use dumpsys meminfo 22273 to dump memory info in adb shell, the result is like this:

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

We can see that the TOTAL PSS is 157880KB, it is not the same with the result return by getTotalPss function. And actually, the real memory is more close to result of dumpsys.

But after a few minutes, we can see that getTotalPss function return value is almost closed to result of 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

Can anybody tell me what is the different between Debug.MemoryInfo.getTotalPss() function and the result of dumpsys meminfo? And why getTotalPss() function cannot get real result as soon as dumpsys? Thank you very much!


Update on 2021-04-09
The android system of my test device is Android10. I try to read source code of ActivityManager::getProcessMemoryInfo, it has a limited rate to get memory info from native: enter image description here

And we find that the inteval of this function is set to 5mins default: enter image description here

I feel confuse why the inteval is so long and if we can change the inteval?

Siven
  • 21
  • 3

0 Answers0