Here is something interesting about memory management in Linux: most

Arpit Bhayani

Arpit Bhayani

Jan 01, 2026 • 3 min read


Here is something interesting about memory management in Linux: most systems use 4KB pages by default. But, there is something called Transparent Huge Pages (THP) that configures your system to use 2MB pages instead. Here’s why it matters…

Every time the CPU needs to translate a virtual address to a physical one, it looks up the page table. This lookup is expensive, so there is a small cache called the Translation Lookaside Buffer that stores recent translations (yes, the one we studied in operating systems).

The problem? TLB is tiny - it can only hold a few hundred entries. So if each page is just 4KB, the TLB can only cache translations for a small slice of memory (128KB to 512KB).

But if each page is 2MB, suddenly the same number of TLB entries can cover 64MB to 256MB, which is 500x more memory accesses cached. Fewer TLB misses mean faster memory access.

When this setting is changed, the applications need not do anything; it is a pure OS setting change.

But here is the catch - THP can hurt database performance. To create these 2MB contiguous pages, the kernel sometimes needs to “compact” memory by shuffling pages around. This can stall your application for hundreds of milliseconds, sometimes even seconds.

Databases like MongoDB, Redis, and MySQL have sparse, random memory access patterns. They do not benefit from huge pages, and the compaction overhead just adds unpredictable latency.

Not only that, but if your application only needs 100KB, a 2MB page still gets allocated. That is 1.9MB wasted. For applications with lots of small, sparse allocations, this adds up quickly.

Also, when the system needs to swap out a huge page, it first has to split it back into 4KB pages. This splitting operation takes time and adds latency during memory pressure.

Even when a process forks, pages are shared until one process writes to them. With huge pages, a single byte write triggers a 2MB copy instead of a 4KB copy.

This is why most databases recommend disabling THP entirely. If you want to check your THP status, you can just fire the command

cat /sys/kernel/mm/transparent_hugepage/enabled

When I benchmarked it, I saw tremendous results for Spark workloads and Redis with large KV data. But the penalty when data is sparse is actually much worse.

THP is a classic trade-off. So, know your memory access patterns before you decide to turn it on.

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses