Skip to content

Instantly share code, notes, and snippets.

@zed9h
Last active August 29, 2015 14:06
Show Gist options
  • Save zed9h/1903c80fefc315251450 to your computer and use it in GitHub Desktop.
Save zed9h/1903c80fefc315251450 to your computer and use it in GitHub Desktop.
Does your Mac OSX 10.9.4 Mavericks do a bad job at memory management? Use this script to force it to free some memory. Run it passing the amount of gigs of ram you have (default 8gb) and [optionally] wait the chart on the Memory tab of Activity Monitor to display some yellow and red.
#if 0
BIN=`mktemp $0.bin.XXXX`
trap "rm -fv $BIN" 0
gcc -O3 $0 -Wall -Werror -o $BIN && $BIN $1
exit
#endif
#include<stdio.h>
#include<stdlib.h>
#define GB (1024*1024*1024)
#define CHUNK 256000000
int main(int argc, char *argv[])
{
int i, j;
int total = argc > 1 ? atoi(argv[1]) : 8;
int n = ((long long)total * GB) / CHUNK;
long long mem = 0;
for(i=0; i<n; i++) {
char *p = malloc(CHUNK);
mem+=CHUNK;
printf("%03d %p %0.1f/%dGB\n", i, p, mem/(double)GB, total);
for(j=0; j<CHUNK; j++) {
p[j] = i^j; // XXX avoid mavericks' compression
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment