Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習 Linux

編譯報錯 virtual memory exhausted:用 swap 擴充記憶體

編譯報錯 virtual memory exhausted:用 swap 擴充記憶體

問題

安裝虛擬機時若沒有配置 swap,或記憶體給得太小,編譯時就會出現 virtual memory exhausted: Cannot allocate memory。此時最直接的解法是掛一個 swap 檔案來擴充可用記憶體。

解法

先確認現況,你會發現連 free -m 都提示 Cannot allocate memory,且 Swap 為 0:

[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

接著建立 swap 檔(位置可自訂,這裡放在 /opt/images/):

mkdir /opt/images/
rm -rf /opt/images/swap
dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000
mkswap /opt/images/swap
swapon /opt/images/swap
free -m

dd 會輸出類似訊息:

2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 82.7509 s, 25.3 MB/s
Setting up swapspace version 1, size = 2047996 KiB

掛載後再查一次,Swap 已從 0 變為約 999 MB:

     total   used   free  shared  buffers  cached
Mem:     488    481      7       0        6     417
-/+ buffers/cache:  57    431
Swap:    999      0    999
編譯報錯 virtual memory exhausted:用 swap 擴充記憶體

如果記憶體實在太小,根本解法仍是加記憶體;swap 只是應急。

用完關閉

swapoff swap
rm -f /opt/images/swap

swap 檔也可以留著重複使用,前提是你的虛擬機磁碟空間夠。另外提醒:若要開機自動生效,需把對應行寫入 /etc/fstab,否則每次重啟都得手動 swapon。

Copyright © Linux教程網 All Rights Reserved