From patchwork Thu Nov 9 10:55:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 24180 Received: (qmail 14382 invoked by alias); 9 Nov 2017 10:55:39 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 14362 invoked by uid 89); 9 Nov 2017 10:55:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com To: GNU C Library , DJ Delorie From: Florian Weimer Subject: malloc: Trim unused arenas on thread exit Message-ID: Date: Thu, 9 Nov 2017 11:55:32 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 I tried the attached patch to trim unused arenas on thread exit. The trimming actually happens (the heap consolidation is visible in the malloc_info output from tst-malloc_info), but the arena heaps aren't deallocated. I think trimming unused arenas as much as possible is a good heuristics to minimize RSS, so getting this to work might be worthwhile. Thanks, Florian diff --git a/malloc/arena.c b/malloc/arena.c index 85b985e193..758226c222 100644 --- a/malloc/arena.c +++ b/malloc/arena.c @@ -953,12 +953,22 @@ arena_thread_freeres (void) /* If this was the last attached thread for this arena, put the arena on the free list. */ assert (a->attached_threads > 0); - if (--a->attached_threads == 0) + bool arena_is_unused = --a->attached_threads == 0; + if (arena_is_unused) { a->next_free = free_list; free_list = a; } __libc_lock_unlock (free_list_lock); + + /* If there are no more users, compact the arena as much as + possible. */ + if (arena_is_unused) + { + __libc_lock_lock (a->mutex); + mtrim (a, 0); + __libc_lock_unlock (a->mutex); + } } } text_set_element (__libc_thread_subfreeres, arena_thread_freeres); diff --git a/malloc/malloc.c b/malloc/malloc.c index 1f003d2ef0..a0b11784d2 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1831,7 +1831,7 @@ malloc_init_state (mstate av) static void *sysmalloc (INTERNAL_SIZE_T, mstate); static int systrim (size_t, mstate); static void malloc_consolidate (mstate); - +static int mtrim (mstate av, size_t pad); /* -------------- Early definitions for debugging hooks ---------------- */