HOME BLOG ARCHIVE TAGS

Trimming glibc malloc

November 02, 2016

Glibc malloc and long-lived programs can interact badly, leaving large amounts of unneeded memory resident. Even when developers work carefully.

Constrained environments suffer with unrestrained caching. E.g., when Linux is out of memory, OOM Killer sometimes terminates (theoretically) well-behaved apps.

If glibc heap can’t be replaced (for whatever reason), malloc_trim function is a workaround for RSS unbounded growth. It handles all arenas (though docs state differently), and leverages demand paging (i.e., if [s]brk issues don’t let address-space shrink, underlying page frames may still be released).

Regardless of being an inconvenience, manual trimming works pretty well in practice.