一、交换分区SWAP

1️⃣:交换分区SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用。

2️⃣:交换分区一般指定虚拟内存的大小为实际内存的1~1.5倍。

3️⃣:如果实际内存超过8GB,可以直接划分16GB给虚拟内存即可,如果虚拟内存不够用的情况,须增加一个虚拟磁盘,由于不能给原有的磁盘重新分区,所以可以选择新建。

二、创建SWAP

1、方式一:创建SWAP文件

  • 使用dd if=/dev/zero of=/root/swap bs=1M count=2048创建2G大小的文件

    • if:指定源- -般写/dev/zero
    • of:指定目标
    • bs:定义块大小
    • count:数量
[root@localhost ~]# dd if=/dev/zero of=/root/swap bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 44.8512 s, 47.9 MB/s
[root@localhost ~]# du -sh /root/swap
2.0G /root/swap
  • 格式化刚刚创建的swap文件
[root@localhost ~]# mkswap -f /root/swap       // -f :指定格式化文件(目标是文件,则加-f ;若为磁盘分区,则不需要加-f)
mkswap: /root/swap: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=20e1675b-3206-497c-b33b-9175be73083f
  • 根据提示设置改格式化后文件的权限为600(此步骤也可以不做)
[root@localhost ~]# ll swap
-rw-r--r-- 1 root root 2147483648 Aug 20 03:03 swap
[root@localhost ~]# chmod 600 /root/swap
[root@localhost ~]# ll swap
-rw------- 1 root root 2147483648 Aug 20 03:03 swap
  • 查看刚刚格式化swap文件的属性
[root@localhost ~]# blkid /root/swap
/root/swap: UUID="20e1675b-3206-497c-b33b-9175be73083f" TYPE="swap"
  • 查看当前SWAP分区情况
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 207Mi 65Mi 5.0Mi 1.5Gi 1.4Gi
Swap: 2.0Gi 3.0Mi 2.0Gi
  • 挂载SWAP分区
[root@localhost ~]# swapon /root/swap
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 209Mi 64Mi 5.0Mi 1.5Gi 1.4Gi
Swap: 4.0Gi 3.0Mi 4.0Gi
  • 卸载SWAP分区
[root@localhost ~]# swapoff /root/swap
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 207Mi 110Mi 5.0Mi 1.5Gi 1.4Gi
Swap: 2.0Gi 3.0Mi 2.0Gi

2、方式二:使用磁盘分区创建SWAP分区

  • 创建一个磁盘主分区
[root@localhost ~]# fdisk /dev/nvme0n2 

Welcome to fdisk (util-linux 2.32.1).
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 (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-104857599, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599): +4G Created a new partition 1 of type 'Linux' and of size 4 GiB.
Partition #1 contains a swap signature. The signature will be removed by a write command. Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
  • 调整刚刚创建的磁盘分区ID为82
[root@localhost ~]# fdisk /dev/nvme0n2
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command. Command (m for help): p //查看分区列表
Disk /dev/nvme0n2: 50 GiB, 53687091200 bytes, 104857600 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
Disklabel type: dos
Disk identifier: 0x8f03f9d8 Device Boot Start End Sectors Size Id Type
/dev/nvme0n2p1 2048 8390655 8388608 4G 83 Linux Command (m for help): t //更改分区ID
Selected partition 1 //默认显著磁盘的第一个分区;如果有多个分区可选,就需要手动选择分区
Hex code (type L to list all codes): 82 //输入分区编号82;可以使用 ‘l’查看看所有的分区ID
Changed type of partition 'Linux' to 'Linux swap / Solaris'. Command (m for help): p //查看分区列表
Disk /dev/nvme0n2: 50 GiB, 53687091200 bytes, 104857600 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
Disklabel type: dos
Disk identifier: 0x8f03f9d8 Device Boot Start End Sectors Size Id Type
/dev/nvme0n2p1 2048 8390655 8388608 4G 82 Linux swap / Solaris //id 为82 ;type 为 Linux swap Command (m for help): w //保存退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
  • 格式化刚刚创建的磁盘分区
[root@localhost ~]# mkswap /dev/nvme0n2p1
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=64bff483-41ba-4180-ae1c-eab2e1075c84

[root@localhost ~]# blkid /dev/nvme0n2p1
/dev/nvme0n2p1: UUID="64bff483-41ba-4180-ae1c-eab2e1075c84" TYPE="swap" PARTUUID="8f03f9d8-01"  
  • 在/etc/fstab文件写入永久挂载(开机自动挂载)
