How process size is calculated? Is it always based on the current highest available address in memory space?

Dan Nelson dnelson at allantgroup.com
Mon Dec 29 16:16:03 UTC 2008


In the last episode (Dec 28), Yuri said:
> malloc(3) can be controlled by MALLOC_OPTIONS to use mmap-based
> allocation as opposed to sbrk-based allocation. But
> allocations/deallocations with mmaps can eventually lead to
> non-continuously mmapped memory (having some non-mmapped gaps).
> 
> Are these gaps excluded from the process size or size is always
> linked to the current highest available address in memory space?

It looks like only mapped memory is counted in process size.  The test
program below shows that the reported size goes down, even if a memory
range inbetween two others is unmapped:

$ ./mmap
Before mmap:
  UID   PID  PPID CPU PRI NI   VSZ   RSS MWCHAN STAT  TT       TIME COMMAND
 1000 48058 62165   0   8  0  1928   824 wait   S+    ph    0:00.01 ./mmap
mmap 64MB A=0x28280000
  UID   PID  PPID CPU PRI NI   VSZ   RSS MWCHAN STAT  TT       TIME COMMAND
 1000 48058 62165   0   8  0 67464   824 wait   S+    ph    0:00.01 ./mmap
mmap 64MB B=0x2c280000
  UID   PID  PPID CPU PRI NI   VSZ   RSS MWCHAN STAT  TT       TIME COMMAND
 1000 48058 62165   0   8  0 133000   824 wait   S+    ph    0:00.01 ./mmap
mmap 64MB C=0x30280000
  UID   PID  PPID CPU PRI NI   VSZ   RSS MWCHAN STAT  TT       TIME COMMAND
 1000 48058 62165   0   8  0 198536   824 wait   S+    ph    0:00.01 ./mmap
munmap B
  UID   PID  PPID CPU PRI NI   VSZ   RSS MWCHAN STAT  TT       TIME COMMAND
 1000 48058 62165   0   8  0 133000   824 wait   S+    ph    0:00.01 ./mmap


#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>

int main(void)
{
  char *cmd;
  void *a, *b, *c;
  asprintf(&cmd, "ps axlp %d", getpid());
  printf("Before mmap:\n");
  system(cmd);
  a = mmap(NULL, 64 * 1024 * 1024, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
  printf("mmap 64MB A=%p\n", a);
  system(cmd);
  b = mmap(NULL, 64 * 1024 * 1024, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
  printf("mmap 64MB B=%p\n", b);
  system(cmd);
  c = mmap(NULL, 64 * 1024 * 1024, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
  printf("mmap 64MB C=%p\n", c);
  system(cmd);
  printf("munmap B\n");
  munmap(b, 64 * 1024 * 1024);
  system(cmd);
  return 0;
}


-- 
	Dan Nelson
	dnelson at allantgroup.com


More information about the freebsd-hackers mailing list