malloc_trim(3)
๐ malloc_trim(3) โ bestie edition ๐
okay so you know how your program grabs a bunch of memory for the heap and then justโฆ never gives it back? ๐ซ yeah. this is the page about the function that finally says "bestie, let it GO." it's malloc_trim from the Linux man-pages 6.20 era, living its best life in the Library Functions Manual. no cap.
โจ NAME โจ
malloc_trim โ release free memory from the heap ๐ธ
like, actually hand it back to the system. giving back what you don't need. main character behavior fr.
๐ LIBRARY ๐
Standard C library (libc, -lc) ๐
she's built in. no extra packages, no nothing. iconic.
๐ SYNOPSIS ๐
#include <malloc.h>
int malloc_trim(size_t pad);
yes you need the #include <malloc.h>, don't forget it, i literally always forget it and then i cry ๐ญ
๐ฆ DESCRIPTION ๐ฆ
okay so malloc_trim() attempts to release free memory from the heap. and the way it does that? by calling sbrk(2) or madvise(2) with suitable arguments. lowkey wild that those are the two tools but okay, she works with what she's got. โจ
now the pad argument. this one is the whole vibe of the function so listen up:
pad specifies the amount of free space to leave untrimmed at the top of the heap. think of it like โ you're cleaning your room but you're allowed to keep a lil pile at the very top. cute.
- if this argument is
0, only the minimum amount of memory is maintained at the top of the heap. and by minimum we mean one page or less. basically bare bones. nothing extra. she's serving minimalism. ๐ - a nonzero argument can be used to maintain some trailing space at the top of the heap, in order to allow future allocations to be made without having to extend the heap with
sbrk(2). it's like keeping a lil snack in your bag so you don't have to go to the store later. strategic. ๐
๐ซถ RETURN VALUE ๐ซถ
- returns
1if memory was actually released back to the system ๐ (slay) - returns
0if it was not possible to release any memory ๐ (not slay but it happens)
so basically a bool with extra steps. we love a confident yes/no. periodt.
๐ ERRORS ๐
No errors are defined.
literally none. she doesn't do errors. she's too busy trimming. icon behavior fr fr. ๐
โญ ATTRIBUTES โญ
for an explanation of the terms used in this section, pls pls pls see attributes(7). i'm not gonna re-explain it, that's a whole other page. go read it. ily. ๐
- Interface:
malloc_trim()- Attribute: Thread safety
- Value:
MT-Safe๐ฅ
MT-Safe means she can hang with the whole friend group at once. no drama, no collisions, no problems. ๐
๐ STANDARDS ๐
GNU.
just GNU. that's the whole thing. she's a GNU girlie and she's not pretending otherwise. ๐
๐ธ VERSIONS ๐ธ
glibc 2.0.
she's been here since 2.0, which is likeโฆ ancient. veteran status. respect the elders. ๐ง
๐ NOTES ๐
okay THIS is the part where it gets real, so pay attention:
- Only the main heap (the one using
sbrk(2)) honors thepadargument. thread heaps do not. so if you're out here passingpadto a thread heap expecting it to workโฆ it does not. i'm so sorry bestie. ๐ญ - Since glibc 2.8, this function frees memory in all arenas and in all chunks with whole free pages. glow up era. โจ
- But before glibc 2.8, this function only freed memory at the top of the heap in the main arena. that's it. that's all she did. the old days were rough. ๐
so like, if you're on a quirky old system, don't expect the full trim. check your glibc version first, okay? ๐
๐ SEE ALSO ๐
go read those too, they're all in the same friend group. iykyk. ๐ฆ
and that's the page!! malloc_trim(3), from the Linux man-pages 6.20, dated 2026-02-08 โ thank you for coming to my ted talk, mwah, go free some memory bestie, the heap deserves to breathe ๐๐โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2012, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms