Negative current commited stat with purge_delay=0

Open
#1,141 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
c

Research direction

Start by running the minimal C reproducer with mi_option_purge_decommits, mi_option_purge_delay, mi_collect, and mi_stats_get, then compare the reported committed.current value with the printed mappings. Review the existing discussion in duplicate issues #929 and #1118; done means the committed statistic no longer becomes negative or its documented meaning explains the result.

Written by the indexing model from the issue text.

Description

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
Dominant language
C
Stars
13.6k
Forks
1.2k
Avg merge
18h 12m
Merged PRs (30d)
20

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/mimalloc

All issues in microsoft/mimalloc

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.