Linux 虚拟内存的使用

使用阿里云服务器经常遇到内存不足造成服务器挂掉的问题,没办法改变物理内存太小,那就弄虚拟内存吧。

Linux系统实现虚拟内存有两种方法:交换分区(swap分区)和交换文件

交换文件

  • 查看内存 (-m显示单位为MB,-g显示单位为GB)
1
2
3
4
5
6
7
8
9
[root@caoxl /]# free -m
total used free shared buff/cache available
Mem: 3789 111 3079 9 599 3435
Swap: 0 0 0

[root@caoxl /]# free -g
total used free shared buff/cache available
Mem: 3 0 3 0 0 3
Swap: 0 0 0
  • 创建一个文件
1
2
3
4
[root@caoxl /]# touch swap
[root@caoxl /]# ls
bin data download home lib64 media opt root sbin swap tmp var
boot dev etc lib lost+found mnt proc run srv sys usr
  • 使用dd命令,创建大小为2G的文件swap
1
2
3
4
[root@caoxl /]# dd if=/dev/zero of=/swap bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 11.7135 s, 183 MB/s
  • if - input_file 输入文件
  • of - output_file 输出文件
  • bs - block_size 块大小
  • count 表示计数
  • 格式化交换文件
1
2
3
[root@caoxl /]# mkswap swap
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=f77dd6aa-0625-47e9-9eb9-bf26050a8508
  • 启用交换文件
1
2
[root@caoxl /]# swapon swap
swapon: /swap: insecure permissions 0644, 0600 suggested.
  • 开机自动加载虚拟内存,在/etc/fstab文件中加入如下命令
1
2
[root@caoxl etc]# vim fstab 
/swap swap swap defults 0 0
  • 重启后生效reboot
1
2
3
4
[root@caoxl /]# free -m
total used free shared buff/cache available
Mem: 3789 110 1309 4 2369 3424
Swap: 2047 0 2047

删除交换分区和交换文件

  • 先删除/etc/fstab文件中添加的交换文件行

  • 停用交换文件

1
[root@caoxl /]# swapoff /swap
  • 删除交换文件
1
[root@caoxl /]# rm -rf ./swap 
1
2
3
4
[root@caoxl /]# free -m
total used free shared buff/cache available
Mem: 3789 109 3416 4 264 3454
Swap: 0 0 0

交换分区

  • 查看硬盘信息
1
2
3
4
5
6
7
8
9
10
11
[root@caoxl ~]# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x0008d73a

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 83884031 41940992 83 Linux
  • 查看挂载信息
1
2
3
4
5
6
7
8
[root@caoxl ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 40G 13G 26G 33% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 484K 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
tmpfs tmpfs 379M 0 379M 0% /run/user/0
  • 以root身份创建分区 hdb1 fdisk /dev/vda1 然后 w 保存退出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@caoxl /]# sudo fdisk /dev/vda1
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xc5d30913.

Command (m for help): n // 新增分区
Partition type:
p primary (0 primary, 0 extended, 4 free) // 主分区
e extended // 扩展分区
Select (default p): e // 选择要创建的分区类型
Partition number (1-4, default 1): 1 // 分区编号
First sector (2048-83881983, default 2048): // 柱面起始值,直接回车默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-83881983, default 83881983): +1G
Partition 1 of type Extended and of size 1 GiB is set

Command (m for help): w // 保存分区表
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 22: Invalid argument.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
  • 查看分区
1
2
3
4
5
6
7
8
9
10
11
12
Command (m for help): p

Disk /dev/vda1: 42.9 GB, 42947575808 bytes, 83881984 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
Disk label type: dos
Disk identifier: 0xc5d30913

Device Boot Start End Blocks Id System
/dev/vda1p1 2048 2099199 1048576 5 Extended
/dev/vda1p2 2099200 3147775 524288 83 Linux
  • 设置交换分区
1
[root@caoxl /]# mkswap /dev/vda1
  • 启用交换分区
1
[root@caoxl /]# swapon /dev/vda1
  • 开机自动加载虚拟内存,在/etc/fstab文件中加入如下命令
1
/dev/vda1 swap swap defults 0 0

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2023 Keep It Simple And Stupid All Rights Reserved.

访客数 : | 访问量 :