大家都知道eclipse里面有个initialize文件,这个文件可以定义一些eclipse启动和运行的环境。

新下载的eclipse,里面的ini文件如下:

 
  
  1. -startup 
  2. plugins/org.eclipse.equinox.launcher_1.1.1.R36x_v20101122_1400.jar 
  3. --launcher.library 
  4. plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.2.R36x_v20101222 
  5. -product 
  6. org.eclipse.epp.package.jee.product 
  7. --launcher.defaultAction 
  8. openFile 
  9. --launcher.XXMaxPermSize 
  10. 256M 
  11. -showsplash 
  12. org.eclipse.platform 
  13. --launcher.XXMaxPermSize 
  14. 256m 
  15. --launcher.defaultAction 
  16. openFile 
  17. -vmargs 
  18. -Dosgi.requiredJavaVersion=1.5 
  19. -Xms40m 
  20. -Xmx512m 

okay,在深入讲解之前,我们先解释一下此文件的语义:

 
  
  1. -startup 
  2. plugins/org.eclipse.equinox.launcher_1.1.1.R36x_v20101122_1400.jar 
  3. --launcher.library 
  4. plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.2.R36x_v20101222 
  5. -product 
  6. org.eclipse.epp.package.jee.product 
  7. --launcher.defaultAction 
  8. openFile 
  9. --launcher.XXMaxPermSize  //为Eclipse对非堆区的内存分配default的设置
  10. 256M  //为Eclipse对非堆区的内存分配default的设置
  11. -showsplash 
  12. org.eclipse.platform 
  13. --launcher.XXMaxPermSize  //为Eclipse对非堆区的内存分配default的设置
  14. 256m //为Eclipse对非堆区的内存分配default的设置
  15. --launcher.defaultAction 
  16. openFile 
  17. -vmargs 
  18. -Dosgi.requiredJavaVersion=1.5 
  19. -Xms40m  //自定义堆区的内存分配,最小为40m
  20. -Xmx512m //自定义堆区的内存分配,最大为521m

好,看完//,你应该知道我们这个ini文件,主要是对JVM的堆区和非堆区内存分配的设定。

那么什么是堆区和非堆区呢?在讲解原理之前先,做一个简单的实际优化工作。

无数的博文,告诉我们【 注意-vm得在-vmargs之前,才能生效,不然会误认为它也只是一个JVM参数】

1. 确切地指明JDK的路径,有助于你知道你的eclipse是运行在什么环境的。

2. 增加堆区和非堆区的内存分配(结合实际的硬件情况)

3. 增加对多核CPU的利用

可以参考官网:http://wiki.eclipse.org/Eclipse.ini

修改后的文件如下:

 
  
  1. -startup 
  2. plugins/org.eclipse.equinox.launcher_1.1.1.R36x_v20101122_1400.jar 
  3. --launcher.library 
  4. plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.2.R36x_v20101222 
  5. -product 
  6. org.eclipse.epp.package.jee.product 
  7. --launcher.defaultAction 
  8. openFile 
  9. --launcher.XXMaxPermSize 
  10. 256M 
  11. -showsplash 
  12. org.eclipse.platform 
  13. --launcher.XXMaxPermSize 
  14. 256m 
  15. --launcher.defaultAction 
  16. openFile 
  17. -vm 
  18. C:\Java\jdk1.6.0_24\bin\javaw.exe 
  19. -vmargs 
  20. -Dosgi.requiredJavaVersion=1.5 
  21. -Xms512m 
  22. -Xmx1024m 
  23. -XX:+UseParallelGC 
  24. -XX:PermSize=256M 
  25. -XX:MaxPermSize=512M 

修改完以后,你可以启动eclipse,看是否成功,而且可以对比启动速度。

好设置完以后,稍稍讲一下什么是堆区(heap size)和非堆区(permanent size)。

英文已经很好地解释了两者的区别。

PermGen is the permanent generation of objects in the VM (Class names, internalized strings, objects that will never get garbage-collected).

JVM主要管理两种类型的内存:堆和非堆。简单来说堆就是Java代码可及的内存,是留给开发人员使用的;非堆就是JVM留给自己用的,所以方法区、 JVM内部处理或优化所需的内存(如JIT编译后的代码缓存)、每个类结构(如运行时常数池、字段和方法数据)以及方法和构造方法的代码都在非堆内存中。

按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配。堆是在 Java 虚拟机启动时创建的。”“在JVM中堆之外的内存称为非堆内存(Non-heap memory)”。