22

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
  • Are there any messages in kern.log at the time of the segfaults? A message about oom-killer would indicate that your system doesn't have enough virtual memory, which could mean that swap isn't being used. Is this a virtualized server (and what kind)? – Gilles Aug 29 '10 at 20:24
  • There are no oom-killer entries in the log just stuff like segfault at 54 ip b7619ba8 sp bf9c3380 error 4 I'm thinking it's a hardware problem which is going to be a pain to track down. This is a physical server with dual Athlon MP 2000+ processors and 1.5GB of RAM. It runs fairly stably but segfaults during compiles. – joshperry Aug 30 '10 at 0:45
  • 1
    Well, turned out that the case fans were not plugged in which was causing issues when the server would start doing anything processor intensive causing it to overheat. – joshperry Aug 30 '10 at 1:35
28

Is this linux? If so you could try the following:

# sysctl vm.swappiness=100

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: http://www.linuxatemyram.com/play.html):

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

int main(int argc, char** argv) {
    int max = -1;
    int mb = 0;
    char* buffer;

    if(argc > 1)
        max = atoi(argv[1]);

    while((buffer=malloc(1024*1024)) != NULL && mb != max) {
        memset(buffer, 0, 1024*1024);
        mb++;
        printf("Allocated %d MB\n", mb);
        sleep(1);
    }      
return 0;
}

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.

  • Thanks, that worked well to gobble up memory and start swapping. I guess my segfaults are caused by something else... probably hardware :/ – joshperry Aug 30 '10 at 0:40
  • For linux, it might be useful to start by writing to /proc/self/oom_score_adj to ensure that it is the most likely OOM-killer victim... – Gert van den Berg Oct 15 '17 at 20:33
  • 1
    need to include <unistd.h> for sleep, otherwise it throws a warning warning: implicit declaration of function ‘sleep’; – Debanjan Basu Jan 26 '18 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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