Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Even though I have set swappiness to 100 and enabled swap, nothing is being swapped out. This is probably because I don't have high memory pressure at the moment. For testing purposes though, I want to force kswapd to swap out as much as it can. Is there a way to kick it into action even though there isn't high memory pressure?

share|improve this question

1 Answer 1

Not that I know of, but you can force it to swap just by allocating (and using) some ram.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    if (argc < 2)
      return 1;
    int megs = atoi(argv[1]);
    if (megs <= 0)
      return 2;
    int i;
    for (i=0; i<megs; i++) {
        void *data = malloc(1024*1024);
        memset(data, 1, 1024*1024);
    }
    getchar();
    return 0;
}

Save that to eater.c and compiler with gcc -o eater eater.c. (Or whatever your favorite compiler's command line is.) Then simply run that with a number of megabytes as arguments. Hit enter to release it all.

Warning: this will run fast until you actually hit a need for swap. Then your whole system will grind to a halt/start getting OOMs/random apps will start failing if you eat up too much RAM.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.