Maximum number of mmap()’ed ranges and how to set it on Linux?

What’s the maximum number of mmap()‘ed ranges that a process can makes and how to set the limits on Linux?

I have a program that mmap()s and mprotect()s lots ranges. After allocating many ranges, mprotect() starts to fail with ENOMEM error number. From the man page, ENOMEM means 2 possible problems:

ENOMEM

Internal kernel structures could not be allocated.

ENOMEM

Addresses in the range [addr, addr+len-1] are invalid for the address space of the process, or specify one or more pages that are

not mapped.

I am sure that the mprotect()ed range is mapped and valid. So the problem is the first situation: internal kernel structure allocation failed.

That question is what is the limit here and how to enlarge the limit?

There is a kernel parameter vm.max_map_count that controls the number of ranges that you can mmap().

You can find the current limit by

$ sysctl vm.max_map_count

On my system, the default value is 65530:

vm.max_map_count = 65530

To change the maximum number of mapped ranges allowed to some number such as 655300:

# sysctl -w vm.max_map_count=655300

This changes the runtime parameter of the Linux kernel. To make the change permanent to take effect every time the Linux is rebooted, add a like to the /etc/sysctl.conf file like

vm.max_map_count=655300

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

2 comments:

  1. Pingback: Elasticsearch: administration | SnowCrash
Leave a Reply

Your email address will not be published. Required fields are marked *