Skip to content

Instantly share code, notes, and snippets.

@didix21
Last active December 8, 2022 16:31
Show Gist options
  • Save didix21/e90d218c8d13c48ad526d271e69f5ea6 to your computer and use it in GitHub Desktop.
Save didix21/e90d218c8d13c48ad526d271e69f5ea6 to your computer and use it in GitHub Desktop.

Main Commands

vmmap

Display the virtual memory regions allocated in a process.

Some interesting flags

  • Get only the summary
$ vmmap --summary MemgraphName.memgraph

OUTPUT:

                                VIRTUAL RESIDENT    DIRTY  SWAPPED VOLATILE   NONVOL    EMPTY   REGION
REGION TYPE                        SIZE     SIZE     SIZE     SIZE     SIZE     SIZE     SIZE    COUNT (non-coalesced)
===========                     ======= ========    =====  ======= ========   ======    =====  =======
Activity Tracing                   256K      32K      32K       0K       0K      32K       0K        1
Dispatch continuations            2048K      64K      64K       0K       0K       0K       0K        1
Foundation                          16K      16K      16K       0K       0K       0K       0K        1
Kernel Alloc Once                   32K      16K      16K       0K       0K       0K       0K        1
MALLOC guard page                  128K       0K       0K       0K       0K       0K       0K        8
MALLOC metadata                    144K     144K     144K       0K       0K       0K       0K        9
MALLOC_LARGE                      1184K    1040K    1040K       0K       0K       0K       0K       43         see MALLOC ZONE table below
MALLOC_LARGE metadata               16K      16K      16K       0K       0K       0K       0K        1         see MALLOC ZONE table below
MALLOC_SMALL                      16.0M    1008K    1008K       0K       0K       0K       0K        2         see MALLOC ZONE table below
MALLOC_TINY                       4096K    1600K    1600K       0K       0K       0K       0K        4         see MALLOC ZONE table below
SQLite page cache                  576K     288K     288K       0K     256K      32K       0K        9
STACK GUARD                        256K       0K       0K       0K       0K       0K       0K       16
Skywalk Networking                4864K     224K     224K       0K       0K     224K       0K       18
Stack                             9184K     544K     496K       0K       0K       0K       0K       17
Stack (reserved)                  1088K       0K       0K       0K       0K       0K       0K        2         reserved VM address space (unallocated)
Stack Guard                         32K       0K       0K       0K       0K       0K       0K        2
VM_ALLOCATE                        1.0G    2064K    2064K       0K       0K       0K       0K       11
VM_ALLOCATE (reserved)              16K       0K       0K       0K       0K       0K       0K        1         reserved VM address space (unallocated)
__DATA                            11.4M    7705K    2024K       0K       0K       0K       0K      408
__DATA_CONST                      31.1M    25.1M      37K       0K       0K       0K       0K      404
__DATA_DIRTY                      1271K    1095K     561K       0K       0K       0K       0K      322
__FONT_DATA                          4K       0K       0K       0K       0K       0K       0K        1
__LINKEDIT                       197.0M    6880K       0K       0K       0K       0K       0K       21
__LLVM                            61.3M       0K       0K       0K       0K       0K       0K       16
__LLVM_COV                          96K       0K       0K       0K       0K       0K       0K        5
__OBJC_RO                         90.5M    38.0M       0K       0K       0K       0K       0K        1
__OBJC_RW                         3392K    2112K      16K       0K       0K       0K       0K        1
__TEXT                           374.0M   118.9M      80K       0K       0K       0K       0K      435
__UNICODE                          588K     544K       0K       0K       0K       0K       0K        1
dyld private memory               1024K      32K      32K       0K       0K       0K       0K        1
libnetwork                        1152K      48K      48K       0K       0K      48K    1008K       16
mapped file                       28.8M    1952K      80K       0K       0K       0K       0K        7
shared memory                       48K      48K      48K       0K       0K       0K       0K        3
unused but dirty shlib __DATA      194K     194K     194K       0K       0K       0K       0K       70
===========                     ======= ========    =====  ======= ========   ======    =====  =======
TOTAL                              1.8G   209.1M     9.9M       0K     256K     336K    1008K     1859
TOTAL, minus reserved VM space     1.8G   209.1M     9.9M       0K     256K     336K    1008K     1859

                                 VIRTUAL   RESIDENT      DIRTY    SWAPPED ALLOCATION      BYTES DIRTY+SWAP          REGION
MALLOC ZONE                         SIZE       SIZE       SIZE       SIZE      COUNT  ALLOCATED  FRAG SIZE  % FRAG   COUNT
===========                      =======  =========  =========  =========  =========  =========  =========  ======  ======
DefaultMallocZone_0x1010b8000      19.2M      3600K      3600K         0K      16145      3430K       170K      5%      48
NWMallocZone_0x109aa4000           2048K        64K        64K         0K         39         5K        59K     93%       2
===========                      =======  =========  =========  =========  =========  =========  =========  ======  ======
TOTAL                              21.2M      3664K      3664K         0K      16184      3434K       230K      7%      50
  • Get memory regions occupied by MALLOC_LARGE:
vmmap QNetworkExtension\[1420\].memgraph | grep 'MALLOC_LARGE'

OUTPUT:

REGION TYPE                    START - END         [ VSIZE  RSDNT  DIRTY   SWAP] PRT/MAX SHRMOD PURGE    REGION DETAIL
MALLOC_LARGE                106360000-106368000    [   32K    32K    32K     0K] rw-/rwx SM=PRV          DefaultMallocZone_0x1010b8000
MALLOC_LARGE metadata       106368000-10636c000    [   16K    16K    16K     0K] rw-/rwx SM=PRV          DefaultMallocZone_0x1010b8000
MALLOC_LARGE                10636c000-106374000    [   32K    32K    32K     0K] rw-/rwx SM=PRV          DefaultMallocZone_0x1010b8000
MALLOC_LARGE                106374000-106378000    [   16K    16K    16K     0K] rw-/rwx SM=PRV          DefaultMallocZone_0x1010b8000

