0

I am allocating memory using "huge pages(1MB size)" and using mmap. After allocating 4 GB of memory ,mmap returns fail.

mmap(NULL, memsize, PROT_READ | PROT_WRITE,MAP_PRIVATE | MAP_ANONYMOUS |MAP_POPULATE | MAP_HUGETLB, -1, 0);

here memsize = 1GB

I am calling above statement in a loop. Upto 4 iterations it is fine. In 5th iteration mmap is failed.

mmap(NULL, memsize, PROT_READ | PROT_WRITE,MAP_PRIVATE | MAP_ANONYMOUS |MAP_POPULATE , -1, 0);

Above statement (without hugepages) works perfectly for any number of iterations. Am I missing any information related to hugepages? I tried "MAP_NORESERVE" flag also as mentioned in mmap fail after 4GB.

Any sort of information will be greatly appreciated. Thank you.

Community
  • 1
  • 1
ANTHONY
  • 333
  • 5
  • 18

1 Answers1

2

Change the allocated "number of huge pages" in file

 /proc/sys/vm/nr_hugepages

according to the amount of memory you want to allocate. Earlier it says:

>cat /proc/meminfo | grep HugePages 
 HugePages_Total = 2500

4GB => it has 2048*2Mb= 4Gb

2048 huge pages already consumed.

one more GB of memory need (1GB/2MB= 512) 512 more huge pages. But 2500 - 2048 =452 only left. But you need 512. Thats the problem why mmap failed. If you edit the above mentioned file(/proc/sys/vm/nr_hugepages) contents to 2560, it allows 5GB. Change it according to the amount of memory you need. Thanks to @ Klas Lindbäck. I referred back the link, small research exposed the working

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
ANTHONY
  • 333
  • 5
  • 18