How To Add Swap on Ubuntu 12.04

Aug 17, 2012  Linux Basics Ubuntu

 

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 256MB— 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=256k

“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 10. Skipping this step may cause both poor performance, whereas setting it to 10 will cause swap to act as an emergency buffer, preventing out-of-memory crashes.

You can do this with the following commands:

echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | 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

[转载]How To Add Swap on Ubuntu 12.04的更多相关文章

  1. How To Add Swap on Ubuntu 14.04

    https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 How To Add Swap on ...

  2. [转]Ubuntu 12.04 安装屏保

    From:http://www.howtogeek.com/114027/how-to-add-screensavers-to-ubuntu-12.04/ How to Add Screensaver ...

  3. Get back Typing Break in Ubuntu 12.04 & 11.10(转载)

    转自:http://ubuntuguide.net/get-back-typing-break-in-ubuntu-12-04-11-10 Since Ubuntu 11.10 Oneiric, th ...

  4. Ubuntu 12.04的gnome classic panel添加程序快捷键(转载)

    How to add applets to the Gnome classic panel in Ubuntu 12.04 转自:http://www.borfast.com/blog/how-add ...

  5. 【NS2】Ubuntu 12.04 LTS 中文输入法的安装(转载)

    本文是笔者使用 Ubuntu 操作系统写的第一篇文章!参考了红黑联盟的这篇文章:Ubuntu 12.04中文输入法的安装 安装 Ubuntu 12.04 着实费力一番功夫,老是在用 Ubuntu 来引 ...

  6. [转载] Ubuntu 12.04下安装git,SSH及出现的Permission denied解决办法

    如何安装ssh http://os.51cto.com/art/201109/291634.htm 仅需要阅读至成功开启ssh服务即可 http://www.linuxidc.com/Linux/20 ...

  7. ubuntu 12.04 alt+tab无法切换窗口的问题(转载)

    转自:http://www.2cto.com/os/201209/153282.html ubuntu 12.04 alt+tab无法切换窗口的问题   安装cpmpiz配置管理程序.   sudo ...

  8. Ubuntu 12.04 设置终端字体为文泉驿(转载)

    转自:http://my.oschina.net/uniquejava/blog/98480 0.说明      在gnome-terminal终端中看习惯了文泉驿微米黑字体,没想到从11.10升级到 ...

  9. Ubuntu 12.04 root默认密码? 如何使用root登录? (转载)

    转自:http://www.lupaworld.com/article-219280-1.html 在安装Ubuntu 12.04时并没有设置root的密码,登录的时候也没有使用root账户.当我们使 ...

随机推荐

  1. loj 1002(spfa变形)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25828 题意:求所有点到给定的目标顶点的路径上的权值的最大值的最小 ...

  2. Linux学习笔记(10)文本编辑器vim

    vim是一个功能强大的全屏幕文本编辑器,是Linux/Unix最常用的文本编辑器,其作用是建立.编辑.显示文本文件.vim的特点是没有菜单,只有命令. vim主要有三种工作模式,分别为命令模式.插入模 ...

  3. 组合数问题hdu5894

    http://acm.hdu.edu.cn/showproblem.php?pid=5894 题意如上

  4. 【转】Linux 查看内存(free buffer cache)

    转自:http://elf8848.iteye.com/blog/1995638 Linux下如何查内存信息,如内存总量.已使用量.可使用量.经常使用Windows操作系统的朋友,已经习惯了如果空闲的 ...

  5. HDU 4162 Shape Number (最小表示法)

    题意:给你一串n个数,求出循环来看一阶差的最小字典序:数字串看成一个顺时针的环,从某一点开始顺时针循环整个环,保证字典序最小就是答案 例如给你 2 1 3 就会得到(1-2+8 注意题意负数需要加8) ...

  6. JavaScript 之 iframe自适应问题---可以用来实现网页局部刷新

    1.HTML <iframe src="index.html" id="iframepage" frameborder="0" scr ...

  7. 《DSP using MATLAB》示例Example4.10

    上代码: b = [1, 0.4*sqrt(2)]; a = [1, -0.8*sqrt(2), 0.64]; % compute the polynomials coefficients given ...

  8. Delphi中Messagedlg用法

    if MessageDlg('Welcome to my Delphi application. Exit now?', mtConfirmation, [mbYes, mbNo], 0) = mrY ...

  9. iOS实现简书的账号识别方式(正则表达式)

    通过简书iOS客户端登录,我们会看到请输入手机号或者邮箱登录,但是我们随机输入1234567的时候,便会弹出手机格式不正确,同样也会识别我们的邮箱格式,那么我们在项目中怎么实现这种判断呢? 0E471 ...

  10. DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots

    题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...