一、問題
當安裝虛拟機時系統時沒有設定swap大小或設定記憶體太小,編譯程式會出現virtual memory exhausted: Cannot allocate memory的問題,可以用swap擴充記憶體的方法。
二、解決方法
在執行free -m的是時候提示Cannot allocate memory:
(swap檔案可以放在自己喜歡的位置如/var/swap)
[[email protected] byrd]# free -m
total used free shared buffers cached
Mem: 512 108 403 0 0 28
-/+ buffers/cache: 79 432
Swap: 0 0 0
[[email protected] ~]# mkdir /opt/images/
[[email protected] ~]# rm -rf /opt/images/swap
[[email protected] ~]# dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 82.7509 s, 25.3 MB/s
[[email protected] ~]# mkswap /opt/images/swap
mkswap: /opt/images/swap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=59daeabb-d0c5-46b6-bf52-465e6b05eb0b
[[email protected] mnt]# swapon /opt/images/swap
[[email protected] mnt]# free -m
total used free shared buffers cached
Mem: 488 481 7 0 6 417
-/+ buffers/cache: 57 431
Swap: 999 0 999
記憶體太小,增加記憶體可以解決。
使用完畢後可以關掉swap:
[[email protected] mnt]# swapoff swap
[[email protected] mnt]# rm -f /opt/images/swap
swap檔案也可以不删除,留着以後使用,關鍵是你的虛拟機硬碟夠用。
————————————————
版權聲明:本文為CSDN部落客「烏托邦2号」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。
原文連結:https://blog.csdn.net/taiyang1987912/article/details/41695895/