LinuxSwap swap partition settings

Source: Internet
Author: User
About swap partition settings: a popular saying that swap transmits messages by region is that the swap partition size should be twice the memory size when Linux is installed. That is to say, if the memory is 2 GB, 4 GB hard disk space should be split as swap space. In fact, this is a serious waste. Real...

About swap partition settings:

A popular saying that the swap partition should be twice the size of memory when installing the Linux system. That is to say, if the memory is 2 GB, 4 GB hard disk space should be split as swap space. In fact, this is a serious waste. The actual situation is: you can decide the size of the swap partition based on the memory size of your system and the program you are using, or even do not need to swap the partition at all!

First, explain what is swap partition. Swap partition: swap, which means "swap" and "physical transaction ". When the memory is insufficient, the operating system first saves the data temporarily unused in the memory to the swap space on the hard disk to free up the memory for other programs to run, and Windows virtual memory (pagefile. sys.

Make a hypothesis: if your memory is large enough and there is very little insufficient memory, you do not need to swap partitions. In fact, this possibility exists completely. The current 1 GB memory computer is nothing. Computers with frequent 4G memory are also becoming increasingly popular. In daily use, the full memory is rarely used. When the memory capacity is not used up, we need to allocate two times of hard disk space for memory swap. isn't that a waste?

It can be said that when your memory is enough, you can avoid swap space. The same is true in Windows. in system attributes, the virtual memory is set to 0, and the system still runs well. Of course, if you are using a server, you still need dedicated virtual memory. However, the virtual memory size does not have to be twice the memory size.

So how do you know if your system uses swap space? You only need to run the following command under the root user.
Free-m
In daily applications, the usage of swap space is 0 through the above command, so you do not need a lot of virtual memory, or even do not need another hard disk space as virtual memory. So, if one day you need it, do you need to reinstall the system? This is not necessary. in Linux, the virtual memory can not only be placed in a separate swap partition, but also a swap file under a normal partition.

---------------------------------------------------------------------

1. view the swap space (total ):
# Free-m default ticket

The bit is k, and the unit of-m is M.
Total used free shared buffers cached
Mem: 377 180 197 0 19 110
-/+ Buffers/cache: 50 327
Swap: 572 0 572

2. view the swap space (file (s)/partition (s )):

Including file and partition details

# Swapon-s
Equivalent
# Cat/proc/swaps
3. add swap space
Two options: add a swap partition or add a 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.

3.1 add a swap partition
The procedure is as follows:
A uses fdisk to create swap partitions (assuming/dev/sdb2 is the swap partition created)
B. use the mkswap command to set the swap partition:
# Mkswap/dev/sdb2
C enable swap partition:
# Swapon/dev/sdb2
D write/etc/fstab to enable it during boot:
/Dev/sdb2 swap defaults 0 0

3.2 add an exchange file
A creates a m swap file:
# Dd if =/dev/zero of =/swapfile1 bs = 1024 k count = 512
B. use the mkswap command to set the swap file:
# Mkswap/swapfile1
C enable swap partition:
# Swapon/swapfile1
D write/etc/fstab to enable it during boot:
/Swapfile1 swap defaults 0 0

After a new swap partition is added and enabled, check the output of cat/proc/swaps or the free command to ensure that the swap partition is enabled.

4. delete a swap space:
A disable swap partition:
# Swapoff/dev/sdb2
B. delete the project from/etc/fstab;
C. use fdisk or yast to delete a partition.

The process for deleting a swap file is the same as that for deleting a swap file.


--------------------------------------------------------------------------------

# Free-m
Total used free shared buffers cached
Mem: 377 180 197 0 19 110
-/+ Buffers/cache: 50 327
Swap: 572 0 572

The Mem row shows the memory usage from the system perspective. total indicates the available memory size of the system, which is equal to the physical memory of the system minus the memory reserved by the kernel.
Buffers and cached are the memory used by the system as a buffer. buffers is associated with a block device, including the metadata of the file system, and tracks block changes. The cache only contains the file itself.

-/+ The buffers/cache row displays the memory information from the user's perspective. the number of available memory is equal to the used column value of the mem row minus the size of the buffers and cached memory.

Since buffers and cached are set by the operating system to speed up system running, they can only be used as needed by users.

Top displays the dynamic and real-time status of processes (cpu and memory) during system running)

The top table contains three columns, virt res shr, which indicate the memory usage of the process.
VIRT identifies the total memory size that a process can use, including the memory actually used by the process, mapped files, and memory shared with other processes.
RES indicates the memory occupied by the process.
SHR identifies the memory and library size that can be shared with other processes.

Vmstat displays memory usage

These commands read memory information from/proc/meminfo.

The meanings of each line in/proc/meminfo are described in the Documentation/filesystems/proc.txt file of the kernel source code.

The actual memory size of the system can be viewed in dmesg | grep [mM] [eE] [mM ].

/Proc/pid/status: displays the detailed status of a process.
# Cat, proc, 5346, and status
Name: bash
State: S (sleeping)
SleepAVG: 98%
Tgid: 5346
Pid: 5346
PPid: 5343
TracerPid: 0
Uid: 1000 1000 1000 1000
Gid: 104 104 104 104
FDSize: 256.
Groups: 6 24 29 44 104 113 1000
VmPeak: 6528 kB
VmSize: 6528 kB
VmLck: 0 kB
VmHWM: 1976 kB
VmRSS: 1976 kB
VmData: 752 kB
VmStk: 84 kB
VmExe: 644 kB
VmLib: 1788 kB
VmPTE: 16 kB
Threads: 1
SigQ: 0/4294967295
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000384004
SigCgt: 255.255.4b813efb
CapInh: 0000000000000000
Cappr: 0000000000000000
CapEff: 0000000000000000

VmSize indicates the total memory size of the process, which is consistent with the VIRT in the top output.

Author: "City of the Sky"
 

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.