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 1 if memory was actually released back to the system ๐ŸŽ‰ (slay)
  • returns 0 if 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 the pad argument. thread heaps do not. so if you're out here passing pad to 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms