Article View: borland.public.cppbuilder.nativeapi
Article #8598Re: malloc: where is the catch?
From: Alan Bellingham
Date: Thu, 07 Oct 2004 15:11
Date: Thu, 07 Oct 2004 15:11
30 lines
2478 bytes
2478 bytes
"Azazello" <snimi_potniki@hotmail.com> wrote: >Questions: >1. Can someone explain to me how malloc (serialized function, by the way) can be 33 (SIC!) times faster than heap functions and 11 times faster than VirtualAlloc (ok, VirtualAlloc zeros memory, but still, calloc is 2 times faster)? >2. Why Non-serialized/non-growable heap is twice slower than serialized/growable heap? Shouldn't it be other way around? >3. malloc seems to be too good to be true, so where is the catch? What does not malloc do compare to other functions? Question: could you please wrap your lines. Thanks. The standard strategy for a program to manage memory is for it to contain its own memory manger. This memory manager has one or more chunks of raw memory, which it originally got from the OS. When you call malloc() (or operator new(), or calloc(), etc.), this asks the memory manager for a chunk of raw memory. The memory manager checks to see if it's got a suitable free block, and if so, detaches it from what it knows to be free and returns it. If it hasn't got a suitable amount, then it asks the OS (using HeapAlloc or similar) for more, notes that it's all currently free, and goes back to the previous step. Calls into the OS will always be more expensive than internal calls. Since the OS is itself doing something similar to what your own memory manager is doing, but has to be very robust (doing things like mapping the memory into your process space), it _will_ be more expensive. Alan Bellingham -- ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK
Technical Details
Message-ID:
<hhmam0tmuhig13ajud7ncm06c922ovcho3@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4165469c$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,50f14b41a5262fc0 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: Alan Bellingham <alanb@episys.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: malloc: where is the catch? Date: Thu, 07 Oct 2004 16:11:40 +0100 Reply-To: alanb@episys.com Message-ID: <hhmam0tmuhig13ajud7ncm06c922ovcho3@4ax.com> References: <4165469c$1@newsgroups.borland.com> X-Newsreader: Forte Agent 2.0/32.652 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 193.129.251.130 X-Trace: newsgroups.borland.com 1097161899 193.129.251.130 (7 Oct 2004 08:11:39 -0700) Lines: 30 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail