linux中swap的构建
Posted 正义的朋友
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux中swap的构建相关的知识,希望对你有一定的参考价值。
一.使用物理分区构建swap
1.分区
[[email protected] ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (2050048-41943039, default 2050048):
Using default value 2050048
Last sector, +sectors or +size{K,M,G} (2050048-41943039, default 41943039): +500M
Partition 3 of type Linux and of size 500 MiB is set
Command (m for help): p
Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x7afa732b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 1026047 512000 83 Linux
/dev/vdb2 1026048 2050047 512000 83 Linux
/dev/vdb3 2050048 3074047 512000 83 Linux
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[[email protected] ~]# partprobe *更新分区表
2.格式化
[[email protected] ~]# mkswap /dev/vdb3
Setting up swapspace version 1, size = 511996 KiB
no label, UUID=7d39405c-d91b-44f6-9db4-17cb097909b5
3.开始查看与加载
[[email protected] ~]# free
total used free shared buff/cache available
Mem: 1016496 239032 446852 13684 330612 605148
Swap: 511996 0 511996
[[email protected] ~]# swapon /dev/vdb3
[[email protected] ~]# free
total used free shared buff/cache available
Mem: 1016496 239624 446232 13684 330640 604556
Swap: 1023992 0 1023992
[[email protected] ~]# swapon -s *列出目前使用swap设备有哪些
Filename Type Size Used Priority
/dev/vda2 partition 511996 0 -1
/dev/vdb3 partition 511996 0 -2
二.使用文件构建swap
1.使用dd命令新增一个名为swap的128M的文件在/media下面
[[email protected] media]# dd if=/dev/zero of=/media/swap bs=1M count=128
128+0 records in
128+0 records out
134217728 bytes (134 MB) copied, 0.119075 s, 1.1 GB/s
[[email protected] media]# ll -h swap
-rw-r--r-- 1 root root 128M Jan 9 19:19 swap
2.使用mkswap将swap这个文件格式化为swap的文件格式
[[email protected] media]# mkswap swap
Setting up swapspace version 1, size = 131068 KiB
no label, UUID=f7cf0864-fa43-46d0-9cd6-72fb82d5bfec
3.用swapon 启动
[[email protected] media]# free
total used free shared buff/cache available
Mem: 1016496 239228 312492 13680 464776 603064
Swap: 511996 0 511996
[[email protected] media]# swapon swap
swapon: /media/swap: insecure permissions 0644, 0600 suggested.
[[email protected] media]# free
total used free shared buff/cache available
Mem: 1016496 239228 312492 13680 464776 603064
Swap: 643064 0 643064
[[email protected] media]# swapon -s
Filename Type Size Used Priority
/dev/vda2 partition 511996 0 -1
/media/swap file 131068 0 -2
4.swapoff 关闭swapfile
[[email protected] media]# swapoff swap
[[email protected] media]# free
total used free shared buff/cache available
Mem: 1016496 239244 312476 13680 464776 603048
Swap: 511996 0 511996
三.swap使用限制
swap的主要功能时当物理内存不够时,将某些内存中所占用的程序暂时移动到swap当中,让物理内存可以被需要的程序使用。另外,如果你的主机支持电源管理模式,也就是说,你的linux主机可以进入到“休眠”模式的话,那么,运行当中的程序状态则会被记录到swap去,以作为“唤醒”主机状态的依据。另外,由某些程序在运行时,本来就会利用swap的特性来存放一些数据段,所以,swap时需要创建的,只是不需要太大。但是,swap在创建时,是有限制的:
*在内核2.4.10版本后,单一swap已经没有2GB的限制了
*但是,最多还是仅能创建32个swap
*而且,由于目前x86_64最大内存寻址到64GB,因此,swap总量最大也是仅能达到64GB。
以上是关于linux中swap的构建的主要内容,如果未能解决你的问题,请参考以下文章
虚拟机无法分配内存 virtual memory exhausted: Cannot allocate memory