swap交换分区及buffers和cached

本文围绕Linux系统磁盘管理展开,介绍了swap交换分区的作用、大小计算及创建方法,还解析了buffers和cached的功能,给出释放缓存的命令。此外,讲解了开机自动挂载文件的配置及参数,以及df、du命令在检查挂载点使用情况和统计磁盘容量方面的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

swap交换分区

swap 是 linux 系统磁盘管理的一块特殊的分区,当实际的物理内存不足的时候,操作系统会从整个内存中,取出一部分暂时没在使用的内存,拿出来放到交换分区,从而提供给当前正在使用的程序,可以使用更多的内存。
使用 swap 分区的作用是,通过操作系统的调取,程序可以用到的实际内存,会远大于物理内存。
swap 分区大小,必须根据物理内存和硬盘容量来计算
-当你的物理内存小于 1G,必须使用 swap 提升内存使用量
-内存使用过多的应用程序,比如图像,视频等,必须用 swap 分区防止物理内存不足,造成软件崩溃。
-当你的电脑休眠,内存中的数据会放入 swap 交换分区中,当你的电脑恢复后,再从 swap 中读取数据,恢复软件正常工作

创建 swap 分区
centos7 系统,计算 swap 分区如下:
-内存小于2G, swap 分配和内存同样大小的空间
-内存大于2G, swap 也就分配2G空间

1.先给磁盘分一个区,得指定 swap 分区类型, fdisk /dev/sdc
2.针对磁盘分区进行格式化 mkswap /dev/sdc1
3.再来使用 swap 分区 swapon 开启交换空间 swapoff 关闭交换空间

swap 代码验证

root@ubuntu:~# free -m
               total        used        free      shared  buff/cache   available
Mem:            3870         660        2636          13         573        2968
Swap:           2047           0        2047

root@ubuntu:~# fdisk -l | grep sdc
Disk /dev/sdc: 120 GiB, 128849018880 bytes, 251658240 sectors
/dev/sdc1      34   976562   976529 476.8M Linux filesystem
/dev/sdc2  978944 97656831 96677888  46.1G Linux filesystem

第一种 GPT格式 磁盘分区 创建 swap 交换分区
# 进入创建分区磁盘
root@ubuntu:~# parted /dev/sdc
GNU Parted 3.4
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p    # 打印磁盘分区信息                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 129GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  500MB   500MB   ext4         primary
 2      501MB   50.0GB  49.5GB  ntfs         logical

# 创建主分区
(parted) mkpart primary 50001 52000                                       
(parted) p   # 打印分区信息                                                             
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 129GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  500MB   500MB   ext4         primary
 2      501MB   50.0GB  49.5GB  ntfs         logical
 3      50.0GB  52.0GB  1999MB               primary

# 创建 swap 交换分区
(parted) t                                                                
Partition number? 3                                                       
Flag to Invert? swap                                                      
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 129GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  500MB   500MB   ext4         primary
 2      501MB   50.0GB  49.5GB  ntfs         logical
 3      50.0GB  52.0GB  1999MB               primary  swap

(parted) q     # 退出                                                           
Information: You may need to update /etc/fstab.


root@ubuntu:~# fdisk -l | grep sdc
Disk /dev/sdc: 120 GiB, 128849018880 bytes, 251658240 sectors
/dev/sdc1        34    976562   976529 476.8M Linux filesystem
/dev/sdc2    978944  97656831 96677888  46.1G Linux filesystem
/dev/sdc3  97658880 101562367  3903488   1.9G Linux swap
root@ubuntu:~# parted /dev/sdc
GNU Parted 3.4
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 129GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  500MB   500MB   ext4         primary
 2      501MB   50.0GB  49.5GB  ntfs         logical
 3      50.0GB  52.0GB  1999MB               primary  swap

(parted) q   
# 对 /dev/sdc3 分区进行 swap 格式化
root@ubuntu:~# mkswap /dev/sdc3
Setting up swapspace version 1, size = 1.9 GiB (1998581760 bytes)
no label, UUID=6afd7ed9-2ff8-4306-8721-9030287a5609
# 查看磁盘分区信息
root@ubuntu:~# parted /dev/sdc
GNU Parted 3.4
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 129GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system     Name     Flags
 1      17.4kB  500MB   500MB   ext4            primary
 2      501MB   50.0GB  49.5GB  ntfs            logical
 3      50.0GB  52.0GB  1999MB  linux-swap(v1)  primary  swap
 root@ubuntu:~# fdisk -l | grep sdc
Disk /dev/sdc: 120 GiB, 128849018880 bytes, 251658240 sectors
/dev/sdc1        34    976562   976529 476.8M Linux filesystem
/dev/sdc2    978944  97656831 96677888  46.1G Linux filesystem
/dev/sdc3  97658880 101562367  3903488   1.9G Linux swap



第二中  MBR格式 磁盘分区  创建 swap 交换分区
root@ubuntu:~# fdisk -l | grep sd
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
/dev/sda1     2048      4095      2048    1M BIOS boot
/dev/sda2     4096   1054719   1050624  513M EFI System
/dev/sda3  1054720 209713151 208658432 99.5G Linux filesystem
Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk /dev/sdc: 120 GiB, 128849018880 bytes, 251658240 sectors
/dev/sdc1        34    976562   976529 476.8M Linux filesystem
/dev/sdc2    978944  97656831 96677888  46.1G Linux filesystem
/dev/sdc3  97658880 101562367  3903488   1.9G Linux swap
root@ubuntu:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.37.2).
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/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: VMware Virtual S
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: 0xf4813d41

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): 
First sector (2048-209715199, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199): +500M

Created a new partition 1 of type 'Linux' and of size 500 MiB.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   pri
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值