mimalloc icon indicating copy to clipboard operation
mimalloc copied to clipboard

Negative current commited stat with purge_delay=0

Open dranikpg opened this issue 3 months ago • 0 comments

Duplicate of #929 #1118, but I want to bump this issue up so it gains more attention. With mi_option_purge_decommits=1 and mi_option_purge_delay=0, the current commited stat becomes negative after calling mi_collect. I first assumed that this is the current amount of memory that is not yet decommited (relative to total), but it doesn't add up. So its either a bug or an undocumented feature.

Running with latest master:

#include <stdio.h>
#include <mimalloc.h>
#include <mimalloc-stats.h>

int main() {
  const int num_elements = 1000;
  void* ptrs[num_elements];

  mi_process_init();
  mi_option_set(mi_option_purge_decommits, 1);
  mi_option_set(mi_option_purge_delay, 0);
  mi_option_set(mi_option_arena_reserve, 1024000); // 1GB

  for (int i = 0; i < num_elements; i++) {
    ptrs[i] = mi_malloc(50000);
  }
  for (int i = 0; i < num_elements; i++) {
    mi_free(ptrs[i]);
  }


  mi_collect(true);

  mi_stats_t stats;
  mi_stats_get(sizeof(stats), &stats);
  printf("%d mb", stats.committed.current / 1024 / 1024);
  mi_stats_print_out(NULL, NULL);
}
[100%] Built target test
heap stats:     peak       total     current       block      total#   
  reserved:     1.0 GiB     1.0 GiB     1.0 GiB                          
 committed:     1.0 GiB     1.0 GiB  -906.8 MiB                          
     reset:     0      
    purged:     1.8 GiB
   touched:   192.7 KiB   192.7 KiB   -54.6 MiB                          
  segments:     3           3           1                                not all freed
-abandoned:     0           0           0                                ok
   -cached:     0           0           0                                ok
     pages:     0           0        -121                                not all freed
-abandoned:     0           0           0                                ok
 -extended:     0      
   -retire:     0      
    arenas:     1      
 -rollback:     0      
     mmaps:     1      
   commits:     0      
    resets:     0      
    purges:   123      
   guarded:     0      
   threads:     0           0           0                                ok
  searches:     0.0 avg
numa nodes:     1
   elapsed:     0.009 s
   process: user: 0.000 s, system: 0.009 s, faults: 0, rss: 63.5 MiB, commit: 1.0 GiB
-906 mb%
pmap -x 77734 | grep -e "total \| RSS"  
Address           Kbytes     RSS   Dirty Mode  Mapping
total kB         1051248    1868     220

dranikpg avatar Sep 23 '25 11:09 dranikpg