Linux_交换分区SWAP
一、交换分区SWAP
1️⃣:交换分区SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用。
2️⃣:交换分区一般指定虚拟内存的大小为实际内存的1~1.5倍。
3️⃣:如果实际内存超过8GB,可以直接划分16GB给虚拟内存即可,如果虚拟内存不够用的情况,须增加一个虚拟磁盘,由于不能给原有的磁盘重新分区,所以可以选择新建。
二、创建SWAP
1、方式一:创建SWAP文件
使用ddif=/dev/zeroof=/root/swapbs=1M count=2048创建2G大小的文件- if:指定源- -般写/dev/zero
- of:指定目标
- bs:定义块大小
- count:数量
- if:指定源- -般写/dev/zero
[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的更多相关文章
- Linux 交换分区swap
Linux 交换分区swap 一.创建和启用swap交换区 如果你的服务器的总是报告内存不足,并且时常因为内存不足而引发服务被强制kill的话,在不增加物理内存的情况下,启用swap交换区作为虚拟内存 ...
- Linux设置交换分区swap
参考: http://www.vpser.net/opt/vps-add-swap.html https://www.zntec.cn/archives/vps-swap.html http://yz ...
- mount挂载和交换分区swap
目录 mount挂载 挂载方法 选项 查看设备 卸载命令 文件挂载配置文件fstab 交换文件与分区 swap优先级 三个工具free,df,du 扩展 移动介质 使用光盘 挂载USB设备 mount ...
- linux 交换分区 swap
linux swap分区用来保证内存过载时也可以使用,是在磁盘级别对内存的一次扩展,swap分区必须是一个单独的分区 创建过程: 1.用fdisk 命令新建分区,在创建过程中通过L命令和t命令来调整分 ...
- Linux交换分区swap
一.SWAP 说明 1.1 SWAP 概述 当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被 ...
- 41 修改树莓派交换分区 SWAP 的大小
http://blog.lxx1.com/3289 SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用.它和Windows系统的交 ...
- CentOS设置交换分区swap
环境查看 查看未设置交换分区之前 free -h 新加一块磁盘用于交换分区/dev/sdc 格式化 mkswap /dev/sdc 设置为交换分区 swapon /dev/sdc 再次查看 设置为重启 ...
- 交换分区swap
一.查看当前的交换分区[root@server0 ~]# free -mtotal used free shared buff/cache availableMem: 489 140 145 ...
- 【转】交换分区SWAP
SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用. 它和Windows系统的交换文件作用类似,但是它是一段连续的磁盘空间,并且对 ...
随机推荐
- [Kong 与 Konga 与 Postgres数据库] 之 Kuberneres 部署
1.Kong的概述 Kong是一个clould-native.快速的.可扩展的.分布式的微服务抽象层(也称为API网关.API中间件或在某些情况下称为服务网格)框架.Kong作为开源项目在2015年推 ...
- kong 结合 istio demo
- Rancher 安装和使用-实践
Rancher 安装和使用 Rancher是一个完整的,开源的平台,用于在生产环境中部署和管理容器.它包括Kubernetes,Mesos和Docker Swarm的商业支持发行版,使得在任何基础架构 ...
- servlet学习(一)
Tomcat 注:以下资料摘自孙鑫的<sevlet/JSP深入详解>,仅用于个人学习使用. 一.web技术的发展 早期web是静态页面的浏览,使用HTML编写,放入服务器. 1.1浏览器请 ...
- 万字长文,带你彻底理解EF Core5的运行机制,让你成为团队中的EF Core专家
在EF Core 5中,有很多方式可以窥察工作流程中发生的事情,并与该信息进行交互.这些功能点包括日志记录,拦截,事件处理程序和一些超酷的最新出现的调试功能.EF团队甚至从Entity Framewo ...
- B1029/A1048 旧键盘损坏了,在输入一段文字时坏了的键不可以正常使用,现给出应输入的一段文字,和实际输出的文字,找出坏掉的键。
#include<cstdio> #include<cstring> const int maxn = 1000; bool HashTable[maxn] = { false ...
- C#通过rabbitmq实现定时任务(延时队列)
本文主要讲解如何通过RabbitMQ实现定时任务(延时队列) 环境准备 需要在MQ中进行安装插件 地址链接 插件介绍地址:https://www.rabbitmq.com/blog/2015/04/1 ...
- hdu4454 三分 求点到圆,然后在到矩形的最短路
题意: 求点到圆,然后在到矩形的最短路. 思路: 把圆切成两半,然后对于每一半这个答案都是凸性的,最后输出两半中小的那个就行了,其中有一点,就是求点到矩形的距离,点到矩形的距离 ...
- MySQL三种报错注入方式下的insert,update,delete命令注入示例
select 查询数据(大部分) 在网站应用中进行数据显示查询操作 insert 插入数据 在网站应用中进行用户注册添加等操作 delete 删除数据 后台管理里面删除文章删除用户等操作 update ...
- PHP 判断当前日期是否是法定节假日或者休息日
1.代码如下 $date = date("Ymd",time()); $url = "http://api.goseek.cn/Tools/holiday?date=&q ...