malloc pre-allocated too much memory

Steve Munroe sjmunroe@us.ibm.com
Fri Feb 18 00:09:00 GMT 2005


libc-alpha-owner@sources.redhat.com wrote on 02/17/2005 10:31:38 AM:

> Hi,
> 
>     I'm running a service with very small memory usage (less than 500 
KB)
> but a large amount of servicing processes (more than 20 thousand).
> 
>     Since we always work hard to reduce the memory usage, we've found 
that
> gnu libc's malloc seems to pre-allocate about 128 KB of memory.  (We 
found
> this phenomenon because we add additional memory to the machine, but the
> number of processes grew up in FreeBSD but Linux.) But in most case we
> won't use that much, I think 5 KB are really enough for each process. So
> there are a large ratio of unused memory.  I would like to ask either 
any
> suggestion, solution to reduce the pre-allocation, or any other good
> idea       about this problem.
> 
These are the default settings from malloc.c:

#define DEFAULT_MMAP_THRESHOLD (128 * 1024)
#define DEFAULT_MMAP_MAX       (65536)
#define DEFAULT_TRIM_THRESHOLD (128 * 1024)
#define DEFAULT_TOP_PAD        (0)

You can also adjust these values at runtime using #include <malloc.h>

#define MY__MMAP_THRESHOLD (1024 * 8)
#define MY__MMAP_MAX (0)
#define MY__TRIM_THRESHOLD (1024 * 8)
#define MY__TOP_PAD (0)

        mallopt (M_MMAP_THRESHOLD, MY__MMAP_THRESHOLD);
        mallopt (M_MMAP_MAX, MY__MMAP_MAX);
        mallopt (M_TRIM_THRESHOLD, MY__TRIM_THRESHOLD);
        mallopt (M_TOP_PAD, MY__TOP_PAD);



Steven J. Munroe
Linux on Power Toolchain Architect
IBM Corporation, Linux Technology Center




More information about the Libc-alpha mailing list