//先查看一下SWAP分区的UUID
UUID="64bff483-41ba-4180-ae1c-eab2e1075c84" //在/etc/fstab文件写入内容
[root@localhost ~]# vim /etc/fstab
..........
UUID="64bff483-41ba-4180-ae1c-eab2e1075c84" swap swap defaults 0 0  
  • 挂载SWAP分区
 //手下查看一下SWAP分区情况
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 204Mi 1.3Gi 8.0Mi 253Mi 1.4Gi
Swap: 2.0Gi 0B 2.0Gi //挂载SWAP分区
[root@localhost ~]# swapon /dev/nvme0n2p1 //查看挂载后的SWAP分区情况
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 208Mi 1.3Gi 8.0Mi 253Mi 1.4Gi
Swap: 6.0Gi 0B 6.0Gi
  • 卸载SWAP分区
 //查看SWAP分区情况
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 208Mi 1.3Gi 8.0Mi 253Mi 1.4Gi
Swap: 6.0Gi 0B 6.0Gi //卸载SWAP分区
[root@localhost ~]# swapoff /dev/nvme0n2p1 //查看卸载后SWAP分区情况
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 204Mi 1.3Gi 8.0Mi 253Mi 1.4Gi
Swap: 2.0Gi 0B 2.0Gi
  • 使用swapon -s 查看所有在使用交换分区
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2146300 0 -2
/dev/nvme0n2p1 partition 4194300 0 -3 //这是刚刚创建的交换分区

  

Linux_交换分区SWAP的更多相关文章

  1. Linux 交换分区swap

    Linux 交换分区swap 一.创建和启用swap交换区 如果你的服务器的总是报告内存不足,并且时常因为内存不足而引发服务被强制kill的话,在不增加物理内存的情况下,启用swap交换区作为虚拟内存 ...

  2. Linux设置交换分区swap

    参考: http://www.vpser.net/opt/vps-add-swap.html https://www.zntec.cn/archives/vps-swap.html http://yz ...

  3. mount挂载和交换分区swap

    目录 mount挂载 挂载方法 选项 查看设备 卸载命令 文件挂载配置文件fstab 交换文件与分区 swap优先级 三个工具free,df,du 扩展 移动介质 使用光盘 挂载USB设备 mount ...

  4. linux 交换分区 swap

    linux swap分区用来保证内存过载时也可以使用,是在磁盘级别对内存的一次扩展,swap分区必须是一个单独的分区 创建过程: 1.用fdisk 命令新建分区,在创建过程中通过L命令和t命令来调整分 ...

  5. Linux交换分区swap

    一.SWAP 说明 1.1 SWAP 概述 当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被 ...

  6. 41 修改树莓派交换分区 SWAP 的大小

    http://blog.lxx1.com/3289 SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用.它和Windows系统的交 ...

  7. CentOS设置交换分区swap

    环境查看 查看未设置交换分区之前 free -h 新加一块磁盘用于交换分区/dev/sdc 格式化 mkswap /dev/sdc 设置为交换分区 swapon /dev/sdc 再次查看 设置为重启 ...

  8. 交换分区swap

    一.查看当前的交换分区[root@server0 ~]# free      -mtotal used free shared buff/cache availableMem: 489 140 145 ...

  9. 【转】交换分区SWAP

    SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用. 它和Windows系统的交换文件作用类似,但是它是一段连续的磁盘空间,并且对 ...

随机推荐

  1. ElasticSearch创建文档

    1. 新建文档 支持自动生成文档 ID 和指定文档 ID 两种方式 通过调用 "post/users/_doc" ,系统会自动生成 document id 使 用HTTP PUT ...

  2. elementui 表格格式化

    <el-table-column prop="userType" label="角色" width="180" :formatter= ...

  3. [Fundamental of Power Electronics]-PART I-3.稳态等效电路建模,损耗和效率-3.2 考虑电感铜损

    3.2 考虑电感铜损 可以拓展图3.3的直流变压器模型,来对变换器的其他属性进行建模.通过添加电阻可以模拟如功率损耗的非理想因素.在后面的章节,我们将通过在等效电路中添加电感和电容来模拟变换器动态. ...

  4. 「Unity卡牌自走棋」项目NABCD分析

    项目 内容 这个作业属于哪个课程 2021学年春季软件工程(罗杰 任健) 这个作业的要求在哪里 团队项目-初次邂逅,需求分析 在这个课程的目标是 锻炼在大规模开发中的团队协作能力 这个作业在哪个具体方 ...

  5. 远程线程注入dll,突破session 0

    前言 之前已经提到过,远线程注入和内存写入隐藏模块,今天介绍突破session 0的dll注入 其实今天写这个的主要原因就是看到倾旋大佬有篇文章提到:有些反病毒引擎限制从lsass中dump出缓存,可 ...

  6. Day16_89_通过反射机制获取所有构造方法

    通过反射机制获取某个特定的构造方法 * 代码 import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; publ ...

  7. ingress controller 和ingress使用实例

    ingress controller安装 k8s集群版本:1.15+ 官方文档: https://kubernetes.github.io/ingress-nginx/ 创建基础配置 kubectl ...

  8. 7.bug生命周期

    new:测试发现并提交bug,状态为new/active; open: 分配bug到开发人员,状态为open: fixed:开发人员处理完bug,将状态改为fixed: closed/reopen:测 ...

  9. Python容器数据类型之间的转换

    强制转换成字符串 str() 将列表(list)转为字符串 var1 = [1, 2, 3, 4] var2 = str(var1) print(type(var2)) # <class 'st ...

  10. 1.7.3- HTML表格属性