Date: Wed, 15 Dec 2021 10:10:21 -0500 From: Rich Felker <dalias@...c.org> To: tugouxp <13824125580@....com> Cc: musl@...ts.openwall.com Subject: Re: Did the musl libc never decrease the brk pointer of Kernel? just increase ? On Wed, Dec 15, 2021 at 05:30:23PM +0800, tugouxp wrote: > Hi guys: > > > i found i intresting things when i fix a memory leak issue on may > platform which based on musl c library. the issue has been fixed but > a puzzle leave it to me. in the file of malloc/mallocng/malloc.c, a > function called "alloc_meta" says that as belows,so you can see the > brk pointer of brk system call parmeter never decrease the brk, is > not it ? did gilibc also does like this way? why design like this, > thank you ! mallocng only uses the brk area for the (out of band) metadata structures, not actual storage space provided to the application. The amount of storage here is a small fraction of the peak memory usage during the process lifetime. Accounting for the unlikely possibility that the entire most-recently-added block of metadata storage becomes unneeded would add a lot more code complexity and additional data complexity, and somewhat de-harden the allocator, for basically no benefit. In general, "brk" is not a good system for allocating and returning memory because it can only return memory "at the end". mallocng allocates all application memory via mmap, which isn't subject to that limitation but allows abitrary page-granular units to be freed. The only reason we use brk at all is that it provides an independent ASLR zone whose location relative to both code and mmap-allocated memory is somewhat unpredictable. I'm not up to date on what current glibc malloc does, but historically it has used the brk area in the traditional way, to actually get storage to return to the application, and it can "trim" (reduce) it. But this is a very different usage. Rich
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.