Why process memory starts so high up in virtual space with FreeBSD malloc?

Giorgos Keramidas keramida at ceid.upatras.gr
Mon Dec 1 14:43:40 PST 2008

On Mon, 01 Dec 2008 13:28:48 -0800, Yuri <yuri at rawbw.com> wrote:
> I am compiling the following program:
> #include <stdlib.h>
> main() { printf("0x%x\n", malloc(1)); }

You should probably use printf("%p", ptr) to print pointers :)

> in 32-bit 7.1-PRERELEASE and get 0x28201100 which is ~673MB of 4GB
> address space or 16%.
> When I run the same program with the google malloc (from
> devel/google-perftools)
> I get much lower value 0x80aa0e8 which is ~135MB of 4GB address space or
> ~3%.
> Why FreeBSD memory allocator wastes such a high percentage of the
> memory space?

The FreeBSD malloc(3) implementation can use either mmap() or sbrk() to
obtain memory from the system.  It does not 'waste a high percentage of
memory' but it simply maps only high addresses (with an unmapped 'hole'
in lower addresses).

You can tune FreeBSD malloc(3) to prefer either sbrk() or mmap() by
setting the 'D' and 'M' malloc options.  The following program prints
different initial allocation results when these two malloc() options are
configured differently:

    % cat -n foo.c
         1  #include <stdio.h>
         2  #include <stdlib.h>
         4  int
         5  main(void)
         6  {
         7          char *p;
         9          p = malloc(1);
        10          if (p == NULL)
        11                  return EXIT_FAILURE;
        13          printf("%p\n", malloc(1));
        14          free(p);
        15          return EXIT_SUCCESS;
        16  }

For example:

    % env MALLOC_OPTIONS='Dm' ./foo
    % env MALLOC_OPTIONS='dM' ./foo

More details about the 'D', 'M' and other malloc() options should be
available in the manpage for your release.

- Giorgos

More information about the freebsd-questions mailing list