How to add swap partitions in centos

Source: Internet
Author: User

Scenario:

Recently, the server memory is always very small, prompting you to increase the memory space, but adding the memory requires money, but how can you increase the memory at the same time without spending money. So I thought about the virtual memory. I checked it and found that the server didn't set the swap partition. So I split 1 GB from the data disk to use it as the swap space.

Method:

Use the dd command to create a swap partition

[Root @ localhost Desktop] # dd if =/dev/zero of =/home/swap bs = 1024 count = 1048576
   
Calculation Formula of count: count = SIZE * 1024 (size in MB)

In this way, create a/home/swap partition file with the size of 1 GB, and then format the new SWAP partition:

[Root @ localhost Desktop] # mkswap/home/swap


Use the swapon command to convert the file partition into a swap partition.

[Root @ localhost Desktop] # swapon/home/swap
(Run the [root @ localhost Desktop] # swapoff/home/SWAP command to disable swap Partitioning)

Use free-m to check whether swap is expanded.

    

To enable automatic swap mounting, 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 does not need to be manually mounted.

Effect:

[Root @ AY130701184903434d7bZ classes] # free-m
Total used free shared buffers cached
Mem: 491 443 48 0 0 17
-/+ Buffers/cache: 424 66
Swap: 1023 113 910

It can be seen that there is obviously some memory space remaining, and the need to increase the memory will not be reported every time tomcat is restarted.

Supplement: how to configure swap partitions for Alibaba Cloud hosts

When installing Linux, I don't know how much swap space is allocated, so I will allocate one randomly. In actual use, for example, installing Oracle10g requires a lot of swap space, if swap space is insufficient, how can we increase the size of swap space.

The following operations must be performed under the root user. First, create a partition and run the dd command, such

Dd if =/dev/zero of =/home/swap bs = 1024 count = 512000

This will create a partition file such as/home/swap. The file size is 512000 blocks. Generally, 1 block is 1 K, so the space is 512 MB. Then convert the partition into a swap partition.

/Sbin/mkswap/home/swap

Then use the swap partition. Make it valid.

/Sbin/swapon/home/swap

Now, run the free-m command to check the memory and swap partition size, and we can see that the space is increased by MB. However, after the computer is restarted, it is found that swap is still so large, the new swap is not automatically started, and manual start is required. Modify the/etc/fstab file and add the following line:

/Home/swap defaults 0 0

You will find that swap space increases after your machine is automatically started.

Supplement:

For the usefulness of swap partitioning, swap puts data into swap when the physical memory is insufficient, so swap plays a role in virtual memory, in a sense, memory space is also increased.

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.