I'd like add the number of mapped memory regions to the status report of my daemon.
There's a number of reasons why you may want this:
- There's a limit for that (vm.max_map_count) so it's good to monitor the current value.
- A growing number may be a sign of an allocator bug resulting in unnecessary virtual memory fragmentation.
- A growing number may be a sign of leaking file mappings. They don't necessarily result in growing RSS and may not trigger an OOM. However, they pollute the VMA tree and may exceed max_map_count limit.
The question is: what it is the best way to get this number from the inside of your process?
One way is to count lines in /prof/self/map but it doesn't look optimal. Neither I want to parse a 10K lines text file to obtain a single integer nor the kernel needs to materialize the whole buffer each time.
There's already a counter in the kernel (mm_struct->map_count) so I'd expect it to be present somewhere in /proc/self/* but cannot actually find it. Any ideas?