我最近发现Linux不保证分配的内存mmap可以释放,munmap如果这导致VMA(虚拟内存区域)结构数超过的情况vm.max_map_count.Manpage(几乎)清楚地说明了这一点:
ENOMEM The process's maximum number of mappings would have been exceeded.
This error can also occur for munmap(), when unmapping a region
in the middle of an existing mapping, since this results in two
smaller mappings on either side of the region being unmapped.
Run Code Online (Sandbox Code Playgroud)
问题是Linux内核总是尝试合并VMA结构,munmap即使对于单独创建的映射也会失败.我能够编写一个小程序来确认这种行为:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
// value of vm.max_map_count
#define VM_MAX_MAP_COUNT (65530)
// number of vma for the empty process linked against libc …Run Code Online (Sandbox Code Playgroud)