Welcome! Log In Create A New Profile

Advanced

Virtual Memory Area fragmentation

Posted by Eugene Birukov 
Hello,

I am using Ubuntu Linux 15.10.

I am working with a program that is paranoid about managing its own memory. So, that program uses mmap by specifying specific address and commits two pages at a time in the regions 512KiB apart. Eventually all the memory is used. What surprises me, is that memory map reported by /proc/self/maps still shows separate 512KiB regions. I.e. vma_merge() did not stitch them together when gap is filled. This is unfortunate because eventually we run out of memory region number limit.

Is there any way to fix the situation?

Here is a small program that demonstrates the issue.

#include <stdio.h>
#include <sys/mman.h>

size_t req = 0x587180000;

void map_regions(size_t start, size_t stop)
{
void* adr;
for (size_t i = start; i < stop; ++i)
{
for (size_t j = 0; j < 0x10; ++j)
{
adr = mmap((void*)(req + j * 0x80000), 0x2000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
*(int*)adr = 0;
}
req += 0x2000;
printf("%ld %p\n", i, adr);
}
}

int main(int argc, char** argv)
{
map_regions(0, 0x40);
printf("done\n"winking smiley;
}

The resulting map:

587180000-587200000 rw-p 00000000 00:00 0
587200000-587280000 rw-p 00000000 00:00 0
587280000-587300000 rw-p 00000000 00:00 0
587300000-587380000 rw-p 00000000 00:00 0
587380000-587400000 rw-p 00000000 00:00 0
587400000-587480000 rw-p 00000000 00:00 0
587480000-587500000 rw-p 00000000 00:00 0
587500000-587580000 rw-p 00000000 00:00 0
587580000-587600000 rw-p 00000000 00:00 0
587600000-587680000 rw-p 00000000 00:00 0
587680000-587700000 rw-p 00000000 00:00 0
587700000-587780000 rw-p 00000000 00:00 0
587780000-587800000 rw-p 00000000 00:00 0
587800000-587880000 rw-p 00000000 00:00 0
587880000-587900000 rw-p 00000000 00:00 0
587900000-587980000 rw-p 00000000 00:00 0

Thanks,
Eugene
Author:

Your Email:


Subject:


Spam prevention:
Please, solve the mathematical question and enter the answer in the input field below. This is for blocking bots that try to post this form automatically.
Question: how much is 21 plus 2?
Message: