malloc() is a library function. In Linux, there are two syscalls that can be used to allocate new memory for a process:
sbrk() and
mmap() .
The maximum amount of memory a process can allocate, is limited by kernel-enforced per-process limits. See
man setrlimit and the
RLIMIT_AS and
RLIMIT_DATA limits for details. When Pluggable Authentication Modules (PAM) is used, those are often set using
pam_limits module; see
limits.conf somewhere under
/etc/ (the location varies from Linux distribution to distribution).
For a 32-bit process, the absolute maximum amount of memory a process can allocate depends on kernel configuration, and varies from 1 gigabyte to 3 gigabytes. This split is configurable when the kernel is compiled.
Because processes do not always actually use (access) all the memory they ask from the kernel, a policy called
overcommit is used by most Linux systems. This means that the kernel will allow allocations to exceed the total amount of RAM and SWAP available. This is a configurable setting, and in certain cases it is useful to turn it off, in which case the amount of RAM and swap available is the absolute limit for any new allocation.
For a 64-bit process, the maximum amount of memory a process can allocate depends on various factors. Normal allocations (for example, using
malloc()) are limited by the available RAM and swap, adjusted depending on the overcommit policy. File-backed allocations (memory-mapped files), however, can greatly exceed the amount of RAM and swap available. As an example,
here is a simple example program in C I wrote in an earlier thread, that creates a terabyte (1099511627776 bytes) memory map using a sparse file. For the application, it does look like it just had allocated a terabyte of RAM, as the kernel takes care of which pages (parts of the map) are in RAM, and which are stored in the file.
The maximum possible size of a memory map is limited by the available RAM, since the kernel needs to keep track of each page in the map, and these structures must stay in RAM. I believe the structures alone need about two gigabytes of RAM for each terabyte mapped (1:512 ratio). However, much sooner than than the RAM limit is approached, the usefulness of the map degrades. The kernel needs some RAM to keep most used pages of the map in RAM, or it will end up writing and reading each page to and from the disk for every access; this is at least a million times slower than normal operation! (If your accesses are mostly to the same page, the slowdown is much smaller.)
Therefore, on 64-bit Linux systems, the "absolute allocation maximum" is irrelevant, as it would be counterproductive to try to allocate that much. As a programmer, the fact that you can always allocate all available RAM, and/or map a file several dozen times the size of available RAM, should suffice.