编译时: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文件也可以不删除,留着以后使用,关键是你的虚拟机硬盘够用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值