[轉]編譯報錯:virtual memory exhausted: Cannot allocate memory

一、問題

當安裝虛擬機時系統時沒有設置swap大小或設置內存太小,編譯程序會出現virtual memory exhausted: Cannot allocate memory的問題,可以用swap擴展內存的方法。

二、解決方法

在執行free -m的是時候提示Cannot allocate memory:

(swap文件可以放在自己喜歡的位置如/var/swap)

[root@Byrd byrd]# free -m
             total       used       free     shared    buffers     cached
Mem:           512        108        403          0          0         28
-/+ buffers/cache:         79        432
Swap:            0          0          0
[root@Byrd ~]# mkdir /opt/images/
[root@Byrd ~]# rm -rf /opt/images/swap
[root@Byrd ~]# 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
[root@Byrd ~]# 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
[root@hz mnt]# swapon /opt/images/swap
[root@hz 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:

[root@hz mnt]# swapoff swap
[root@hz mnt]# rm -f /opt/images/swap

swap文件也可以不刪除,留着以後使用,關鍵是你的虛擬機硬盤夠用。

參考原文

[1] https://blog.csdn.net/taiyang1987912/article/details/41695895

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章