mailing list of musl libc
 help / color / mirror / code / Atom feed
* confusion about brk and mmap in expand_heap in malloc implementation
@ 2015-09-10 18:58 Yuxin Ren
  2015-09-10 20:58 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Yuxin Ren @ 2015-09-10 18:58 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 426 bytes --]

Hi,

I found in the __expand_heap function used by malloc, if brk fails, it will
use mmap to get a memory area.
I think in this case, this area should be returned back to system via
munmap.
But in my test, when I free such area, I found it is still passed to
madvise, not munmap.

How do we track if an area is expanded by brk or mmap?
And when we free an area, how do we decide to use munmap or madvise?

Thanks a lot.
Yuxin

[-- Attachment #2: Type: text/html, Size: 1584 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: confusion about brk and mmap in expand_heap in malloc implementation
  2015-09-10 18:58 confusion about brk and mmap in expand_heap in malloc implementation Yuxin Ren
@ 2015-09-10 20:58 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2015-09-10 20:58 UTC (permalink / raw)
  To: musl

On Thu, Sep 10, 2015 at 02:58:33PM -0400, Yuxin Ren wrote:
> Hi,
> 
> I found in the __expand_heap function used by malloc, if brk fails, it will
> use mmap to get a memory area.
> I think in this case, this area should be returned back to system via
> munmap.
> But in my test, when I free such area, I found it is still passed to
> madvise, not munmap.
> 
> How do we track if an area is expanded by brk or mmap?
> And when we free an area, how do we decide to use munmap or madvise?

I think you're starting from some mistaken assumptions. The managed
heap for small allocations (under a few hundred kb) is permanent for
the lifetime of the process once it's allocated, whether it comes from
brk or mmap. The only difference between the two approaches of
obtaining memory is that brk is faster and makes it possible to obtain
a contiguous range gradually rather than having to request large
amounts at a time to ensure contiguity.

Either way, what madvise does is free up the _physical_ memory backing
large contiguous free areas on the heap. No attempt is made to free
virtual address space (this would be a very bad idea; it could lead to
heavy fragmentation) or commit charge. I have some ideas for freeing
commit charge in the future, which may be desirable.

Rich


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-09-10 20:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-10 18:58 confusion about brk and mmap in expand_heap in malloc implementation Yuxin Ren
2015-09-10 20:58 ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).