IntelliJ IDEA 是一个基于 Java 的集成开发环境(Integrated Development Environment, aka IDE), 这意味着很容易配置虚拟机的系统占用。Jetbrains 家默认给了一个很差的配置,这让我们可以在很差的机器上也能很容易跑起来,但同时意味着在好一点的机器上,也是即便系统有很多资源依然慢得要死。
不过如前文所说,这一切都可以靠配置很容易解决。 以 OS X举例,假如 IntelliJ IDEA 这个 App 在 ~/Applications/ 目录下(当然,个人洁癖,你的也可能在 /Applications/) 下,请自行替换。
我可以在 ~/Applications/IntelliJ\ IDEA.app/Contents/bin/ 目录下找到一个文件名叫 idea.vmoptions
$ ls -al ~/Applications/IntelliJ\ IDEA.app/Contents/bin/ total 9248 drwxr-xr-x 17 yu staff 578 May 14 21:11 . drwxr-xr-x 13 yu staff 442 May 14 21:09 .. -rw-r--r-- 1 yu staff 136 May 14 20:11 appletviewer.policy -rw-r--r-- 1 yu staff 597 May 14 21:01 idea.properties -rw-r--r-- 1 yu staff 92 May 14 21:01 idea.vmoptions ....
copy 这个文件, paste 到 ~/Library/Preferences/IntelliJIdea2016.1 下,并进行编辑即可。 其中 IntelliJIdea2016.1 这个是根据版本等的不同而各有不同。
默认文件大致是这样的:
-Xms128m -Xmx750m -XX:MaxPermSize=350m -XX:ReservedCodeCacheSize=240m -XX:+UseCompressedOops
我修改后如下:
-Xms2g -Xmx6g -XX:MaxPermSize=4g -XX:ReservedCodeCacheSize=2g -XX:+UseCompressedOops
m,g作为后缀,和普通的 jvm 配置参数一样。内容尽可以随便写,不过有个特别的,高亮的 “-XX:ReservedCodeCacheSize” 结果不能超过 2 g,不然错误如下:
yu$ /Users/yu/Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea 2016-06-02 16:22:25.320 idea[28721:1467259] Value of IDEA_JDK: (null) 2016-06-02 16:22:25.327 idea[28721:1467272] fullFileName is: /Users/yu/Applications/IntelliJ IDEA.app/Contents/bin/idea.vmoptions 2016-06-02 16:22:25.327 idea[28721:1467272] fullFileName exists: /Users/yu/Applications/IntelliJ IDEA.app/Contents/bin/idea.vmoptions 2016-06-02 16:22:25.327 idea[28721:1467272] Value of IDEA_VM_OPTIONS is (null) 2016-06-02 16:22:25.327 idea[28721:1467272] Processing VMOptions file at /Users/yu/Library/Preferences/IntelliJIdea2016.1/idea.vmoptions 2016-06-02 16:22:25.327 idea[28721:1467272] Done Invalid ReservedCodeCacheSize=4096M. Must be at most 2048M. 2016-06-02 16:22:25.336 idea[28721:1467272] JNI_CreateJavaVM (/Users/yu/Applications/IntelliJ IDEA.app/Contents/jre/jdk) failed: 4294967295
如果懒得 copy&paste,直接编辑 Application 下的内容,也可以达到同样的目的。
但是不推荐这么做,因为随着软件更新,它可能会被直接覆盖。反之,在我们升级后,配置目录中的内容通常会被 Idea 自动迁移。
此外,对此官方警告道:
Do not change JVM options in the default file, because it is replaced when IntelliJ IDEA is updated. Moreover, in case of macOS, editing this file violates the application signature.
editing this file violates the application signature. hmmm….
Update:
如果需要,现在可以考虑菜单栏选择 Help -> Edit Custom VM Options... 即可打开这个文件。
在 2025 年的今天,我们可以用 Kimi 来获取一些更加合理的推荐:
方案 A:大型项目(推荐)
-Xms4g -Xmx8g -XX:MaxMetaspaceSize=1g -XX:ReservedCodeCacheSize=2g -XX:+UseG1GC -XX:SoftRefLRUPolicyMSPerMB=50 -XX:+UnlockExperimentalVMOptions -XX:+UseContainerSupport -Didea.max.intellisense.filesize=10000 -Didea.dynamic.classpath=false
方案 B:超大型项目/微服务架构
-Xms8g -Xmx16g -XX:MaxMetaspaceSize=2g -XX:ReservedCodeCacheSize=2g -XX:+UseG1GC -XX:SoftRefLRUPolicyMSPerMB=50 -XX:+UnlockExperimentalVMOptions -XX:+UseContainerSupport -Didea.max.intellisense.filesize=25000 -Didea.dynamic.classpath=false
🔧 关键参数说明
| 参数 | 值 | 作用 |
|---|---|---|
-Xmx |
8-16g | 主堆内存,不推荐超过 16GB |
-Xms |
等于或小于 Xmx 的 50% | 初始堆,避免运行时扩容 |
-XX:MaxMetaspaceSize |
1-2g | 替代过时的 PermSize |
-XX:+UseG1GC |
– | 低延迟垃圾回收器 |
SoftRefLRUPolicyMSPerMB |
50 | 减少软引用内存占用 |
UseContainerSupport |
– | 更好支持现代云平台 |
idea.max.intellisense.filesize |
10000 | 提升大文件编辑性能 |
💡 其他优化建议
- 内存指示器:启用
View → Appearance → Status Bar Widgets → Memory Indicator实时监控 - 禁用不必要的插件:减少内存占用
- 调整索引线程数:在
idea.properties添加idea.indexing.thread.count=8 - 使用新版 IDEA:2023.2+ 对内存管理有重大改进
总结:对于 64GB 内存,给 IDEA 分配 8-16GB 已非常充足,重点在于现代 GC 调优和避免过时参数,而非单纯堆砌内存。
3 Comments
Mr.Li · September 26, 2017 at 22:58
居然给的这么高。哈哈
罗耕 · September 18, 2017 at 02:42
你这个写的不错,帮到我了,楼主交个朋友吧
redfox241 · December 6, 2016 at 15:20
?,我的问题解决了。