Manually add or delete virtual memory swap zones in CentOS

Source: Internet
Author: User
Tags centos

The released space may come from some programs that haven't been operated for a long time. The released space is temporarily saved to the Swap space and will be executed by those programs, then, the stored data is restored from Swap to the memory. In this way, the system always performs Swap switching when the physical memory is insufficient. In fact, Swap adjustment is crucial to the performance of Linux servers, especially Web servers. By adjusting Swap, the system performance bottleneck is sometimes crossed to save system upgrade costs. SWAP is a virtual memory in Linux. It is used to expand the physical memory and store temporary data. It is similar to virtual memory in Windows. In Windows, files can only be used as virtual memory. In linux, files or partitions can be used as virtual memory.

First, check the current memory and swap space (default unit: k,-m unit: M ):

Free-m

We can see that the total memory is 503 MB, and SWAP does not exist.

View swap information, including file and partition details

# Swapon-s or

# If neither cat/proc/swaps exists, we need to manually add swap partitions. Note that VPS in OPENVZ architecture does not support manual addition of swap partitions.

You can add a swap partition or an swap file. We recommend that you add a swap partition. However, if you do not have much free space available, you can add swap files.

1. Add swap files

1. Use the dd command to create a swap file

Dd if =/dev/zero of =/home/swap bs = 1024 count = 1024000 to create a/home/swap partition file with the size of 1 GB.

2. Create a file in swap format:

Mkswap/home/swap3. Run the swapon command to mount the file partition to the swap partition.

/Sbin/swapon/home/swap we can use the free-m command to check whether swap partitions exist.

However, after the system is restarted, the swap partition becomes 0 again.

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

Add vi/etc/fstab to the end of the file (the last line)

/Home/swap default 0 0. Even if the system is restarted, the swap partition still has a value.

Delete swap files

1. Stop swap partition first

/Sbin/swapoff/home/swap2. Delete the swap partition file.

Rm-rf/home/swap3. Delete the automatic mounting configuration command

Vi/etc/fstab delete this line

/Home/swap default 0 0 so that you can delete the manually added swap file.
Note:
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.

 
II. Use partitions for SWAP (virtual memory ).

1. Use fdisk to create swap partitions (assuming/dev/sdb2 is the swap partition created)
2. Use the mkswap command to set the swap partition:

# Mkswap/dev/sdb23 enable swap partition:

# Swapon/dev/sdb24 write/etc/fstab to enable it during boot:

/Dev/sdb2 swap defaults 0 0 delete swap partition
The procedure is as follows:


1. Stop swap partition first

/Sbin/swapoff/dev/sdb22. Delete the automatic mounting configuration command.

Vi/etc/fstab delete this line

/Dev/sdb2 swap defaults 0 0 so that you can delete the manually added swap partition.


Summary
In fact, we can think that the memory used by the program is all virtual memory, so there is no physical memory limit (but it is still limited to the number of computer addressing digits, such as 32-bit and 64-bit, because the virtual memory system needs to use physical memory ). He can put a portion of his memory in the physical memory, and a portion of his cache into the disk. In addition, the virtual memory has a corresponding virtual address. Therefore, the virtual address can be used for each program, it can be mapped to different physical addresses, that is, every program can think of itself as having the memory of the entire machine.

Related Article

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.