一、交换分区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. openGL官方Glut库配置教程

    在配置前要先安装好Visual Stdio环境 官方下载网站 注:一台Windows操作系统中可以存在多版本的Visual Stdio,多个版本之间互不干扰但不共享插件库,且高版本向下兼容,因此笔者更 ...

  2. Java实现操作系统中四种动态内存分配算法:BF+NF+WF+FF

    1 概述 本文是利用Java实现操作系统中的四种动态内存分配方式 ,分别是: BF NF WF FF 分两部分,第一部分是介绍四种分配方式的概念以及例子,第二部分是代码实现以及讲解. 2 四种分配方式 ...

  3. Magicodes.IE.ASPNETCore之多样化接口使用

    1.安装包 Install-Package Magicodes.IE.AspNetCore 2.开始配置 在Startup.cs的Configure()方法中,在UseRouting()中间件之后,注 ...

  4. 8.switch语句

    switch语句语法 switch(expression){ case value : //语句 break; //可选 case value : //语句 break; //可选 //你可以有任意数 ...

  5. 命令行运行py文件报错

    起因 今天用ubuntu 终端运行py文件报了个错,找不到模块? 我切换回pycharm中运行,运行一切正常 解决 在报错模块中,插入绝对路径 import sys sys.path.append(' ...

  6. MySQL批量删除数据表

    SELECT CONCAT('drop table ',table_name,';') FROM information_schema.`TABLES` WHERE table_schema='数据库 ...

  7. 【Vue】Vue学习(一)-Vue指令

    1.v-text v-text主要用来更新文本,等同于JS的text属性 <span v-text="msg"></span> 这两者等价 <span ...

  8. Android平台dalvik模式下java Hook框架ddi的分析(2)--dex文件的注入和调用

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/77942585 前面的博客<Android平台dalvik模式下java Ho ...

  9. Average Score39届亚洲赛牡丹江站A题

    题意:       A班有n个人,B班有m个人,然后现在给你n-1个A班人的成绩,和m个B班人的成绩,然后题目要求求出A班剩下的没给成绩那个人的成绩范围,要求是这个人从A班转到B班后能使A,B的平均分 ...

  10. 常见设备/CMS弱口令

    目录 tomcat Apache axis2 Apache ActiveMQ zabbix RabbitMQ zentao