4 ms·
Shouldn't huge pages be used automatically if you malloc() large amounts of memory at once? Wouldn't that cover some of the applications that benefit from it?
by phkahler 9y ago
Shouldn't huge pages be used automatically if you malloc() large amounts of memory at once? Wouldn't that cover some of the applications that benefit from it?
- zaarn 9y agomalloc() is higher level than what the kernel does. At the lower syscall level you move the BRK address, which is the highest memory address you're allowed to use. By default this is just after the statically initialized memory. malloc() is just a library that manages this memory for you. Linux has no idea if you will use the memory you just allocated, usually this happens dynamically; when you access a memory region for the first time, it is allocated in memory for real.
- myst 9y agoModern allocators use mmap(2).
- zaarn 9y agoThey do? Quite interesting. Tho it shouldn't really change the underlying mechanism; map only pages the process has touched.
- aduitsis 9y agoMay add, if you mmap(2) a memory segment (even a very large one, say 1Tb), nothing happens with regard to page mapping. It is not uncommon to see java tomcat processes allocating north of 80 Gb. But only a small percentage of these is actually used.
- loeg 9y agoWell, the page tables are initialized. Those aren't totally free, especially if a large mapping uses 4k pages.
- aseipp 9y agoI think the more fundamental issue is that transparent huge pages can break the assumed (key word) interface between the application and the system, and this can manifest in very non-obvious ways. Only the application knows the workload characteristics for using the pages it has reserved, and THP messes with assumptions you might be making otherwise. This turns it into a pessimization, overall. For example, if I map a bunch of pages of memory, use some, and then set MADV_DONTNEED on just a few of those pages afterwords (so they can be given back to the system temporarily), this will only work if I know the entire page is unused. If a page magically gets 512x larger under my feet, it's possible it will "coalesce" with another page -- into a huge page -- that can't be given back, because some of the (now much larger) page is still needed. This is the case of what happens with `jemalloc` + `redis`, where what looks like a memory leak is actually a failure to give back pages to the operating system, because small pages coalesce into huge ones automatically, defeating MADV_DONTNEED. Whether or not malloc(2) uses THP "under the hood" in this case is more an implementation decision (there are many malloc variants), but it just punts the problem down the road. Ultimately it manifests as a violation of the "unspoken protocol" between the kernel and application when managing memory mappings. All that said, there's some posts elsewhere in this thread pointing out that FreeBSD's Huge Page implementation avoids some of these deficiencies (possibly as a trade for some of its own), so all that said -- there's still room for better implementations, clearly: https://www.cs.rice.edu/~druschel/publications/superpages.pdf https://www.cs.rice.edu/~druschel/publications/superpages.pd...