Linux 之编译安装时内存不足
问题描述
如果出现错误提示:virtual memory exhausted: Cannot allocate memory
,开启 swap
内存交换空间即可解决。
先用 free -m 查看是否有 swap 空间
> free -m
total used free shared buff/cache available
Mem: 990 352 439 54 199 442
Swap: 0 0 0
如果出现:Swap: 0 0 0
,这样的情况可以往下走。
创建用于交换分区的文件
dd if=/dev/zero of=/mnt/swap bs=1M count=2048
设置交换分区文件
mkswap /mnt/swap
立即启用交换分区文件
swapon /mnt/swap
查看 /etc/rc.local
如果在 /etc/rc.local
中有 swapoff -a
需要修改为 swapon -a
,没有的话忽略即可。
> cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
swapoff -a
附录:
1. 关闭删除分区
swapoff /mnt/swap
rm /mnt/swap
2. 设置开机时自启用 swap 分区
需要修改文件 /etc/fstab
中的 swap
行,如果没有增加一行即可。
添加 /mnt/swap swap swap defaults 0 0
。