Reason for doing malloc / bzero over calloc (performance)?

Ivan Voras ivoras at fer.hr
Thu Jun 14 22:14:11 UTC 2007


youshi10 at u.washington.edu wrote:

> Do you know if that's with malloc or calloc? What portion of the source
> demonstrates this?

No source, but here's a quote from
http://boredzo.org/blog/archives/2006-11-26/calloc-vs-malloc:

For large blocks (where "large" is surprisingly small, something like 14
KB) Mac OS X's default malloc() will always go to the kernel for memory
by calling vm_allocate(). vm_allocate() always returns zero-filled
pages; otherwise, you might get back a chunk of physical RAM or swap
space that had been written to by some root process, and you'd get
privileged data. So for large blocks, we'd expect calloc() and malloc()
to perform identically.

Mach will reserve some memory but not physically allocate it until you
read or write it. The pages can also be marked zero filled without
having to write zeros to RAM. But the first time you read from the page,
it has to allocate and then zero-fill it.


Google lead me to this:
http://developer.apple.com/documentation/Performance/Conceptual/ManagingMemory/Articles/MemoryAlloc.html

It's not conclusive:

For allocations greater than a few virtual memory pages, malloc uses the
vm_allocate routine to obtain a block of the requested size.The
vm_allocate routine assigns an address range to the new block in the
virtual memory space of the current process but does not allocate any
physical memory. Instead, the malloc routine pages in the memory for the
allocated block as it is used.
The granularity of large memory blocks is 4096 bytes, the size of a
virtual memory page. If you are allocating a large memory buffer, you
should consider making it a multiple of this size.
Note: Large memory allocations are guaranteed to be page-aligned.

but:

The calloc routine reserves the required virtual address space for the
memory but waits until the memory is actually used before initializing
it. This approach alleviates the need to map the pages into memory right
away. It also lets the system initialize pages as they’re used, as
opposed to all at once.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 250 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20070614/458e8ac2/signature.pgp


More information about the freebsd-hackers mailing list