Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following analysis applies only to glibc (based on ptmalloc2 algorithm). There are certain options that seem helpful to return the freed memory back to the system:</p> <ol> <li><p><a href="http://www.gnu.org/s/hello/manual/libc/Malloc-Tunable-Parameters.html" rel="noreferrer">mallopt()</a> (defined in <code>malloc.h</code>) does provide an option to set the trim threshold value using one of the parameter option <code>M_TRIM_THRESHOLD</code>, this indicates the minimum amount of free memory (in bytes) allowed at the top of the data segment. If the amount falls below this threshold, glibc invokes <code>brk()</code> to give back memory to the kernel.</p> <p>The default value of <code>M_TRIM_THRESHOLD</code> in Linux is set to 128K, setting a smaller value might save space.</p> <p>The same behavior could be achieved by setting trim threshold value in the environment variable <code>MALLOC_TRIM_THRESHOLD_</code>, with no source changes absolutely.</p> <p>However, preliminary test programs run using <code>M_TRIM_THRESHOLD</code> has shown that even though the memory allocated by malloc does return to the system, the remaining portion of the actual chunk of memory(the arena) initially requested via <code>brk()</code> tends to be retained. </p></li> <li><p>It is possible to trim the memory arena and give any unused memory back to the system by calling <code>malloc_trim(pad)</code> (defined in <code>malloc.h</code>). This function resizes the data segment, leaving at least <code>pad</code> bytes at the end of it and failing if less than one page worth of bytes can be freed. Segment size is always a multiple of one page, which is 4,096 bytes on i386.</p> <p>The implementation for this modified behaviour of <code>free()</code> using <code>malloc_trim</code> could be done using the malloc hook functionality. This would not require any source code changes to the core glibc library.</p></li> <li><p>using <code>madvise()</code> system call inside the free implementation of glibc.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload