今天碰到一個大坑,差點要了老命!
之前裝了ubuntu雙系統,后來崩潰,想在就想裝VMware虛擬機,再裝ubuntu,一切進展順利,直到在虛擬機的ubuntu中安裝IDEA時出現了問題。
安裝過程並沒有報錯,但是啟動后運行一個項目,項目還沒加載呢就直接退出,生成個錯誤日志,如下:

OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000f5a9b000, 66166784, 0) failed; error='無法分配內存' (errno=12)
日志中的部分內容是:
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 66166784 bytes for committing reserved memory.
# Possible reasons:
# The system is out of physical RAM or swap space
# In 32 bit mode, the process size limit was hit
# Possible solutions:
# Reduce memory load on the system
# Increase physical memory or swap space
# Check if swap backing store is full
# Use 64 bit Java on a 64 bit OS
# Decrease Java heap size (-Xmx/-Xms)
# Decrease number of Java threads
# Decrease Java thread stack sizes (-Xss)
# Set larger code cache with -XX:ReservedCodeCacheSize=
# This output file may be truncated or incomplete.
#
# Out of Memory Error (os_linux.cpp:2627), pid=3309, tid=0x00007f0174cfc700
#
# JRE version: OpenJDK Runtime Environment (8.0_112-b16) (build 1.8.0_112-release-736-b16)
# Java VM: OpenJDK 64-Bit Server VM (25.112-b16 mixed mode linux-amd64 compressed oops)
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
真是走了很多彎路,嘗試了無數次:重裝idea,idea配置文件配置jvm的大小,把虛擬機的內存再調大一倍等等等等
最后來根據 (errno=12)這個搜索到一篇文章,說道:
后來看了美軍一個文章(http://www.enchantedage.com/node/235),加一個配置即可:echo 1000000 > /proc/sys/vm/max_map_count
我看了自己虛擬機ubuntu里面,/proc/sys/vm/max_map_count的初始值很小,只有65530,果斷使用這個重設了一下。
啊,終於好了,太艱難了。
后來又請裝雙系統的同事看下他們系統里的這個值,也是65530,但是他們的卻不報錯,真是醉醉的。看來造成“無法分配內存”的原因,並不是這里,但是可以通過修改這個來解決。
猜測這個原因:1、雙系統和虛擬機不同 2、安裝jdk方式的不同(之前的我,和現在的同事們,都是先下載好jdk再安裝的;可是現在虛擬機我卻使用命令安裝,這樣不需要配置環境變量)
只是猜測,暫時就不去驗證它了,如果再有遇到的同學,解決不了的話,可以朝這個方向嘗試下。
后來在IDEA中操作,實在是各種卡、慢,第二天還是決定重裝jdk,不使用命令安裝,而是使用解壓縮包並且配置環境變量,並且將max_map_count的值改了回去
echo 65530> /proc/sys/vm/max_map_count
你猜怎么着,一點問題沒有。Maven倉庫下載jar包也很溜,果然還是jdk的問題啊。所以最終解決方案是:使用解壓縮包並配置環境變量的方式,重新安裝jdk!
標簽:虛擬機的ubuntu中安裝Idea出錯 無法分配內存 (errno=12)
原創文章,歡迎轉載,轉載請注明處!
附 美軍文章的內容如下:
Linux mmap() ENOMEM error causing Segmentation Fault
I have a system that creates files on disk, then uses mmap and madvise and mflush to asynchronously do I/O to the disk. This system may potentially create many, many files, each of which will have three mmap sections, that will be rotated through the file.
After trying to run this system for a while, I started getting segmentation violations that I couldn't quite understand. Initially, I thought it was a threading problem, because I'm using boost::asio and boost::thread quite heavily. I used strace() to figure out what the system was doing, and found that right before the crashes, one or more calls to mmap() would fail.
Long story short: There is a limit to the number of mmap() segments that can be active in a Linux process at any one time. This limit is configurable in /proc/sys/vm/max_map_count. I already knew there was a file descriptor limit, and I raised that pretty high, but apparently Linux doesn't think you'll be using lots of mmap() just because you're using lots of files. Adding the following to /etc/rc.local will fix the problem:
echo 1000000 > /proc/sys/vm/max_map_count
