Linux虚拟内存不足,扩展虚拟内存的解决办法
virtual memory exhausted: Cannot allocate memory
在编译软件的时候,出现了这个错误:
virtual memory exhausted: Cannot allocate memory
使用free -m命令检查swap分区情况
lk@Mibook:~$ free -m
total used free shared buff/cache available
Mem: 3828 1212 113 1022 2501 1315
Swap: 976 856 120
发现交换分区只有976M ,显然太小了,所以考虑增加虚拟内存。
1 创建用于交换分区的空间
标准格式:
$ dd if=/dev/zero of=/mnt/swap bs=1024 count=4096000
运行后:
lk@Mibook:~$ sudo dd if=/dev/zero of=/mnt/swap bs=1024 count=4096000
4096000+0 records in
4096000+0 records out
4194304000 bytes (4.2 GB, 3.9 GiB) copied, 19.1016 s, 220 MB/s
count=1024 代表设置1G大小swap分区,
我这里设置了4096,也就是4个G大小的swap分区。
2 设置交换分区文件
标准格式:
$ mkswap /mnt/swap
运行后:
lk@Mibook:~$ sudo mkswap /mnt/swap
Setting up swapspace version 1, size = 3.9 GiB (4194299904 bytes)
no label, UUID=3a46d2c9-2eba-433e-ba9a-6e98a5f256ba
3 立即启用交换分区文件
标准格式:
$ swapon /mnt/swap
运行后:
lk@Mibook:~$ sudo swapon /mnt/swap
swapon: /mnt/swap: insecure permissions 0644, 0600 suggested.
查看一下/etc/rc.local,如果有swapoff -a需要改为swapon -a
lk@Mibook:~$ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
exit 0
4 设置开机启用swap分区
查看/etc/fstab文件
lk@Mibook:~$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during installation
UUID=388726b7-1e49-429a-b9dd-7e285931159b / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/sda1 during installation
UUID=5E17-7068 /boot/efi vfat umask=0077 0 1
# swap was on /dev/sda3 during installation
UUID=600bf7c2-11c2-4e94-9ee5-062cbe203afc none swap sw 0 0
使用sudo gedit /etc/fstab命令打开,在末尾添加/mnt/swap swap swap defaults 0 0
再次使用cat /etc/fstab命令查看确认
lk@Mibook:~$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during installation
UUID=388726b7-1e49-429a-b9dd-7e285931159b / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/sda1 during installation
UUID=5E17-7068 /boot/efi vfat umask=0077 0 1
# swap was on /dev/sda3 during installation
UUID=600bf7c2-11c2-4e94-9ee5-062cbe203afc none swap sw 0 0
/mnt/swap swap swap defaults 0 0
5 查看新的分区情况
再次执行free -m查看效果
lk@Mibook:~$ free -m
total used free shared buff/cache available
Mem: 3828 1233 254 965 2340 1352
Swap: 4976 806 4169
发现swap分区变成了4个G,扩展成功。
当Linux系统提示'virtual memory exhausted: Cannot allocate memory'时,可以通过创建交换分区来扩展虚拟内存。本文详细介绍了如何创建交换分区空间、设置交换分区文件、立即启用、设置开机启动以及检查分区变化,成功将swap分区从976M扩展到4G。

1005

被折叠的 条评论
为什么被折叠?



