内存交换空间(swap)的构建

Posted 青衫lys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了内存交换空间(swap)的构建相关的知识,希望对你有一定的参考价值。

一、使用物理分区构建swap

1、先进行分区的行为。

 1 [[email protected] ~]# fdisk /dev/xvdb
 2 Welcome to fdisk (util-linux 2.23.2).
 3 
 4 Changes will remain in memory only, until you decide to write them.
 5 Be careful before using the write command.
 6 
 7 
 8 Command (m for help): n
 9 Partition type:
10    p   primary (1 primary, 0 extended, 3 free)
11    e   extended
12 Select (default p): p
13 Partition number (1,3,4, default 1): 4
14 First sector (20973568-62914559, default 20973568): 
15 Using default value 20973568
16 Last sector, +sectors or +size{K,M,G} (20973568-62914559, default 62914559): +512M17 Partition 4 of type Linux and of size 10 GiB is set
18 
19 Command (m for help): p
20 
21 Disk /dev/xvdb: 32.2 GB, 32212254720 bytes, 62914560 sectors
22 Units = sectors of 1 * 512 = 512 bytes
23 Sector size (logical/physical): 512 bytes / 512 bytes
24 I/O size (minimum/optimal): 512 bytes / 512 bytes
25 Disk label type: dos
26 Disk identifier: 0x25a2caf5
27 
28     Device Boot      Start         End      Blocks   Id  System
29 /dev/xvdb2            2048    20973567    10485760   83  Linux
30 /dev/xvdb4        20973568    41945087    10485760   83  Linux
31 
32 Command (m for help): t           <==修改系统ID
33 Partition number (2,4, default 4): 4 
34 Hex code (type L to list all codes): 82           <==改成swap的ID
35 Changed type of partition Linux to Linux swap / Solaris
36 
37 Command (m for help): p
38 
39 Disk /dev/xvdb: 32.2 GB, 32212254720 bytes, 62914560 sectors
40 Units = sectors of 1 * 512 = 512 bytes
41 Sector size (logical/physical): 512 bytes / 512 bytes
42 I/O size (minimum/optimal): 512 bytes / 512 bytes
43 Disk label type: dos
44 Disk identifier: 0x25a2caf5
45 
46     Device Boot      Start         End      Blocks   Id  System
47 /dev/xvdb2            2048    20973567    10485760   83  Linux
48 /dev/xvdb4        20973568    41945087    10485760   82  Linux swap / Solaris
49 
50 Command (m for help): w
51 The partition table has been altered!
52 
53 Calling ioctl() to re-read partition table.
54 Syncing disks.

强制内核更新分区表:

[[email protected] ~]# partprobe

2、开始构建swap格式。

[[email protected] ~]# mkswap /dev/xvdb4
Setting up swapspace version 1, size = 5485756 KiB
no label, UUID=033a1d75-88fd-4347-8726-08d856cb9ad1

3、查实查看与加载。

1 [[email protected] ~]# swapon /dev/xvdb4 
2 [[email protected] ~]# free
3              total       used       free     shared    buffers     cached
4 Mem:       1016656     152956     863700       6624      11036      75828
5 -/+ buffers/cache:      66092     950564
6 Swap:     10485756          0   10485756
[[email protected] ~]# swapon -s
Filename                Type        Size    Used    Priority
/dev/xvdb4                                 partition    10485756    0    -1

 

 

二、使用文件系统构建swap

1、使用dd这个命令来新增一个128MB的文件在/data下面:

1 [[email protected] ~]# dd if=/dev/zero of=/data/swap bs=1M count=512
2 512+0 records in
3 512+0 records out
4 536870912 bytes (537 MB) copied, 7.82149 s, 68.6 MB/s

2、使用mkswap将/data/swap这个文件格式化为swap的文件格式:

1 [[email protected] ~]# mkswap /data/swap 
2 Setting up swapspace version 1, size = 524284 KiB
3 no label, UUID=ede63d44-cf78-4d08-92ca-15fb38a625f4

3、使用swapon来将/data/swap启动

 1 [[email protected] ~]# swapon /data/swap 
 2 swapon: /data/swap: insecure permissions 0644, 0600 suggested.
 3 [[email protected] ~]# free -m
 4              total       used       free     shared    buffers     cached
 5 Mem:           992        670        322          6         11        586
 6 -/+ buffers/cache:         72        919
 7 Swap:          511          0        511
 8 [[email protected] ~]# swapon -s
 9 Filename                Type        Size    Used    Priority
10 /data/swap                                 file    524284    0    -1

 

使用swapoff关掉swapfile

1 [[email protected] ~]# swapoff /dev/xvdb4
2 [[email protected] ~]# swapoff /data/swap 

为了能够让swap自动挂载,要修改/etc/fstab文件 

在文件末尾加上 

/data/swap swap swap default 0 0 
这样就算重启系统,swap分区就不用手动挂载了。

 

以上是关于内存交换空间(swap)的构建的主要内容,如果未能解决你的问题,请参考以下文章

虚拟机无法分配内存 virtual memory exhausted: Cannot allocate memory

Linux 交换内存空间原理(swap)(Linux内存管理)(cgroups)

linux交换空间

阿里云云主机添加swap分区与swap性能优化

Linux SWAP 交换分区配置说明(转)

linux啥情况就会使用到交换区swap,比如剩下多少内存,还有其他哪些因素