Common configuration parameters related to memory

Several very common memory configuration parameters:
  -Xms
  Heap initial value of the JVM startup application, the operating system default 64th of physical memory but less than 1G. When the default heap memory spare is greater than 70%, JVM reduces the size of the heap to the specified size -Xms, by -XX: MaxHeapFreeRation = specify this column ratio. Server-side JVM is best to -Xms and -Xmx to the same value, to avoid after each JVM garbage collection to complete re-allocate memory, it can reduce the number of garbage collection ; development of test machine JVM can leave the default value. (Example: -Xms4g)
  -Xmx
  Heap JVM can apply the maximum value, the default value but less than 1/4 of the physical memory 1G, when the default heap memory spare is less than 40%, JVM Heap -Xmx increases to the specified size, by -XX: MinHeapFreeRation = this ratio is specified column. The optimum value should be set depending on the size of physical memory in a computer and other may be memory overhead. (Example: -Xmx4g)
See example, be a comparison test , and a different set -Xmx -Xms and the same value, compare with the code, see the changes in the recovery times:
Jvm give such a configuration: -Xms5M -Xmx20M -XX: + PrintGCDetails -XX: + UseSerialGC
public  class JvmTest {
     public  static  void main (String [] args) {
         byte [] = BS new new  byte [1024 * 1024 *. 1 ]; 
        System.out.println ( "1M allocated memory space" ); 
        jvmInfo (); 
        byte [] = BS1 new new  byte [1024 * 1024 *. 4 ]; 
        System.out.println ( "4M allocated memory space" ); 
        jvmInfo (); 
    } 
    public  static String toM ( Long byteNum) {
         a float NUM = byteNum / (1024 * 1024 ); 
        DecimalFormat df= New new DecimalFormat ( "0.00" ); 
        String STR = df.format (NUM);
         return STR; 
    } 
    public  static  void jvmInfo () {
         // Get the maximum memory configuration 
        Long maxMemory = Runtime.getRuntime () maxMemory ().; 
        System.out.println ( "maxMemory:" + + maxMemory "byte," + toM (maxMemory) + "M" ); 

        // Get the size of the free memory 
        Long freeMemory = . Runtime.getRuntime () freeMemory (); 
        the System .out.println ( "freeMemory:" + + maxMemory "byte," + toM (freeMemory) + "M" );

        // Get the current use of swap memory size
        long totalMemory = Runtime.getRuntime().totalMemory();
        System.out.println("totalMemory:" + maxMemory + "字节," + toM(totalMemory) + "M");
    }
}
  -Xmn ( after jdk1.4 the -XX: NewSize and -XX: MaxNewSize unity , because we set them, they also recommended to set the same size)
  Heap the Java Young area size . The young generation throughout the heap size = size + size + old generation of permanent generation size (relative to the type of HotSpot virtual machine is). Permanent generation is generally a fixed size 64m, so the increase in the young generation, will reduce the size of the old generation. This value greater impact on system performance, Sun official recommended configuration for the entire heap of 3/8. (Example: -Xmn2g)
 
Old used to store objects after several years of the new generation GC still alive, such as cache object, the new object is also possible to go directly to the old era, there are two situations:
1, large objects can be started by setting parameters -XX: PretenureSizeThreshold = 1024 (bytes, 0 is the default) to represent no more than what was partitioned new generation, but directly partitioned years old.
2, a large array of objects, and an array of non-referenced external objects. Old's share of the memory size minus -Xmn corresponding values ​​for the corresponding -Xmx value. If there is no complete examples in the heap memory allocation, and the stack can no longer expand, it will throw an OutOfMemoryError.
 
  -Xss
  Java每个线程的Stack大小。JDK5.0以后每个线程堆栈大小为1M,以前每个线程堆栈大小为256K。根据应用的线程所需内存大小进行调整。 在相同物理内存下,减小这个值能生成更多的线程。 但是操作系统对一个进程内的线程数还是有限制的,不能无限生成, 经验值在3000~5000左右。(推荐:小系统:-Xss128K,大系统:-Xss256K)
 
 -XX:PermSize
  持久代(方法区)的初始内存大小。(例如:-XX:PermSize=64m)
 -XX:MaxPermSize
  持久代(方法区)的最大内存大小。(例如:-XX:MaxPermSize=512m)
 -XX:+MaxTenuringThreshold=10
  垃圾的最大年龄,代表对象在Survivor区经过10次复制以后才进入老年代。如果设置为0,则年轻代对象不经过Survivor区,直接进入老年代。
 
注意:tomcat中的配置内存参数可以放在 catalina.sh或者catalina.bat的第二行:
set JAVA_OPTS=%JAVA_OPTS% -server -Xms1800m -Xmx1800m -Xmn600m -XX:PermSize=512M -XX:MaxPermSize=512m -Xss128K -XX:+PrintGCDetails

Guess you like

Origin www.cnblogs.com/chuanqi1995/p/11646832.html