12
Stack Exchange
votes

When malloc_trim is (was) called automatically from free in glibc's ptmalloc or dlmalloc?

Posted on 2/16/2017
UQ ID:385
Technology
Stack Overflow
Original

There is incorrectly documented function malloc_trim in glibc malloc (ptmalloc2), added in 1995 by Doug Lea and Wolfram Gloger (dlmalloc 2.5.4).

In glibc this function can return some memory freed by application back to operating system using negative sbrk for heap trimming, madvise(...MADV_DONTNEED) for unused pages in the middle of the heaps (this feature is in malloc_trim since 2007 - glibc 2.9, but not in systrim), and probably not by trimming additional thread arenas (which are not sbrk-allocated by mmaped as separate heaps).

Such function can be very useful for some long-running C++ daemons, doing a lot of threads with very high number of mixed size allocations, both tiny, small, middle and large in required size. These daemons may have almost constant amount of live (allocated by malloc and not yet freed) memory, but at the same time grow in RSS (physical memory consumption) over time. But not every daemon can call this function, and not every author of such daemons know that he should call this function periodically.

Man page of malloc_trim by Kerrisk http://man7.org/linux/man-pages/man3/malloc_trim.3.html (2012) says in Notes about automatic calls to malloc_trim from other parts of glibc malloc:

This function is automatically called by free(3) in certain circumstances.

(Also in http://www.linuxjournal.com/node/6390/print Advanced Memory Allocation, 2003 - Automatic trimming is done inside the free() function by callingmemory_trim())

This note is probably from source code of malloc/malloc.c which mentions in several places that malloc_trim is called automatically, probably from the free():

 736  M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
 737  to keep before releasing **via malloc_trim in free().**

 809  * When malloc_trim is called automatically from free(),
 810    it is used as the `pad' argument.

2722   systrim ... is also called by the public malloc_trim routine.

But according to grep for malloc_trim in glibc's malloc: http://code.metager.de/source/search?q=malloc_trim&path=%2Fgnu%2Fglibc%2Fmalloc%2F&project=gnu there are: declaration, definition and two calls of it from tst-trim1.c (test, not part of malloc). Same result for whole glibc grep (additionally it was listed in abilists). Actual implementation of malloc_trim is mtrim() but it is called only from __malloc_trim() of malloc.c.

So the question is: When malloc_trim or its internal implementation (mtrim/mTRIm) is called by glibc, is it called from free or malloc or malloc_consolidate, or any other function?

If this call is lost in current version of glibc, was it there in any earlier version of glibc, in any version of ptmalloc2, or in original dlmalloc code (http://g.oswego.edu/dl/html/malloc.html)? When and why it was removed? (What is the difference between systrim/sys_trim and malloc_trim?)

Keywords & Tags
malloc
heap-memory
glibc
Model Answers (9)
Submitted answers from various AI models
Unsolved
No reviews yet

K2-Think

Pending UQ Validation
Needs Human Verification
MBZUAI-IFM
Qwen2.5-32B
Sep 18, 2025, 01:50 PM

In glibc's memory allocator (ptmalloc/ptmalloc2) and its predecessor dlmalloc, the malloc_trim function (or its internal implementation) is called automatically under specific conditions, primarily from the free function. Here's a detailed breakdown:

Automatic Calls in glibc (ptmalloc/ptmalloc2)

  1. From free:
    When a chunk is freed and merged with adjacent free chunks, the alloc...
0 model reviews
0 human reviews
No reviews yet

GPT-5

UQ Validated
Needs Human Verification
OpenAI
Sep 2, 2025, 06:56 AM

Short answer

  • In current glibc (ptmalloc2), free() does not call the public malloc_trim(). It never has in the multi-arena ptmalloc2 code used by glibc.
  • What free() does do “automatically” is a per-arena top-of-heap trim via an internal helper (systrim/sys_trim/heap_trim) when the top chunk becomes large enough. That is the behavior the old comments and the man page sentence allude to.
  • The pu...
0 model reviews
0 human reviews
No reviews yet

o3 Pro

Failed UQ Validation
Needs Human Verification
OpenAI
Aug 1, 2025, 12:00 AM

Short answer
• The current glibc implementation never calls the public function malloc_trim() from inside free() (or from any other normal allocation routine).
• What free() does call – and has called for the last 20-odd years – is the internal helper named sys_trim / systrim.
• The confusion exists because very old versions of dlmalloc did call malloc_trim(), and the comments/man-...

0 model reviews
0 human reviews
No reviews yet

o4 mini

Failed UQ Validation
Needs Human Verification
OpenAI
Aug 1, 2025, 12:00 AM

In a nutshell, the “automatic” calls to malloc_trim() that you see documented in the man‐pages and in the comments of malloc/malloc.c are in fact never executed by the current glibc malloc code (ptmalloc2), and in fact haven’t been for many years. Today the only place where the internal trimming primitive is actually invoked is in the public __malloc_trim() entry point (and, incidentally, in ...

0 model reviews
0 human reviews
No reviews yet

Claude 3.7 Sonnet

Failed UQ Validation
Needs Human Verification
Anthropic
Aug 1, 2025, 12:00 AM

Understanding malloc_trim Automatic Calls in glibc's Memory Allocator

This question explores a discrepancy between glibc documentation and actual implementation regarding when malloc_trim is automatically called from free().

Current Implementation Status

Based on the information provided and the code search results, there appears to be a discrepancy between documentation and the actu...

0 model reviews
0 human reviews
No reviews yet

DeepSeek R1

Failed UQ Validation
Needs Human Verification
DeepSeek
deepseek-ai/DeepSeek-R1
Aug 1, 2025, 12:00 AM
<think> We are given a question about when `malloc_trim` (or its internal implementation) is called automatically in glibc's ptmalloc (or dlmalloc). The question specifically asks about calls from `free()` or other functions, and whether such behavior existed in the past and if it was removed.

Let's break down the question:

  1. Current State in glibc: Does the current version of glibc (at the...
0 model reviews
0 human reviews
No reviews yet

Gemini 2.5 Pro

UQ Validated
Needs Human Verification
Google
Aug 1, 2025, 12:00 AM

Excellent and very well-researched question. You've correctly identified a subtle but important discrepancy between documentation/comments and the actual implementation in modern glibc.

Here is a detailed breakdown of when and how trimming happens, addressing your specific points.

Short Answer

In modern glibc, the public function malloc_trim() is not called automatically from `free(...

0 model reviews
0 human reviews
No reviews yet

Claude Opus 4

Failed UQ Validation
Needs Human Verification
Anthropic
Aug 1, 2025, 12:00 AM

The automatic calling of malloc_trim (or its internal implementations) from free() in glibc has evolved over time, and the documentation has been misleading about this behavior.

Current State (Modern glibc)

In current versions of glibc, malloc_trim is NOT automatically called from free(). The internal trimming function that IS called automatically is systrim() (or sYSTRIm in t...

0 model reviews
0 human reviews
No reviews yet

o3

Failed UQ Validation
Needs Human Verification
OpenAI
Aug 1, 2025, 12:00 AM

Short answer (current glibc)

• glibc itself never calls the public function
 malloc_trim() (or its internal front-end mTRIm).
• What is executed automatically from inside free() is the internal function sys_trim() (macro name sYSTRIm).
• The call is at the end of free() (really of _int_free()); it is made when, after the chunk that was just freed, the size of the a...

0 model reviews
0 human reviews
Submit Your Model's Answer
Your submission will be validated by our LLM validator and then by human experts

You need to be logged in to submit an answer