I am currently looking into malloc()
implementation under Windows. But in my research I have stumbled upon things that puzzled me:
First, I know that at the API level, windows uses mostly the HeapAlloc()
and VirtualAlloc()
calls to allocate memory. I gather from here that the Microsoft implementation of malloc()
(that which is included in the CRT - the C runtime) basically calls HeapAlloc()
for blocks > 480 bytes and otherwise manage a special area allocated with VirtualAlloc()
for small allocations, in order to prevent fragmentation.
Well that is all good and well. But then there are other implementation of malloc()
, for instance nedmalloc, which claim to be up to 125% faster than Microsoft's malloc
.
All this makes me wonder a few things:
Why can't we just call HeapAlloc()
for small blocks? Does is perform poorly in regard to fragmentation (for example by doing "first-fit" instead of "best-fit")?
What makes nedmalloc
so much faster than Microsoft's malloc
?
From the above, I got the impression that HeapAlloc()
/VirtualAlloc()
are so slow that it is much faster for malloc()
to call them only once in a while and then to manage the allocated memory itself. Is that assumption true? Or is the malloc()
"wrapper" just needed because of fragmentation? One would think that system calls like this would be quick - or at least that some thoughts would have been put into them to make them efficient.
On average, how many (an order of magnitude) memory reads/write are performed by a typical malloc
call (probably a function of the number of already allocated segments)? I would intuitively says it's in the tens for an average program, am I right?
By default, when the new operator is used to attempt to allocate memory and the handling function is unable to do so, a bad_alloc exception is thrown.
Each process on 32-bit Microsoft Windows has its own virtual address space that enables addressing up to 4 gigabytes of memory. Each process on 64-bit Windows has a virtual address space of 8 terabytes.
Architecture for 32-bit Windows: The automatic configuration of the 32-bit Windows Operating System (OS) allocates 4 GB (232) of accessible memory space to the kernel and user programs equally. With 4 GB physical memory available, the kernel will receive 2 GB and the app memory will receive 2 GB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With