public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "carlos at redhat dot com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug malloc/26969] A common malloc pattern can make memory not given back to OS
Date: Tue, 01 Dec 2020 02:51:10 +0000	[thread overview]
Message-ID: <bug-26969-131-Qjr9ozAd7i@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-26969-131@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=26969

Carlos O'Donell <carlos at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |carlos at redhat dot com
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |NOTABUG

--- Comment #1 from Carlos O'Donell <carlos at redhat dot com> ---
The glibc implementation of malloc is a heap-based allocator and in that design
the heap must be logically freed back down in the order that it was originally
allocated or the heap will continue to grow to keep a maximum working set of
chunks for application.

If you want to free back down to zero at the last deallocation you must tune
the allocator by disabling fastbins and tcache.

For example:
- Allocate A
- Allocate B
- Allocate C
- Free A
- Free B
- Free C

Consider A, B and C are all the same size.

Until "Free C" happens the entire stack is held at 3 objects deep.

This can happen because tcache or fastbins holds the most recently freed chunk
for re-use. There is nothing wrong with this strategy because the C library,
apriori, doesn't know if you'll carry out this entire workload again.

The worse-case degenerate situation for tcache is a sequence of allocations
which cause tcache to always hold the top-of-heap chunks as in-use. In a real
program those chunks are refilled into the tcache much more randomly via malloc
from the unsorted bin or small bin refill strategy. Thus tcache should not keep
the top-of-heap from freeing down in those cases. It's only in synthetic test
cases like this where I think you see tcache being the blocker to freeing down
from the top of heap.

If you need to free pages between workloads and while idle you can call
malloc_trim() to release page-sized consolidated parts of the heaps.

If you need a minimal working set, then you need to turn off fastbins and
tcache.

One possible enhancement we can make is to split the heaps by pool sizes, and
that's something I've talked about a bit with DJ Delorie. As it stands though
that would be a distinct enhancement.

I'm marking this as RESOLVED/NOTABUG since the algorithm is working as intended
but doesn't meet your specific synthetic workload. If you have a real
non-synthetic workload that exhibits problems please open a bug and we can talk
about it and review performance and capture an API trace.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

  parent reply	other threads:[~2020-12-01  2:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-28 13:58 [Bug malloc/26969] New: " keyid.w at qq dot com
2020-11-28 13:59 ` [Bug malloc/26969] " keyid.w at qq dot com
2020-11-29  3:24 ` keyid.w at qq dot com
2020-12-01  1:03 ` uwydoc at gmail dot com
2020-12-01  2:51 ` carlos at redhat dot com [this message]
2020-12-01  8:43 ` keyid.w at qq dot com
2021-01-29 16:08 ` dimahabr at gmail dot com
2021-02-01  8:52 ` keyid.w at qq dot com
2022-06-29 16:34 ` romash at rbbn dot com

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=bug-26969-131-Qjr9ozAd7i@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).