TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Does malloc lazily create the backing pages for an allocation on Linux? (2009)

38 pointsby tzhenghaoover 5 years ago

4 comments

blibbleover 5 years ago
it&#x27;ll call mmap (probably), which will then find 1GB(+ overhead) of contiguous address space<p>mmap will go and create the page table mappings (entries) for that address space (~262144 of them with 4k pages)<p>unless someone&#x27;s been messing with overcommit: the physical memory allocation is normally lazy<p>this is done by setting the &quot;faulted&quot; bit on the allocated page table entries<p>when the application first reads&#x2F;writes from a page not yet allocated: a fault will occur, the kernel will step in, (hopefully) find an unused physical 4kb of memory, update the page table entry to point to it, unmark the faulted bit, then return to the application (which will be blissfully unaware that anything happened)
评论 #21666126 未加载
评论 #21666098 未加载
评论 #21666261 未加载
评论 #21666731 未加载
OskarSover 5 years ago
Suppose you WANT malloc to be eager (i.e. make sure all the pages are committed right away, so you don&#x27;t pay a performance penalty for it later), what do you do?<p>The obvious answer is just looping through it and setting it to zero would be replaced with a call to `calloc` by an optimizing compiler, and doesn&#x27;t calloc also do some kind of similar &quot;lazy&quot; trick? So what do you do? Mark the pointer as volatile and loop through it and set all the memory to 0 (or some other value)? Call memset, I guess?<p>EDIT: apparently malloc + memset compiles to calloc as well [0], so if you want to allocate, zero out memory, and make sure it&#x27;s committed, using volatile seems like the best bet to me...<p>[0]: <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;qhaaYc" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;qhaaYc</a>
评论 #21667311 未加载
评论 #21667164 未加载
评论 #21667165 未加载
评论 #21667236 未加载
zabzonkover 5 years ago
malloc() is a C (and C++) Standard Library function that you could write user-level code for yourself (in fact, there&#x27;s an extended exercise in TCPL to do just that). It doesn&#x27;t do the low-level handling of pages - that&#x27;s down to the Linux kernel.
评论 #21666725 未加载
评论 #21665944 未加载
lightedmanover 5 years ago
Is this why when I create an encrypted RAM drive that Linux shits itself? I assume that it should not fill itself up yet it does almost instantly and brings the system, down to its knees.
评论 #21667331 未加载