{"id":2318,"date":"2026-03-31T17:53:29","date_gmt":"2026-03-31T17:53:29","guid":{"rendered":"https:\/\/veerhost.com\/docs\/how-to-set-up-a-swap-file-on-your-vps-prevent-out-of-memory-crashes\/"},"modified":"2026-03-31T17:54:49","modified_gmt":"2026-03-31T17:54:49","slug":"how-to-set-up-a-swap-file-on-your-vps-prevent-out-of-memory-crashes","status":"publish","type":"docs","link":"https:\/\/veerhost.com\/de\/docs\/vps\/how-to-set-up-a-swap-file-on-your-vps-prevent-out-of-memory-crashes\/","title":{"rendered":"How to Set Up a Swap File on Your VPS (Prevent Out of Memory Crashes)"},"content":{"rendered":"<p class=\"wp-block-paragraph\">A swap file lets your VPS use disk space as overflow RAM. On plans with 512MB\u20131GB of RAM, adding swap prevents MySQL, PHP, and other services from crashing when memory gets tight.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Check If Swap Already Exists<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>swapon --show\nfree -h<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the swap row shows 0B or nothing, there&#8217;s no swap configured. Proceed below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create the Swap File<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a 2GB swap file (recommended for 1GB RAM VPS plans):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fallocate -l 2G \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>fallocate<\/code> fails, use <code>dd<\/code> instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dd if=\/dev\/zero of=\/swapfile bs=1M count=2048<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Secure and Enable the Swap File<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Set correct permissions (only root can read it)\nsudo chmod 600 \/swapfile\n\n# Format it as swap\nsudo mkswap \/swapfile\n\n# Enable the swap file\nsudo swapon \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Verify it&#8217;s active:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>free -h<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should now see swap space listed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Make Swap Permanent (Survive Reboots)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add the swap file to <code>\/etc\/fstab<\/code> so it persists after a reboot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo '\/swapfile none swap sw 0 0' | sudo tee -a \/etc\/fstab<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Tune Swappiness (Optional)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swappiness controls how aggressively the system uses swap (0\u2013100). The default is 60. For a server, 10 is recommended so the system prefers using RAM and only falls back to swap when necessary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Check current value\ncat \/proc\/sys\/vm\/swappiness\n\n# Set to 10 for this session\nsudo sysctl vm.swappiness=10\n\n# Make it permanent\necho 'vm.swappiness=10' | sudo tee -a \/etc\/sysctl.conf<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Note on VPS Swap<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swap on a VPS uses your disk (usually SSD), which is slower than RAM. It&#8217;s a safety net, not a replacement for RAM. If you find yourself relying on swap constantly, consider upgrading your VPS plan for better performance.<\/p>","protected":false},"excerpt":{"rendered":"<p>A swap file lets your VPS use disk space as overflow RAM. On plans with 512MB\u20131GB of RAM, adding swap prevents MySQL, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1967,"menu_order":12,"comment_status":"open","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-2318","docs","type-docs","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/veerhost.com\/de\/wp-json\/wp\/v2\/docs\/2318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/veerhost.com\/de\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/veerhost.com\/de\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/veerhost.com\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/veerhost.com\/de\/wp-json\/wp\/v2\/comments?post=2318"}],"version-history":[{"count":1,"href":"https:\/\/veerhost.com\/de\/wp-json\/wp\/v2\/docs\/2318\/revisions"}],"predecessor-version":[{"id":2320,"href":"https:\/\/veerhost.com\/de\/wp-json\/wp\/v2\/docs\/2318\/revisions\/2320"}],"up":[{"embeddable":true,"href":"https:\/\/veerhost.com\/de\/wp-json\/wp\/v2\/docs\/1967"}],"wp:attachment":[{"href":"https:\/\/veerhost.com\/de\/wp-json\/wp\/v2\/media?parent=2318"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/veerhost.com\/de\/wp-json\/wp\/v2\/doc_tag?post=2318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}