Skip to content

Instantly share code, notes, and snippets.

@buchgr
Created February 9, 2016 18:48
Show Gist options
  • Save buchgr/ab49c171ba0a3390ae48 to your computer and use it in GitHub Desktop.
Save buchgr/ab49c171ba0a3390ae48 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <cstring>
#include <cstdio>
int main(int argc, char** argv) {
constexpr size_t SIZE = 512 << 20;
unsigned i = 10;
void *p = mmap(NULL, SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
while(i--) {
memset(p, 1, SIZE);
// 4 = MADV_DONTNEED, 8 = MADV_FREE
long err = syscall(SYS_madvise, p, SIZE, 4);
if (err)
printf("err: %ld\n", err);
}
return 0;
}
@buchgr
Copy link
Author

buchgr commented Feb 9, 2016

buchgra@linux:~$ time ./madv-dontneed 
real    0m2.599s
user    0m0.244s
sys 0m2.352s
buchgra@linux:~$ time ./madv-free 
real    0m1.384s
user    0m1.072s
sys 0m0.308s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment