I’m trying to diagnose some random segfaults on a headless server and one thing that seems curious is that they only seem to happen under memory pressure and my swap size will not go above 0.
How can I force my machine to swap to make sure that it is working properly?
orca ~ # free
total used free shared buffers cached
Mem: 1551140 1472392 78748 0 333920 1046368
-/+ buffers/cache: 92104 1459036
Swap: 1060280 0 1060280
orca ~ # swapon -s
Filename Type Size Used Priority
/dev/sdb2 partition 1060280 0 -1
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Is this Linux? If so, you could try the following:
# sysctl vm.swappiness=100
(You might want to use sysctl vm.swappiness first to see the default value, on my system it was 10)
And then either use a program(s) that uses lots of RAM or write a small application that just eats up RAM. The following will do that (source: Experiments and fun with the Linux disk cache):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv) {
int max = -1;
int mb = 0;
int multiplier = 1; // allocate 1 MB every time unit. Increase this to e.g.100 to allocate 100 MB every time unit.
char* buffer;
if(argc > 1)
max = atoi(argv[1]);
while((buffer=malloc(multiplier * 1024*1024)) != NULL && mb != max) {
memset(buffer, 1, multiplier * 1024*1024);
mb++;
printf("Allocated %d MBn", multiplier * mb);
sleep(1); // time unit: 1 second
}
return 0;
}
Coded the memset line to initialise blocks with 1s rather than 0s,
because the Linux virtual memory manager may be smart enough
not to actually allocate any RAM otherwise.
I added the sleep(1) in order to give you more time to watch the processes as it gobbles up ram and swap. The OOM killer should kill this once you are out of RAM and SWAP to give to the program. You can compile it with
gcc filename.c -o memeater
where filename.c is the file you save the above program in. Then you can run it with ./memeater.
I wouldn’t do this on a production machine.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0