heap

List all the malloc-allocated buffers in the process's heap

Some interesting flags

  • Get diff objects allocated between memory graph and sort them by size:
heap --sortBySize --diffFrom=Memgraph-pre.memgraph Memgraph-post.memgraph

OUTPUT:
Hardware Model:  iPhone9,3
Process:         *****
Path:            *****
Load Address:    0x100cb4000
Identifier:      *****
Version:         ???
Code Type:       ARM64
Platform:        iOS
Parent Process:  debugserver [1424]

Date/Time:       2022-01-18 10:04:31.765 +0100
Launch Time:     2022-01-18 10:02:49.346 +0100
OS Version:      iPhone OS 15.0 (19A346)
Report Version:  104

Physical footprint:         17.1M
Physical footprint (peak):  17.9M
----

Only showing allocations in Memgraph-post.memgraph that are not in Memgraph-pre.memgraph.

Process 1420: 2 zones

All zones: 41590 nodes malloced - Sizes: 112KB[1] 80KB[4] 32KB[75] 16KB[6] 15KB[1] 6.5KB[2] 5.5KB[2] 5KB[2] 4KB[57] 3.5KB[3] 2.5KB[27] 2KB[149] 1.5KB[33] 1KB[6] 1008[3] 992[2] 928[2] 912[3] 896[11] 880[3] 864[4] 848[427] 800[1] 784[3] 768[26] 752[3] 736[11] 720[4] 704[2] 672[19] 640[71] 624[2] 608[40] 592[2] 576[1] 560[5] 544[12] 528[44] 512[13] 480[9] 464[74] 448[25] 432[10] 416[33] 400[39] 384[91] 368[162] 352[51] 336[58] 320[35] 304[15] 288[69] 272[211] 256[422] 240[69] 224[308] 208[142] 192[232] 176[459] 160[789] 144[444] 128[949] 112[849] 96[4111] 80[1372] 64[5734] 48[13317] 32[7575] 16[2854]

Found 101 ObjC classes
Found 28 Swift classes
Found 11 CFTypes

-----------------------------------------------------------------------
All zones: 41590 nodes (7136320 bytes)

   COUNT      BYTES       AVG   CLASS_NAME                                        TYPE    BINARY
   =====      =====       ===   ==========                                        ====    ======
    7116    3636464     511.0   non-object
   13260     723168      54.5   Swift closure context                             Swift   <unknown>
     553     622544    1125.8   _DictionaryStorage<String, Any>                   Swift   libswiftCore.dylib
    4411     564448     128.0   CFString                                          ObjC    CoreFoundation
    1674     311264     185.9   __StringStorage                                   Swift   libswiftCore.dylib
    2709     173376      64.0   _ContiguousArrayStorage<String>                   Swift   libswiftCore.dylib
    3263     156624      48.0   __NSMallocBlock__                                 ObjC    libsystem_blocks.dylib
       2     115280   57640.0   NSConcreteMutableData (Bytes Storage)             C       Foundation
       6      98304   16384.0   Swift Metadata                                    C       libswiftCore.dylib
       ........
  • Another example is we want to check all allocated object that takes more than 1k:
heap --addresses=non-object\[1k-\] Memgraph.memgraph

  • For example we want to see the incrementation in bytes of a specifyc target:
heap --diffFrom=Memgraph-pre.memgraph Memgraph-post.memgraph | grep 'MyFramework' | awk '{sum+=$2;} END {print "Total size: " sum " bytes";}'

OUTPUT:
Total size: 53088 bytes

leaks

Search a process's memory for unreferenced malloc buffers

Some interesting flags

  • Get a list of leaks:
leaks --list Memgraph.memgraph

OUTPUT:
Hardware Model:  iPhone9,3
Process:         ****
Path:            ****
Load Address:    0x100cb4000
Identifier:      ****
Version:         ???
Code Type:       ARM64
Platform:        iOS
Parent Process:  debugserver [1424]

Date/Time:       2022-01-18 10:11:08.826 +0100
Launch Time:     2022-01-18 10:02:49.346 +0100
OS Version:      iPhone OS 15.0 (19A346)
Report Version:  104

Physical footprint:         17.9M
Physical footprint (peak):  23.5M
----

leaks Report Version: 3.0
Process 1420: 40991 nodes malloced for 5506 KB
Process 1420: 5 leaks for 10944 total leaked bytes.

Leak: 0x1064242c0  size=64  zone: DefaultMallocZone_0x1010b8000   CFString  ObjC  CoreFoundation
Leak: 0x106446200  size=16  zone: DefaultMallocZone_0x1010b8000   CFString (Storage)  C  CoreFoundation
Leak: 0x10646a760  size=48  zone: DefaultMallocZone_0x1010b8000   CFString  ObjC  CoreFoundation
Leak: 0x10646a790  size=64  zone: DefaultMallocZone_0x1010b8000   CFString  ObjC  CoreFoundation
Leak: 0x107088a00  size=10752  zone: DefaultMallocZone_0x1010b8000   _TtCs12_SwiftObject  ObjC  libswiftCore.dylib
  • Get reference tree and group them:
leaks --referenceTree --groupByType Memgraph.memgraph

malloc_history

Show the malloc and anonymous VM allocations that the process has performed. Needs MallocStackLogging enabled. (Doesn't work for Network extension).

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment