起因
win8.1、双核CPU、16G内存,装eclipse以后报错非常诡异,有时候开俩仨eclipse妥妥的没事,有时开一个就卡的不可描述,假死
解决方案
step1、修改eclipse.ini参数,扩大JVM的内存(能解决一部分人的问题,然而对我并没有什么卵用)
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-<span class="ruby" style="box-sizing: border-box;"><span class="hljs-constant" style="box-sizing: border-box;">XX</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:MaxPermSize=</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span>M </span>-<span class="ruby" style="box-sizing: border-box;"><span class="hljs-constant" style="box-sizing: border-box;">Xms256m</span> </span>-<span class="ruby" style="box-sizing: border-box;">Xmx4096m</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
step2、设置并行GC(最低是双核cpu,好用了一点)
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-<span class="ruby" style="box-sizing: border-box;"><span class="hljs-constant" style="box-sizing: border-box;">XX</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:+UseParallelGC</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
step3、设置默认虚拟机(重中之重)
<code class="hljs tex has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-vm D:<span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\jdk</span>1.8.0_25<span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\bin</span><span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\javaw</span>.exe(jre下javaw.exe的绝对地址,依据jdk安装位置变动)</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
eclipse.ini环境示例
<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-<span class="ruby" style="box-sizing: border-box;">startup </span>plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar -<span class="ruby" style="box-sizing: border-box;">-launcher.library </span>plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20140603-1326 -<span class="ruby" style="box-sizing: border-box;">product </span>org.eclipse.epp.package.jee.product -<span class="ruby" style="box-sizing: border-box;">-launcher.defaultAction </span>openFile -<span class="ruby" style="box-sizing: border-box;">showsplash </span>org.eclipse.platform -<span class="ruby" style="box-sizing: border-box;">-launcher.<span class="hljs-constant" style="box-sizing: border-box;">XXMaxPermSize</span> </span>768m -<span class="ruby" style="box-sizing: border-box;">-launcher.defaultAction </span>openFile -<span class="ruby" style="box-sizing: border-box;">vm </span>D:\jdk1.8.0_25\bin\javaw.exe -<span class="ruby" style="box-sizing: border-box;">-launcher.appendVmargs </span>-<span class="ruby" style="box-sizing: border-box;">vmargs </span>-<span class="ruby" style="box-sizing: border-box;"><span class="hljs-constant" style="box-sizing: border-box;">Dosgi</span>.requiredJavaVersion=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.5</span> </span>-<span class="ruby" style="box-sizing: border-box;"><span class="hljs-constant" style="box-sizing: border-box;">XX</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:MaxPermSize=</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span>M </span>-<span class="ruby" style="box-sizing: border-box;"><span class="hljs-constant" style="box-sizing: border-box;">Xms256m</span> </span>-<span class="ruby" style="box-sizing: border-box;"><span class="hljs-constant" style="box-sizing: border-box;">Xmx4096m</span> </span>-<span class="ruby" style="box-sizing: border-box;"><span class="hljs-constant" style="box-sizing: border-box;">XX</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:+UseParallelGC</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li></ul>
结果
问题解决、使用流畅、一本满足
原因分析
从官方文档等是可以找到原因的,eclipse.ini官方说明、eclipse.ini官方参数说明、JVM参数优化。
eclipse.ini放置在eclipse安装文件夹的根目录下,是控制eclipse启动的text文件。在里面默认设置了eclipse的几项启动参数,可直接增删改查文件设置eclipse的启动参数,缺省参数eclipse会设置默认值,如没有设置JVM位置则eclipse会使用系统默认的VM。
重要参数如下:
参数 | 说明 | 默认值 |
---|---|---|
-vmargs | 没有默认值设置,仅表示后面都是JVM参数,前面的是eclipse参数 | |
-XX:MaxPermSize=128m | JVM初始分配的最大非堆内存,这里官方标示不能在所有平台上检测到JVM | 默认为128M或缺省(缺省值和-server -client选项相关。-server选项下默认MaxPermSize为64m;-client选项下默认MaxPermSize为32m),按需分配。一般设置为最大堆内存的1/4,但是过小会导致内存溢出 |
-Xms40m | 设置JVM Heap(堆内存)最小值 | 默认为40M,初始分配 |
-Xmx512m | 设置JVM Heap(堆内存)最大值 | 默认为512M,按需分配,过小会导致内存溢出,JVM报错,无法用try catch捕捉 |
这里有一句话非常重要,就是并不能在所有平台上都能检测到JVM虚拟机,需要我们告诉eclipse本地虚拟机的位置。
在官方资料上也特别说明了这一点:
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">One <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> most recommended options <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> use <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> specify a specific JVM <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> Eclipse <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-command" style="box-sizing: border-box;">run</span> <span class="hljs-function_start" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">on</span>.</span> Doing this ensures <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">that</span> you are absolutely certain which JVM Eclipse will <span class="hljs-command" style="box-sizing: border-box;">run</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> insulates you <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">from</span> system changes <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">that</span> can alter <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"default"</span> JVM <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> your system. Many a user has been tripped up because they thought they knew what JVM would be used <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">by</span> default, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">but</span> they thought wrong. eclipse.ini lets you be CERTAIN.</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
翻译:官方推荐在eclipse运行时设置一个特定的JVM,这样可以确保JVM是eclipse的默认虚拟机。很多用户错误的认为JVM是默认虚拟机,然而事实并非如此。用户可通过eclipse.ini来修正虚拟机。
经查证,window下指定JVM需要设置-vm参数,例:
<code class="hljs tex has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-vm C:<span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\jdk</span>1.7.0_21<span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\bin</span><span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\javaw</span>.exe </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
<code class="hljs tex has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-vm C:<span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\jdk</span>1.7.0_21<span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\jre</span><span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\bin</span><span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\java</span>.exe</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
<code class="hljs tex has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-vm C:<span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\Development</span><span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\Java</span>\64bit<span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\jdk</span>1.7.0_09<span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\jre</span><span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\bin</span><span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\server</span><span class="hljs-command" style="box-sizing: border-box; color: rgb(0, 0, 136);">\jvm</span>.dll</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
指定的位置应该是本地java运行环境/bin目录/java程序或ava共享库(jvm.dll或libjvm.so)的绝对地址。eclipse和JVM的位数必须相同,即32位eclipse配置32位JVM,64位eclipse配置64位JVM,交叉不能运行。
重要的tips
tips1:
设置最大最小JVM内存也和系统的位数有关,32位系统下,一般限制在1.5G~2G;64位操作系统对内存无限制。
tips2:
初始分配的堆内存由-Xms指定,默认是物理内存的1/64;
最大分配的堆内存由-Xmx指定,默认是物理内存的1/4;
最大非堆内存的大小由–XX:MaxPermSize设置,默认是物理内存的1/4