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....

2020 年的配置目录更新

在 2020.1 版本以后,配置目录发生了变更。

~/Library/Preferences/IntelliJIdea<VERSION>/ 这个目录只坚持到 2019.3,之后的配置你可能要从 ~/Library/ApplicationSupport/JetBrains/IntelliJIdea<VERSION>/ 这个目录下去找。选项和文件本身没有什么变化,之前如果有过配置,可以观察到在迁移的时候也继承了。

关于参数,具体说明可以参考此处官方文档。

此外,IDE 的界面中 [Help] 栏下拉,选择 [Edit Custom VM Options...] 也可以在 IDE 中直接打开并且编辑这个文件。

vmoptions

这样可能更加方便一点。

References:


Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

3 Comments

Mr.Li · September 26, 2017 at 22:58

Google Chrome 61.0.3163.100 Google Chrome 61.0.3163.100 Mac OS X  10.12.6 Mac OS X 10.12.6

居然给的这么高。哈哈

罗耕 · September 18, 2017 at 02:42

Google Chrome 60.0.3112.113 Google Chrome 60.0.3112.113 Mac OS X  10.12.6 Mac OS X 10.12.6

你这个写的不错,帮到我了,楼主交个朋友吧

redfox241 · December 6, 2016 at 15:20

Google Chrome 54.0.2840.98 Google Chrome 54.0.2840.98 Mac OS X  10.11.6 Mac OS X 10.11.6

?,我的问题解决了。

Leave a Reply to Mr.Li Cancel reply

Your email address will not be published. Required fields are marked *