How to set the Ubuntu swap Zone

Source: Internet
Author: User
How to add swap?


Swap is generally in a proprietary swap zone. This may be because the system prompts you to create a swap zone during installation. But in fact, any file can be used as a SWAp (SWAP device), whether it is a partition or a simple file. However, we recommend that you use large-capacity memory instead of swap for better experience and Speed Sensing. You can add a swap file to add a swap.


Four steps



  1. Create a file of the predefined size.

  2. Format the file into a SWAp (switch device ).

  3. Add a SWAp to an existing system.

  4. Make a permanent change in the system.


Add a m swap



  • Create a 512m file:


We will create a swap file on/mnt/512mb. Swap.


sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512

In the code?Count = 512Which means we want to create a file, including 512?BS = 1 m(BS = block size; 1 m = 1 mebibyte 1 MB = 1048576 bytes )? . Note:* Do not *Use?Dd OF =/mnt/512mb. Swap BS = 1 m seek = 512 COUNT = 0This command. Although the file will grow to 512 MB immediately, this vulnerability may cause unavailability.



  • Format the file to make it swap (switch device ):


sudo mkswap /mnt/512Mb.swap

  • Add swap to the existing system:


sudo swapon /mnt/512Mb.swap

The newly added swap can pass"CAT/proc/meminfo"Command to see.



  • Make permanent changes:


Edit the/etc/fstab file:


gksudo gedit /etc/fstab

Add the following line at the end of the file:


/mnt/512Mb.swap  none  swap  sw  0 0

Save and restart.


Example of a SWAp File


The following example shows how to create and use swap on a computer without a swap partition.


user@computer:~$ sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512
Password:
512+0 records in
512+0 records out
536870912 bytes (537 MB) copied, 35.3802 seconds, 15.2 MB/s
user@computer:~$ sudo mkswap /mnt/512Mb.swap
Setting up swapspace version 1, size = 536866 kB
no label, UUID=dd6a01c8-93f0-41e0-9b7a-306956d8821b
user@computer:~$ sudo swapon /mnt/512Mb.swap
user@computer:~$ cat /proc/meminfo
MemTotal: 499496 kB
MemFree: 9156 kB
Buffers: 4748 kB
Cached: 233140 kB
SwapCached: 724 kB
Active: 254432 kB
Inactive: 157920 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 499496 kB
LowFree: 9156 kB
SwapTotal: 524280 kB
SwapFree: 523556 kB
Dirty: 128 kB
Writeback: 0 kB
Mapped: 243420 kB
Slab: 20672 kB
CommitLimit: 774028 kB
Committed_AS: 648680 kB
PageTables: 2224 kB
VmallocTotal: 524280 kB
VmallocUsed: 5708 kB
VmallocChunk: 518176 kB
user@computer:~$ gksudo gedit /etc/fstab
user@computer:~$ free
total used free shared buffers cached
Mem: 499496 479488 20008 0 8256 215892
-/+ buffers/cache: 255340 244156
Swap: 524280 3856 520424
#####Then, after running a few more programs...
user@computer:~$ free
total used free shared buffers cached
Mem: 499496 492768 6728 0 1240 142336
-/+ buffers/cache: 349192 150304
Swap: 524280 53384 470896

#####Next, reboot to make sure it will work consistently.
user@computer:~$ free
total used free shared buffers cached
Mem: 499496 493136 6360 0 7528 174700
-/+ buffers/cache: 310908 188588
Swap: 524280 17148 507132

Revoke your changes:


Basically, you just need to re-do what you just did.


gksudo gedit /etc/fstab

Delete the last row added.


/mnt/512Mb.swap  none  swap  sw  0 0

Delete the SWAp in the existing system and delete the created swap file.


sudo swapoff /mnt/512Mb.swap && sudo rm /mnt/512Mb.swap

No need to restart.


Of course, none of the examples can be used.If the dual system is installed, we can set the swap area in the extended partition, that is, the storage area shared by Ubuntu and windows,The corresponding directory should also be from/mnt/512mb. change swap to/Media/XXX/512mb. swap and XXX are different, depending on how you name your extended partition.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.