Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created October 30, 2012 22:52
Show Gist options
  • Save bnoordhuis/3983624 to your computer and use it in GitHub Desktop.
Save bnoordhuis/3983624 to your computer and use it in GitHub Desktop.
show difference between mmap() and malloc()
$ gcc -DUSE_MMAP=0 tmp/rss.c && ./a.out
rss 360448
==============
rss 360448
rss 1429504
rss 552960
==============
rss 552960
rss 1359872
rss 1359872
==============
rss 1359872
rss 1359872
rss 1359872
==============
rss 1359872
rss 1359872
rss 1359872
==============
rss 1359872
rss 1359872
rss 1359872
$ gcc -DUSE_MMAP=1 tmp/rss.c && ./a.out
rss 360448
==============
rss 360448
rss 1429504
rss 536576
==============
rss 536576
rss 1343488
rss 536576
==============
rss 536576
rss 1343488
rss 536576
==============
rss 536576
rss 1343488
rss 536576
==============
rss 536576
rss 1343488
rss 536576
/*
* Copyright (c) 2012, Ben Noordhuis <info@bnoordhuis.nl>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __linux__
#error "Linux only."
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
static unsigned long rss(void)
{
int fd = open("/proc/self/stat", O_RDONLY);
if (fd == -1) return 0;
char buf[1024];
ssize_t n = read(fd, buf, sizeof(buf) - 1);
if (n == -1) return close(fd), 0;
buf[n] = '\0';
const char *s = buf;
int i;
for (i = 0; i < 23; i++) { // XXX doesn't handle spaces in process name
while (*s != ' ' && *s != '\0') s++;
if (*s == ' ') s++;
}
unsigned long rv = 0;
if (sscanf(s, "%lu", &rv) != 1) rv = 0;
close(fd);
return rv * sysconf(_SC_PAGESIZE);
}
int main(void)
{
printf("rss %lu\n", rss());
int i;
for (i = 0; i < 5; i++) {
size_t size = 1024*1024;
#if USE_MMAP
int prot = PROT_READ|PROT_WRITE;
int flags = MAP_PRIVATE|MAP_ANONYMOUS;
void *addr = mmap(NULL, size, prot, flags, -1, 0);
if (addr == (void *) MAP_FAILED) perror("mmap"), exit(1);
#else
void *addr = malloc(size);
if (addr == NULL) perror("malloc"), exit(1);
#endif
printf("==============\n");
printf("rss %lu\n", rss());
memset(addr, 0, size);
printf("rss %lu\n", rss());
#if USE_MMAP
munmap(addr, size);
#else
free(addr);
#endif
printf("rss %lu\n", rss());
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment