develooper Front page | perl.perl5.porters | Postings from September 2014

Re: sv_grow() and malloc()

Thread Previous | Thread Next
Aaron Crane
September 23, 2014 14:02
Re: sv_grow() and malloc()
Message ID:
Dave Mitchell <> wrote:
> I haven't researched this properly yet (I'm secretly hoping I wont have to
> and someone already knows the answers).

I don't claim a great deal of expertise here, but as I understand it,
the glibc malloc switches to a different strategy for allocations in
excess of 128 KiB (tunable; see mallopt(3)). For small allocations, it
allocates an arena using sbrk(2), and manages it in the obvious sort
of way. For large allocations, it uses mmap(2) to create a private
mapping (which therefore has to be a multiple of the MMU page size,
typically 4096 bytes).

Further, strace(1) tells me that when glibc is asked to realloc a
large allocation, it uses mremap(2) to do so. This means that
extending large allocations can be done without actually copying the
data: the kernel merely rejigs the virtual memory mapping so that all
the pages (whether previously or newly allocated) form a contiguous
part of the process's virtual address space.

Aaron Crane **

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About