Re: Kernel cached memory

Denis Vlasenko
2005-07-23T12:31:37Z

On Saturday 23 July 2005 00:43, John Pearson wrote:
> Wouldn't having (practically) all your memory used for cache slow down
> starting a new program? First it would have to free up that space, and then
> put stuff in that space, taking potentially twice as long. I think there
> should be a system call for freeing cached memory, for those that do want to
> do it.

I think this one is good enough:

#include <stdlib.h>

int main() {
void *p;
unsigned size = 1<<20;
unsigned long total=0;
while(size) {
p = malloc(size);
if(!p) size>>=1;
else {
memset(p, 0x77, size);
total+=size;
printf("Allocated %9u bytes, %12lu total\n",size,total);
}
}
return 0;
}

You may want to adapt it so that it takes an argument now many megabytes
to eat before it dies.
--
vda