📄 lazyalloc2.html
字号:
<html><!-- Mirrored from c-faq.com/malloc/lazyalloc2.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:58 GMT --><head><title></title></head><body>[Someone asked me what the``big controversy, question, secret, or otherwise in this question''was,correctly noting that the answer is somewhat wishy-washy,and wondering what the point of the question was.This was my reply.]<p>From: Steve Summit<br>Date: Mon, 15 Oct 2001 21:48:01 -0400<br>Subject: Re: One of your C.L.C FAQ Questions<br>Message-Id: <2001Oct15.2148.scs.001@aeroroot.scs.ndip.eskimo.net><p>[The point of the question is]Mostly trying to lay an old, recurring flame war to rest.<p>Part 1 of the story is that, once upon a time, someone allegedlydiscovered that many programs malloc far more memory than theyever actually use. But the kernel has to allocate VM (physicalmemory and/or backing store in swap space) for the memory anyway,because of course the program <em>might</em> use it; the kernel has noway of knowing. This can (and this is still ``allegedly''; Ihaven't personally seen these reports) lead to lots of ``wasted''swap space, with deleterious effects if swap space is prematurelyexhausted.<p>Part 2 of the story is that some versions of Unix (I believeincluding AIX and IRIX) have adopted a ``lazy'' allocationstrategy. When a program calls malloc (or, more precisely, whenmalloc calls brk to extend the program's data space), the kerneldoes <em>not</em> actually allocate any memory right away, neither inphysical memory nor swap space. <em>If</em> the program tries to usethe memory, a page fault results, and when handling it, thekernel discovers that there's no backing store for that page yet,and allocates it on the fly.<p>But part 3 of the story is that there's a potential problem here:what if, at that point, there isn't any free swap space to behad? The kernel is now in a bind: it has promised the callingprogram it can use the memory (that promise was made when thecall to malloc/brk succeeded), but now the kernel might have torenege on that promise.<p>Part 4 of the story is that, under kernels which support this``lazy'' allocation strategy, there's a new signal or two whichthe kernel can send. It can, in effect, say to a process:``Oh, shoot, I'm out of swap space, deal with it somehow''.There may be two of these signals, one nonfatal one whichrequests that the program deallocate some memory somewhereto free up some swap space so that the kernel can satisfy thepage fault, and a second, fatal one which is sent after thereclamation attempts fail. It's not clear to me whether thesesignals are sent to the process which has just suffered theunsatisfiable page fault, or to some <em>other</em> process that thekernel would like to swipe some memory or swap space back from.<p>Part 5 of the story is that there are some people (myselfincluded) who think that this is a lousy strategy, no matter howwell-intentioned it is or how real the overallocated-unused swapspace problem was. When malloc succeeds, it means I've got thememory, darn it, and if I later get killed by a signal becausethe kernel ``overbooked the flight'', so to speak, I'm seriouslyannoyed. Sure, I could catch the relevant signals to protectmyself (i.e., at the very least, to save any vital in-memorydata to disk before the second, fatal signal kills me), butthat's a surprising, non-default thing for me to have to do,and unfortunately the default on such a system, if I don't go outof my way to protect myself (or if I don't even know I have to)seems to be that I can lose.<p>People who dislike the lazy allocation strategy would like tobe able to point at the C Standard and say, ``Look, if mallocsucceeds, I'm supposed to be able to use that memory; aconforming implementation isn't allowed to renege on thepromise.'' But part 6 of the story is that the defenders of thelazy allocation strategy can themselves point at the Standardand say, ``Look, there's nothing in there disallowing it''.And they're right.</body><!-- Mirrored from c-faq.com/malloc/lazyalloc2.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:58 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -