How can I measure the actual memory usage of an application or process?

后端 未结 30 2747
心在旅途
心在旅途 2020-11-22 03:01

This question is covered here in great detail.

How do you measure the memory usage of an application or process in Linux?

From the blog articl

30条回答
  •  青春惊慌失措
    2020-11-22 03:30

    Valgrind can show detailed information, but it slows down the target application significantly, and most of the time it changes the behavior of the application.

    Exmap was something I didn't know yet, but it seems that you need a kernel module to get the information, which can be an obstacle.

    I assume what everyone wants to know with respect to "memory usage" is the following... In Linux, the amount of physical memory a single process might use can be roughly divided into following categories.

    • M.a anonymous mapped memory

    • .p private

      • .d dirty == malloc/mmapped heap and stack allocated and written memory
      • .c clean == malloc/mmapped heap and stack memory once allocated, written, then freed, but not reclaimed yet
    • .s shared

      • .d dirty == malloc/mmaped heap could get copy-on-write and shared among processes (edited)
      • .c clean == malloc/mmaped heap could get copy-on-write and shared among processes (edited)
    • M.n named mapped memory

    • .p private

      • .d dirty == file mmapped written memory private
      • .c clean == mapped program/library text private mapped
    • .s shared

      • .d dirty == file mmapped written memory shared
      • .c clean == mapped library text shared mapped

    Utility included in Android called showmap is quite useful

    virtual                    shared   shared   private  private
    size     RSS      PSS      clean    dirty    clean    dirty    object
    -------- -------- -------- -------- -------- -------- -------- ------------------------------
           4        0        0        0        0        0        0 0:00 0                  [vsyscall]
           4        4        0        4        0        0        0                         [vdso]
          88       28       28        0        0        4       24                         [stack]
          12       12       12        0        0        0       12 7909                    /lib/ld-2.11.1.so
          12        4        4        0        0        0        4 89529                   /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
          28        0        0        0        0        0        0 86661                   /usr/lib/gconv/gconv-modules.cache
           4        0        0        0        0        0        0 87660                   /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
           4        0        0        0        0        0        0 89528                   /usr/lib/locale/en_US.utf8/LC_TELEPHONE
           4        0        0        0        0        0        0 89527                   /usr/lib/locale/en_US.utf8/LC_ADDRESS
           4        0        0        0        0        0        0 87717                   /usr/lib/locale/en_US.utf8/LC_NAME
           4        0        0        0        0        0        0 87873                   /usr/lib/locale/en_US.utf8/LC_PAPER
           4        0        0        0        0        0        0 13879                   /usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
           4        0        0        0        0        0        0 89526                   /usr/lib/locale/en_US.utf8/LC_MONETARY
           4        0        0        0        0        0        0 89525                   /usr/lib/locale/en_US.utf8/LC_TIME
           4        0        0        0        0        0        0 11378                   /usr/lib/locale/en_US.utf8/LC_NUMERIC
        1156        8        8        0        0        4        4 11372                   /usr/lib/locale/en_US.utf8/LC_COLLATE
         252        0        0        0        0        0        0 11321                   /usr/lib/locale/en_US.utf8/LC_CTYPE
         128       52        1       52        0        0        0 7909                    /lib/ld-2.11.1.so
        2316       32       11       24        0        0        8 7986                    /lib/libncurses.so.5.7
        2064        8        4        4        0        0        4 7947                    /lib/libdl-2.11.1.so
        3596      472       46      440        0        4       28 7933                    /lib/libc-2.11.1.so
        2084        4        0        4        0        0        0 7995                    /lib/libnss_compat-2.11.1.so
        2152        4        0        4        0        0        0 7993                    /lib/libnsl-2.11.1.so
        2092        0        0        0        0        0        0 8009                    /lib/libnss_nis-2.11.1.so
        2100        0        0        0        0        0        0 7999                    /lib/libnss_files-2.11.1.so
        3752     2736     2736        0        0      864     1872                         [heap]
          24       24       24        0        0        0       24 [anon]
         916      616      131      584        0        0       32                         /bin/bash
    -------- -------- -------- -------- -------- -------- -------- ------------------------------
       22816     4004     3005     1116        0      876     2012 TOTAL
    

提交回复
热议问题