Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Our company web sites have just been moved from a shared host to a VDS, as the hosting company plans on retiring the less than completely reliable shared hosting server(s).

The VDS only has 256MB of RAM and hosts a few sites, all but one of which are very simple static sites with just a few pages and combined serve less than 20-30 visitors per day.

Our primary site, which runs a somewhat customized version of OSCommerce, is also low traffic (rarely more than 10 or so concurrent users) but does require MySQL. Unfortunately, since the move mysqld keeps crashing due to insufficient memory. I've edited the Apache config a bit (to reduce the number of threads) to ease the load but that's only sidestepping the real issue.

The server has no swap space but it does have considerably more disk space than we require, so I'm thinking about using some of that space for swap. I know that won't exactly help performance but we can live with that.

How can I create a swap file and enable it while the server is running? The OS is CentOS 5.5 (final) and Linux is not my native tongue.

Update: For anyone who may be wondering about the results, since implementing the swap file as described by quanta the server has not only been stable, with no more crashes of mysqld, it is actually serving pages faster than it did before. The performance gain is contrary to what I expected but I'm delighted to be wrong in this instance.

share|improve this question
up vote 15 down vote accepted

Assuming that you want to create 512MB swap file:

# dd if=/dev/zero of=/swapfile bs=1024 count=524288

Setup swap area with:

# mkswap /swapfile

Activate it:

# swapon /swapfile

Add to /etc/fstab for permanent:

echo -e "/swapfile\t\tswap\t\tswap\t\tdefaults\t0 0" >> /etc/fstab

and verify with free -m.

share|improve this answer
No restart required? – John Gardeniers Sep 16 '11 at 3:32
Sure, no need to restart. It was activated with swapon. – quanta Sep 16 '11 at 3:35

Your Answer


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.