Glossary

Memory Allocation

Memory allocation is how a program asks for memory while it runs and gives it back when done. The allocator in between decides where each request goes and how fast it is served.

What is memory allocation?

Programs rarely know in advance how much memory they will need: a request body could be 1 KB or 50 MB, a result set ten rows or ten million. So they ask for memory at run time, use it and release it. The layer that hands out and takes back these pieces is the allocator. It sits between the program and the kernel, requesting large regions from the kernel and carving them into the many small blocks programs ask for. Fast, well-behaved allocation is a quiet but large part of server performance.

Stack or heap?

StackHeap
What goes thereLocal variables, call framesObjects whose size or lifetime is decided at run time
Allocation costNearly free, move a pointerAn allocator call, from nanoseconds to more
LifetimeEnds when the function returnsUntil freed, or until the garbage collector reclaims it
SizeSmall, usually 8 MB per thread by defaultLimited by RAM and settings

Each thread has its own stack; check the default with ulimit -s. Deep recursion or large local arrays can overflow it, which crashes the thread. Everything bigger or longer-lived goes on the heap.

How does the allocator get memory from the kernel?

Two system calls do most of the work. brk grows the classic heap area in one direction, and mmap maps fresh regions anywhere in the address space. glibc's malloc uses the heap for small requests and separate mmap regions for large ones (128 KB and up by default). The kernel only reserves address space at that point; physical pages are assigned when the program first touches them. That is why allocation of a big buffer is fast, and why reported virtual size can be much larger than real use, as Virtual memory explains.

What about garbage-collected languages?

Java, Go, .NET, Node.js and Python manage memory for you. The runtime allocates objects quickly from its own heap and a garbage collector frees those no longer reachable. The trade-offs move elsewhere: heap size settings (-Xmx for the JVM, --max-old-space-size for Node, GOMEMLIMIT for Go), pause times, and memory held by the runtime rather than returned to the OS. In containers, set these limits below the container's memory limit, or the process may be killed before its collector reacts.

What is fragmentation?

After many allocations and frees of different sizes, free memory can end up in small scattered holes. The process holds more memory than its live data needs, and RSS creeps upward over days even without a leak. Long-running servers with mixed workloads, such as Redis, MySQL or Ruby apps, can see this. Remedies include alternative allocators, restarting workers periodically (PHP-FPM's pm.max_requests), and built-in defragmentation where offered, like Redis activedefrag.

Which allocator should a server use?

glibc malloc is solid for most programs. jemalloc and tcmalloc reduce fragmentation and scale better with many threads, which is why several databases and caches bundle one. You can try one without recompiling:

apt install libjemalloc2
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 /usr/local/bin/myservice
MALLOC_ARENA_MAX=2 /usr/local/bin/myservice    # glibc: fewer arenas, less memory on many-thread hosts

On Arm models such as A2, the library lives under aarch64-linux-gnu instead. Measure RSS and latency before and after; the effect varies by program. On machines with many cores, such as X9 with 96 threads, glibc's default of many arenas can use noticeably more memory, which MALLOC_ARENA_MAX trims.

How do I find a memory leak on a server?

First confirm it: graph the process's RSS or PSS over days. A sawtooth that resets is normal; a line that only climbs is a leak or fragmentation. Then narrow it down:

  • Compare /proc/PID/smaps_rollup snapshots to see whether heap or mappings grow.
  • Use language tools: heap dumps for the JVM, pprof for Go, tracemalloc for Python, heap snapshots for Node.
  • For C and C++, run a staging copy under Valgrind or with jemalloc profiling, or attach heaptrack.

Until a fix ships, a cgroup memory limit with automatic restart contains the damage; Out of memory (OOM) shows how. Plenty of physical memory, as on M2 with 512 GB DDR4, buys time but does not cure a leak.

Machines for Memory-Hungry Services

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
S5Intel Xeon E-2276G6 / 12128 GB DDR42x 1 TB NVMe2x 10 Gbps$88.72$88.72 (1 month)
S7Intel Xeon E-2288G8 / 16128 GB DDR42x 1 TB NVMe2x 10 Gbps$100.72$100.72 (1 month)
X9Intel Xeon 6731E96 / 96512 GB DDR52x 4 TB NVMe2x 25 Gbps$1,104.71$1,104.71 (1 month)
M22x Intel Xeon Gold 531724 / 48512 GB DDR42x 4 TB NVMe2x 25 Gbps$435.32$435.32 (1 month)
A2Ampere Altra Q80-3080 / 80256 GB DDR42x 4 TB NVMe2x 25 Gbps$277.57$277.57 (1 month)

Keep Reading

Virtual memory

How every process gets its own private address space, how pages map to RAM or swap, and why VSZ and RSS numbers differ so much.

Open

Out of memory (OOM)

What happens when Linux runs out of RAM, how the OOM killer picks a victim, how to read its log lines and how to keep it from striking.

Open

Linux memory management

Virtual memory, pages, overcommit, huge pages, cgroup limits and the OOM killer: how Linux hands out RAM and the knobs worth turning.

Open

Check memory usage

Read free, /proc/meminfo, vmstat and per-process numbers correctly, and tell real memory pressure apart from a healthy page cache.

Open

FAQ

Quick Answers

The stack holds local variables and is freed when a function returns. The heap holds objects whose size or lifetime is decided at run time.

It returns a block of memory of the requested size from the allocator, which gets larger regions from the kernel with brk or mmap.

Fragmentation can leave small unused holes the allocator cannot return. Alternative allocators or periodic worker restarts help.

It often lowers fragmentation and scales well with threads. Test with LD_PRELOAD and compare memory and latency.

Track the process RSS or PSS over several days. Memory that only grows, never levels off, points to a leak or fragmentation.