microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
9.74k stars 793 forks source link

Any way to know how much memory was freed but not yet purged? #824

Open chakaz opened 7 months ago

chakaz commented 7 months ago

In my program I need to be able to track memory usage, and as part of that I need to know how much of the RSS memory is actually reusable. As mimalloc doesn't immediately purge freed segments (by default, and that's ok), I was wondering if there is any way to determine how much memory that is.

For example, my program shows these stats:

___ Begin mimalloc statistics ___
heap stats:     peak       total       freed     current        unit       count
  reserved:     3.0 GiB     3.0 GiB     0           3.0 GiB
 committed:     2.9 GiB     3.0 GiB    19.8 MiB     2.9 GiB
     reset:     0
    purged:    19.8 MiB
   touched:     4.0 MiB     5.0 MiB     1.0 GiB    -1.0 GiB                          ok
  segments:    80          80          38          42                                not all freed!
-abandoned:     0           0           0           0                                ok
   -cached:     0           0           0           0                                ok
     pages:     0           0           2.4 Ki     -2.4 Ki                           ok
-abandoned:     0           0           0           0                                ok
 -extended:     0
 -noretire:     0
     mmaps:     0
   commits:     0
    resets:     0
    purges:    17
   threads:     1           1           0           1                                not all freed!
  searches:     0.0 avg
numa nodes:     1
   elapsed:    51.823 s
   process: user: 0.381 s, system: 0.823 s, faults: 3, rss: 1.9 GiB, commit: 2.9 GiB

Arena statistics from thread:0
Count BlockSize Reserved Committed Used
1 20480 512000 81920 20480
2500 114688 458752 458752 458752
1 196608 196608 196608 196608
1 64 65344 4096 64
1 16 65488 32768 30640
1 1024 65536 4096 1024
1 8 65512 4096 32
1 24576 516096 98304 24576
1 32768 524288 262144 262144
--- End mimalloc statistics, took 20486us ---
total reserved: 1148890872, comitted: 1147564032, used: 1147415568 fragmentation waste: 0.0129373%

And after I run mi_collect(true) it shows these:

___ Begin mimalloc statistics ___
heap stats:     peak       total       freed     current        unit       count
  reserved:     3.0 GiB     3.0 GiB     0           3.0 GiB
 committed:     2.9 GiB     3.0 GiB     1.0 GiB     1.9 GiB
     reset:     0
    purged:     1.0 GiB
   touched:     4.0 MiB     5.0 MiB     1.0 GiB    -1.0 GiB                          ok
  segments:    80          80          39          41                                not all freed!
-abandoned:     0           0           0           0                                ok
   -cached:     0           0           0           0                                ok
     pages:     0           0           2.4 Ki     -2.4 Ki                           ok
-abandoned:     0           0           0           0                                ok
 -extended:     0
 -noretire:     0
     mmaps:     0
   commits:     0
    resets:     0
    purges:    20
   threads:     1           1           0           1                                not all freed!
  searches:     0.0 avg
numa nodes:     1
   elapsed:  1566.539 s
   process: user: 5.388 s, system: 4.141 s, faults: 3, rss: 1.9 GiB, commit: 2.9 GiB

Arena statistics from thread:0
Count BlockSize Reserved Committed Used
1 20480 512000 81920 20480
2500 114688 458752 458752 458752
1 196608 196608 196608 196608
1 64 65344 4096 64
1 16 65488 32768 30640
1 1024 65536 4096 1024
1 8 65512 4096 32
1 24576 516096 98304 24576
1 32768 524288 262144 262144
--- End mimalloc statistics, took 294us ---
total reserved: 1148890872, comitted: 1147564032, used: 1147415568 fragmentation waste: 0.0129373%

Where could I have detected these 1gb that was collected? (the diff of current-committed)