mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: Re: malloc & uncommitting memory
Date: Thu, 3 Apr 2014 15:17:53 -0400	[thread overview]
Message-ID: <20140403191753.GE26358@brightrain.aerifal.cx> (raw)
In-Reply-To: <20140403201725.7509341f@MG>

On Thu, Apr 03, 2014 at 08:17:25PM +0200, M. Ziebell wrote:
> As far as i understand "uncommitted" memory is allocated address space.
> A region in memory which is not used by the program, but the address
> space expanded to make a smaller region available to the programm for
> use.
> Please correct me if I am wrong.

I'm not clear on exactly what you're asking so I'll try to just
clarify the issue. There are basically three types of memory usage
that matter:

- Virtual address space: Process-local resource. Aside from relatively
  low overhead in kernel bookkeeping, this only matters in that, if
  you run out of addresses, you can't allocate anything else.

- Anonymous pages: Global resource. These are pages that actually
  consume (rather than just temporarily using as cache) physical
  storage (ram or swap) because they're not backed by a file or device
  or the zero page or shared (via COW) with another mapping.

- Commit charge: Global resource. A superset of anonymous pages, also
  counting mapped pages that are presently shared (COW) with the zero
  page, a file, another process, etc. but which will need their own
  storage if/when they're written.

At present, musl's malloc can deallocate only the following:

* All three, only when resizing or freeing an allocation that was
  large enough to be serviced individually by mmap.

* Anonymous pages only, by calling madvise with MADV_DONTNEED to reset
  them to COW copies of the zero page when a large free range
  coalesces in the heap.

In particular, neither commit charge nor virtual address space from
the heap used for small allocations is ever freed. Avoiding freeing
the virtual address space is generally a good thing, since doing so
could result in pathological fragmentation. But not freeing the commit
charge means that an application which momentarily used a large amount
of memory by allocating a small bit at a time (think hideous tree/list
structures used in C++ and glib-based code) then freeing it all will
continue to tie part of the commit limit and reduce the amount of
memory available to other programs (note: this only applies with
overcommit disabled).

> Based on that understanding, the idea or your proposal sounds not
> really desirable.
> You have to take care of tons of exceptions and special cases.
> I'm pretty sure I don't understand what uncommitted memory is for, but
> I'm heary is hundreds of lines of complex code, if-else constructs
> everywhere and bugs.

The whole of malloc is under 600 lines, so anything measured in
"hundreds of lines" is definitely inappropriate as an addition.
However the basic concept (frozen chunk list, described in the first
mail) is at most tens of lines of code, and the strategies 1-3 for
avoiding fragmentation would each be somewhere on the same order of
magnitude, I think.

Rich


      reply	other threads:[~2014-04-03 19:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-02 21:08 Rich Felker
2014-04-03  4:43 ` Rich Felker
2014-04-03 18:17   ` M. Ziebell
2014-04-03 19:17     ` Rich Felker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140403191753.GE26358@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).