前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >编译安装PHP组件出现错误提示内存不足:virtual memory exhausted Cannot allocate memory

编译安装PHP组件出现错误提示内存不足:virtual memory exhausted Cannot allocate memory

作者头像
用户1065635
发布2019-12-30 18:24:55
5.4K1
发布2019-12-30 18:24:55
举报
文章被收录于专栏:前端社区前端社区

背景

前面一篇我们介绍了宝塔面板的PHP默认不安装fileinfo组件,需要手动编译安装。在php5.6上是没有出现问题,但是在编译php7.1的版本的make && make install这一步出现错误,提示:

代码语言:javascript
复制
virtual memory exhausted: Cannot allocate memory
Makefile:197: recipe for target 'libmagic/apprentice.lo' failed

下面是make编译后的详细信息:

代码语言:javascript
复制
/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来查看下内存使用状况
代码语言:javascript
复制
# free -m
               total        used        free      shared  buff/cache   available
Mem:            990         466         447           3          76         401
Swap:             0           0           0
创建一个目录/opt/images/

你可以自己定路径

代码语言:javascript
复制
# mkdir /opt/images/
# rm -rf /opt/images/swap
创建一个2GB大小的文件
代码语言:javascript
复制
# 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分区
代码语言:javascript
复制
# mkswap /opt/images/swap
Setting up swapspace version 1, size = 2 GiB (2097147904 bytes)
no label, UUID=dd2fa2db-f8bd-41db-9e1a-5d9257924c6f
  • 启用这个SWAP文件
代码语言:javascript
复制
# swapon /opt/images/swap
swapon: /opt/images/swap: insecure permissions 0644, 0600 suggested.
看看SWAP是否生效
代码语言:javascript
复制
# 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
代码语言:javascript
复制
# make && make install

执行成功

代码语言:javascript
复制
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
代码语言:javascript
复制
# swapoff swap
# rm -f /opt/images/swap

以后再出现内存不足可以通过增加SWAP虚拟内存来解决~

参考资料

https://www.cnblogs.com/chenpingzhao/p/4820814.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 原因
  • 解决办法
    • 通过free -m来查看下内存使用状况
      • 创建一个目录/opt/images/
        • 创建一个2GB大小的文件
          • 把创建的文件变成SWAP分区
            • 看看SWAP是否生效
              • 然后回到原来的作业
                • 继续编译fileinfo
                  • 完成后关闭SWAP
                  • 参考资料
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档