eclipse modifies jvm parameter tuning method of 2 kinds

  • 2021-01-25 07:30:23
  • OfStack

This paper introduces eclipse to modify jvm parameter tuning methods (2 kinds), and shares them with you as follows:

1. When eclipse is not set, eclipse will always feel slow to start and good to use. In fact, as long as some configuration of eclipse related parameters, there will be a great improvement.

There are two ways:
1, Open eclipse configuration file eclipse.ini and change -Xmx (this value represents the maximum amount of memory jvm can use)
2. When running java program, select run- > run configuration- > arguments, enter -Xms100M -Xmx800M (-Xms represents the amount of memory allocated when jvm is started, -Xmx represents the maximum amount of memory that can be allocated).

The following error was reported today while testing the conversion of documents to images in eclipse:

[

java.lang.OutOfMemoryError: Java heap space

]

As you can see from the above exception message, the memory required by ES43en has exceeded the maximum memory that we have allocated to the virtual machine. So the question becomes how to set the maximum memory for jvm in eclipse.

1. Try to modify Eclipse.ini file (this method does not work)

Find the file eclipse.ini in the eclipse directory and modify the following:


-Xms40m
-Xmx512m

Restart eclipse after modification and find that it does not work at all. After looking up some information later, it turns out that the minimum memory and maximum memory set here are not for JVM, this memory value contains the memory used by eclipse itself.

2. Modify jdk to use memory (this method works)

window- window- eclispe > preferences- > Java- > Installed JRE, click the Edit button on the right, and enter the following values in the "Default VM Arguments" option in the editing interface.


-Xms64m -Xmx128m

3. Modify Run Configurations (this method is feasible)

Right-click on the code and click "Run As" - in turn > "Run Configurations", just fill in the following value in "VM arguments:" in the Arguments parameter.


-Xms64m -Xmx128m

The heap memory is set by the following jvm parameters:

-Xmx512m 最大总堆内存,1般设置为物理内存的1/4
-Xms512m 初始总堆内存,1般将它设置的和最大堆内存1样大,这样就不需要根据当前堆使用情况而调整堆的大小了
-Xmn192m 年轻带堆内存,sun官方推荐为整个堆的3/8
堆内存的组成 总堆内存 = 年轻带堆内存 + 年老带堆内存 + 持久带堆内存
年轻带堆内存 对象刚创建出来时放在这里
年老带堆内存 对象在被真正会回收之前会先放在这里
持久带堆内存 class文件,元数据等放在这里
-XX:PermSize=128m 持久带堆的初始大小
-XX:MaxPermSize=128m 持久带堆的最大大小,eclipse默认为256m。如果要编译jdk这种,1定要把这个设的很大,因为它的类太多了。

4. Query current JVM memory code

Below is the code that queries the current JVM memory size to test whether the memory of JVM will change after the above setting. There is no need to restart eclipse after adding configuration entries for JVM memory. The specific code is as follows:


public class TestMemory {
 
  /**
   * @param args
   */
  public static void main(String[] args) {
    System. out .println( "  Memory information  :" + toMemoryInfo());
  }
 
  /**
   *  Gets the current  jvm  Memory information of 
   *
   * @return
   */
  public static String toMemoryInfo() {
 
    Runtime currRuntime = Runtime.getRuntime ();
    int nFreeMemory = ( int ) (currRuntime.freeMemory() / 1024 / 1024);
    int nTotalMemory = ( int ) (currRuntime.totalMemory() / 1024 / 1024);
    return nFreeMemory + "M/" + nTotalMemory +"M(free/total)" ;
  }
}

Related articles: