Howto Create Linux Swap On Ubuntu 12.04

by admin admin Date: 10-02-2014 linux ubuntu swap ram disk filesystem memory


About Linux Swapping


Linux RAM is composed of chunks of memory called pages. To free up pages of RAM, a “linux swap” can occur and a page of memory is copied from the RAM to preconfigured space on the hard disk. Linux swaps allow a system to harness more memory than was originally physically available.

However, swapping does have disadvantages. Because hard disks have a much slower memory than RAM, virtual private server performance may slow down considerably. Additionally, swap thrashing can begin to take place if the system gets swamped from too many files being swapped in and out.

Check for Swap Space


Before we proceed to set up a swap file, we need to check if any swap files have been enabled on the VPS by looking at the summary of swap usage.
sudo swapon -s

An empty list will confirm that you have no swap files enabled:
Filename				Type		Size	Used	Priority

Check the File System


After we know that we do not have a swap file enabled on the virtual server, we can check how much space we have on the server with the df command. The swap file will take 512MB— since we are only using up about 8% of the /dev/sda, we can proceed.
df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda        20907056 1437188  18421292   8% /
udev              121588       4    121584   1% /dev
tmpfs              49752     208     49544   1% /run
none                5120       0      5120   0% /run/lock
none              124372       0    124372   0% /run/shm

Create and Enable the Swap File


Now it’s time to create the swap file itself using the dd command :
sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k

“of=/swapfile” designates the file’s name. In this case the name is swapfile.

Subsequently we are going to prepare the swap file by creating a linux swap area:
sudo mkswap /swapfile

The results display:
Setting up swapspace version 1, size = 262140 KiB
no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb

Finish up by activating the swap file:
sudo swapon /swapfile

You will then be able to see the new swap file when you view the swap summary.
swapon -s
Filename				Type		Size	Used	Priority
/swapfile                               file		262140	0	-1

This file will last on the virtual private server until the machine reboots. You can ensure that the swap is permanent by adding it to the fstab file.

Open up the file:
sudo nano /etc/fstab

Paste in the following line:
 /swapfile       none    swap    sw      0       0 


Swappiness in the file should be set to 0. Skipping this step may cause both poor performance, whereas setting it to 0 will cause swap to act as an emergency buffer, preventing out-of-memory crashes.

You can do this with the following commands:
echo 0 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 0 | sudo tee -a /etc/sysctl.conf

To prevent the file from being world-readable, you should set up the correct permissions on the swap file:
sudo chown root:root /swapfile 
sudo chmod 0600 /swapfile
That's all, enjoy!
 
by admin admin Date: 10-02-2014 linux ubuntu swap ram disk filesystem memory hits : 7890  
 
 
 
 

Related Posts

    4 Tips For Logging in Java

    Did you realize there are over 9 million Java programmers in the world? When using this programming language, developers are able to build apps that are both appealing and functional. Learning…

    10 Collaborative Coding Tools for Remote Pair Programming

    The days of lonesome coding are long gone since the development of pair programming. It is a technique where two software developers share a keyboard and collaboratively work together. The…

    The Bible of the Best Node.Js Frameworks

    Node.js is the platform built on chrome’s javascript runtime for easily building fast, scalable network applications. It uses an event-driven non-blocking IO modal that makes it lightweight and efficient. node.js…

    Starting with Vue.js: how about using it?

    Vue.js seems to be another JavaScript UI library with a strong community support. If you've got a project coming up and you're wondering which tool to choose from, you can…

    The Best RSS Readers for Ubuntu

    Even if most of the tech experts actively claim that RSS (Rich Site Summary) is dead especially after Google Reader was discontinued 5 years ago but it isn’t yet as…

    Which Javascript frameworks will rule in 2019?

    In this article, we will review and analyze the 2018 State of JavaScript report to later focus attention on frameworks that will be at the center of attention in 2019. Let's…

    Best 5 Frontend Development Topics in 2019

    Ah, a new year. A time of positivity, a time to restart. For new objectives and proposals. And in the world of developers, to look at the scenery and decide…

    Most Popular Node.js Frameworks in 2019

    Before Node.js, web developers always faced the issue of having to use different languages for client-side and server-side scripts. JavaScript was traditionally the language for client-side scripting with the HTML…

    80 Linux Network Monitor Software & Tools for Managing & Monitoring Unix/Linux Systems

    It’s hard work monitoring and debugging Linux performance problems, but it’s easier with the right tools at the right time. Finding a Linux Network Monitor tool or Software package for…

    How to install Letsencrypt certificates with certbot in ubuntu 16.04

    Install Certbot's Nginx package with apt-get. sudo apt-get install python-certbot-nginx sudo certbot -i nginx -a webroot -w /var/www/mysite.org -d www.mysite.org edit the nginx config file for /etc/nginx/sites-available/default. server { listen 443 ssl; server_name mysite.org; ssl_certificate /etc/letsencrypt/live/mysite.org/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/mysite.org/privkey.pem; location…

    Best Development Posts Of August 2018

    For this month, we ranked some Web Development articles to pick the Top 20 stories that can help advance your career. The Five Top Reasons to Use JavaScript by Richard Kenneth Eng I’m a…

    How to Set up a Fully Functional Mail Server on Ubuntu 16.04 with iRedMail

    Setting up your own mail server from scratch on Linux is complex and tedious, until you meet iRedMail. This tutorial is going to show you how you can easily and…