Create, destroy, and use swap in Linux

Source: Internet
Author: User

How to Create a swap file
1) create a large enough fileDd If =/dev/Zero of =/localdisk/swapfile BS = 1024 COUNT = 4096000(The value of count is equal to 1024 x the size of the file you want, 4096000 is 4G) 2) convert this file into a swap file.Mkswap/localdisk/swapfile3) enable this swap fileSwapon/localdisk/swapfile4) automatically load the swap file every time you start the system. Add a line to the/etc/fstab file./Localdisk/swapfile swap defaults 0 05) view swapCAT/proc/SWAPs

Detailed Method

Swap swap partitions are generally available only in xen VPs, while openvz does not. Swap swap partitions can increase a certain amount of memory usage, but the most important role of adding swap partitions is to prevent the sudden increase of website traffic, resulting in insufficient memory allocation by the system and a crash. If your VPS has been using swap for a long time, we recommend that you check the program or VPs, or upgrade the VPs with larger memory because swap is used to swap partitions, this reduces server performance and leads to slower access speeds. Next we will look at how to add swap partitions.

1. log on to SSH with the root permission and type the following command to create a 1024 MB swap file (512*524288 MB = ).
Dd If =/dev/Zero of =/swapfile1 BS = 1024 COUNT = 524288

2. Create a Linux swap file
Mkswap/swapfile1

3. Activate/swapfile1 to swap files immediately
Swapon/swapfile1

4. Set the system self-activation swap file. Edit the/etc/fstab file and add the following second line of code.
VI/etc/fstab
/Swapfile1 swap defaults 0 0

5. restart the system and use free-m to view the swap size.
 
1. Add the swap area

1. Run the command to view the system memory size.


Free-m

We can see that the memory size is 503 (configured as 512 at the time of installation), and swap is 0.

2. Add swap partitions

Use the DD command to create a swap Partition

Dd If =/dev/Zero of =/home/swap BS = 1024 COUNT = 1024000

In this way, create a/home/swap partition file with a size of 1 GB. Then run the Swapon command to convert the partition into a swap partition.

/Sbin/Swapon/home/swap

Use free-m to check the size of the swap.

This is not done yet, because after the reboot restarts the system, the swap partition becomes 0 again.

To prevent swap partition from changing to 0 after restart, modify the/etc/fstab file.

VI/etc/fstab

Add

/Home/swap default 0 0

In this way, even if the system is restarted, the swap partition still has a value.

Ii. Delete swap partitions

1. Stop swap partition first

/Sbin/swapoff/home/swap

2. Delete the swap partition File

Rm-RF/home/swap

3. Modify the/etc/fstab file and delete the/home/swap default 0 0 line.

In this way, you can delete the manually added partitions.

 
PS: 1. You can only use the root user to delete swap.

2. The swap partition allocated during system installation does not seem to be deleted.

3. The swap partition is generally twice the memory size, but the maximum value is 2 GB.

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.