編譯安裝PHP組件出現錯誤提示記憶體不足:virtual memory exhausted Cannot allocate memory
- 2019 年 12 月 30 日
- 筆記
背景
前面一篇我們介紹了寶塔面板的PHP默認不安裝fileinfo組件,需要手動編譯安裝。在php5.6上是沒有出現問題,但是在編譯php7.1的版本的make && make install這一步出現錯誤,提示:
virtual memory exhausted: Cannot allocate memory Makefile:197: recipe for target 'libmagic/apprentice.lo' failed
下面是make編譯後的詳細資訊:
/www/server/php/71/src/ext/fileinfo# make && make install /bin/sh /www/server/php/71/src/ext/fileinfo/libtool --mode=compile cc -I/www/server/php/71/src/ext/fileinfo/libmagic -I. -I/www/server/php/71/src/ext/fileinfo -DPHP_ATOM_INC -I/www/server/php/71/src/ext/fileinfo/include -I/www/server/php/71/src/ext/fileinfo/main -I/www/server/php/71/src/ext/fileinfo -I/www/server/php/71/include/php -I/www/server/php/71/include/php/main -I/www/server/php/71/include/php/TSRM -I/www/server/php/71/include/php/Zend -I/www/server/php/71/include/php/ext -I/www/server/php/71/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /www/server/php/71/src/ext/fileinfo/libmagic/apprentice.c -o libmagic/apprentice.lo cc -I/www/server/php/71/src/ext/fileinfo/libmagic -I. -I/www/server/php/71/src/ext/fileinfo -DPHP_ATOM_INC -I/www/server/php/71/src/ext/fileinfo/include -I/www/server/php/71/src/ext/fileinfo/main -I/www/server/php/71/src/ext/fileinfo -I/www/server/php/71/include/php -I/www/server/php/71/include/php/main -I/www/server/php/71/include/php/TSRM -I/www/server/php/71/include/php/Zend -I/www/server/php/71/include/php/ext -I/www/server/php/71/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /www/server/php/71/src/ext/fileinfo/libmagic/apprentice.c -fPIC -DPIC -o libmagic/.libs/apprentice.o virtual memory exhausted: Cannot allocate memory Makefile:197: recipe for target 'libmagic/apprentice.lo' failed make: *** [libmagic/apprentice.lo] Error 1
原因
這個意思是記憶體不足,無法完成編譯。
本次使用的是AWS t2.micro配置的EC2實例,實際上其記憶體有1GB,但是還是出現本次的錯誤,應該是同時運行的其他程式導致記憶體不足。
解決辦法
既然這樣,物理記憶體不足我們沒辦法,但是可以通過自行增加虛擬記憶體的方法來解決。
通過free -m來查看下記憶體使用狀況
# free -m total used free shared buff/cache available Mem: 990 466 447 3 76 401 Swap: 0 0 0
創建一個目錄/opt/images/
你可以自己定路徑
# mkdir /opt/images/ # rm -rf /opt/images/swap
創建一個2GB大小的文件
# 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, 2.0 GiB) copied, 30.3635 s, 69.1 MB/s
把創建的文件變成SWAP分區
# mkswap /opt/images/swap Setting up swapspace version 1, size = 2 GiB (2097147904 bytes) no label, UUID=dd2fa2db-f8bd-41db-9e1a-5d9257924c6f
- 啟用這個SWAP文件
# swapon /opt/images/swap swapon: /opt/images/swap: insecure permissions 0644, 0600 suggested.
看看SWAP是否生效
# free -m total used free shared buff/cache available Mem: 990 467 64 3 458 356 Swap: 1999 0 1999
可以看到的確有2GB的SWAP記憶體
然後回到原來的作業
使用cd -
回到原來的/www/server/php/71/src/ext/fileinfo
目錄
繼續編譯fileinfo
# make && make install
執行成功
See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- Build complete. Don't forget to run 'make test'. Installing shared extensions: /www/server/php/71/lib/php/extensions/no-debug-non-zts-20160303/
完成後關閉SWAP
# swapoff swap # rm -f /opt/images/swap
以後再出現記憶體不足可以通過增加SWAP虛擬記憶體來解決~
參考資料
https://www.cnblogs.com/chenpingzhao/p/4820814.html