【linux】实战扩展swap分区

swap 分区

Swap分区在系统的物理内存不够用的时候,把硬盘空间中的一部分空间释放出来,以供当前运行的程序使用。

基础命令:

mkswap /devices : 格式化成swap格式
swapon /devices : 激活swap ,加入到swap分区中
开机自动启动新添加的swap分区: /etc/fstab /devices swap swap defaults 0 0

实验步骤:

  1. 建立分区
    [root@xinsz08 ~]# gdisk /dev/sdb
Command (? for help): n  #新建分区
Partition number (2-128, default 2):     #回车
First sector (34-41943006, default = 2099200) or {+-}size{KMGTP}:  #回车
Last sector (2099200-41943006, default = 41943006) or {+-}size{KMGTP}: +1G  #给1G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):  #回车
Changed type of partition to 'Linux filesystem'

Command (? for help): w  #保存
Do you want to proceed? (Y/N): y

2) 查看是否分区成功

[root@centos7-xinsz08 ~]# ll /dev/sdb*
brw-rw----. 1 root disk 8, 16 226 15:36 /dev/sdb
brw-rw----. 1 root disk 8, 17 226 15:36 /dev/sdb1
brw-rw----. 1 root disk 8, 18 226 15:37 /dev/sdb2

  1. 格式化
[root@centos7-xinsz08 ~]# mkswap /dev/sdb2
正在设置交换空间版本 1,大小 = 1048572 KiB
无标签,UUID=bf1788b2-c1f5-4f89-bb62-cd91617ee8dc

对比( 激活swap空间之前的对比)

[root@centos7-xinsz08 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3770         454        2938          14         378        3082
Swap:          2047           0        2047
[root@centos7-xinsz08 ~]# swapon /dev/sdb2 
[root@centos7-xinsz08 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3770         452        2940          14         377        3084
Swap:          3071           0        3071

使用 文件增加swap空间?
阿里云默认的没有swap空间,如何增加swap?

1) 使用dd命令创建个500M大小的文件
[root@centos7-xinsz08 ~]# dd  if=/dev/zero of=swap_file bs=1M count=500
记录了500+0 的读入
记录了500+0 的写出
524288000字节(524 MB)已复制,12.2954 秒,42.6 MB/秒
[root@centos7-xinsz08 ~]# ll -h swap_file 
-rw-r--r--. 1 root root 500M 226 15:46 swap_file
[root@centos7-xinsz08 ~]# chmod 0600 swap_file 
2) 把500M的文件格式化成swap 
[root@centos7-xinsz08 ~]# mkswap -f swap_file 
正在设置交换空间版本 1,大小 = 511996 KiB
无标签,UUID=def65bf9-7143-4734-9801-d3d8bb583061
[root@centos7-xinsz08 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3770         455        2423          14         891        3075
Swap:          3071           0        3071
3) 激活swap空间
[root@centos7-xinsz08 ~]# swapon /root/swap_file

4) 查看是否被激活
[root@centos7-xinsz08 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3770         456        2423          14         891        3074
Swap:          3571           0        3571
[root@centos7-xinsz08 ~]# 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值