JVM parameter configuration

Keywords: Java jvm

Virtual Machine Parameters

In the process of running a virtual machine, if it can track the running state of the system, it will be helpful to troubleshoot the problem. For this reason, the virtual machine provides some parameters to track the state of the system. If the given parameters are used to execute a java virtual machine, the relevant logs can be printed in the system for analysis. Practical problems. In fact, we configure the parameters of the virtual machine around the stack method area.

Heap allocation parameters

- XX:+PrintGC: With this parameter, the log will be printed whenever the virtual machine encounters GC after booting.
- XX:+UseSerial GC: Configuration of Serial Recycler.
- XX:+PrintGCDetails: You can view the details, including the situation of each district.
- Xms: Sets the initial heap size for java program startup.
- Xmx: Setting up a java program to get the maximum heap size
- Xmx20m-Xms5m-XX:+PrintCommandLineFlags: Parameter output that can be passed implicitly or displayed to the virtual machine.
Summary: In practical work, we can directly set the size of the initial heap and the maximum heap to be equal, which can reduce the number of runtime garbage collection, thereby improving performance.

public class Test01 {

    public static void main(String[] args) {

        //-XX:+PrintGC -Xms5m -Xmx20m -XX:+UseSerialGC -XX:+PrintGCDetails -XX:+PrintCommandLineFlags

        //1:
        //-XX:+PrintGC -Xms5m -Xmx20m -XX:+UseSerialGC -XX:+PrintGCDetails

        //View GC information
        System.out.println("max memory:"+Runtime.getRuntime().maxMemory());
        System.out.println("free memory:"+Runtime.getRuntime().freeMemory());
        System.out.println("total memory:"+Runtime.getRuntime().totalMemory());

        byte[] b1 = new byte[1*1024*1024];
        System.out.println("Distribution 1 M");
        System.out.println("max memory:"+Runtime.getRuntime().maxMemory());
        System.out.println("free memory:"+Runtime.getRuntime().freeMemory());
        System.out.println("total memory:"+Runtime.getRuntime().totalMemory());

        byte[] b2 = new byte[4*1024*1024];
        System.out.println("Distribution 4 M");
        System.out.println("max memory:"+Runtime.getRuntime().maxMemory());
        System.out.println("free memory:"+Runtime.getRuntime().freeMemory());
        System.out.println("total memory:"+Runtime.getRuntime().totalMemory());

        int a = 0x00000000fee10000;
        int b = 0x00000000fec00000;
        System.out.println("The result was 1920. k,But the actual results are as follows:"+(a-b)/1024);

    }

}

Heap allocation parameters (2)

- Xmn: You can set the size of the Cenozoic generation. Setting a larger Cenozoic generation will reduce the size of the older generation. This parameter has a great impact on system performance and GC behavior. The size of the Cenozoic generation will generally set about one third to one fourth of the total heap space.
- XX:SurvivorRatio: Used to set the ratio of eden space to from/to space in the Cenozoic. Meaning: - XX:SurvivorRatio=eden/from=eden/to
Summary: Different heap distributions will have a certain impact on the implementation of the system. In practical work, reasonable configuration should be made according to the characteristics of the system. The basic strategy is to reserve the objects in the new generation as far as possible and reduce the number of GC in the old generation.
In addition to setting the absolute size of the Cenozoic (-Xmn), you can also use (-XX: New Ration) to set the ratio of the Cenozoic to the Cenozoic: -XX: New Ratio = Old/Cenozoic

public class Test02 {

    public static void main(String[] args) {
        //First configuration eden 2 =from 1+ to 1
        //-Xms20m -Xmx20m -Xmn1m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC

        //Second configuration
        //-Xms20m -Xmx20m -Xmn7m -XX:SurvivorRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC

        //Third Configuration
        //- XX: New Ratio = Old/Cenozoic
        //-Xms20m -Xmx20m -XX:NewRatio=2 -XX:+PrintGCDetails -XX:+UseSerialGC

        byte[] b=null;
        //Continuous application for 10m space to the system
        for(int i=0;i<10;i++){
            b=new byte[1*1024*1024];
        }
    }

}

Heap overflow handling

In the process of running a java program, if the heap space is insufficient, an Out of Memory error will be thrown. Once this kind of problem occurs in the production environment, it may cause serious business interruption. The java virtual machine provides - XX:+HeapDumpOnOutOfMemory Error, which can be used to export the whole heap information when the memory overflows. In conjunction with the parameter, - XX:HeapDumpPath, you can set the path of the export heap.
Memory Analysis Tool: Memory Analyzer 1.5.0

public class Test3 {

    public static void main(String[] args) {
        //-Xms2m -Xmx2m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=e:/Test03.dump
        //Heap memory overflow
        Vector vector = new Vector();
        for(int i=0;i<5;i++){
            vector.add(new Byte[1*1024*1024]);
        }
    }

}

Stack Profile

Java Virtual Machine provides parameter - Xss to specify the maximum stack space of threads, and the whole parameter directly determines the maximum depth of function callability.

public class Test4 {

    //-Xss1m
    //-Xss5m

    //Stack call depth
    private static int count;

    public static void recursion(){
        count++;
        recursion();
    }

    public static void main(String[] args) {
        try {
            recursion();
        } catch (Throwable e) {
            System.out.println("Call the deepest depth:"+count);
            e.printStackTrace();
        }
    }
}

Method area

Like the java heap, the method area is a memory area shared by all threads. It is used to store the class information of the system. How much information can the method area (permanent area) save to configure it. By default, -XX:MaxPermSize is 64MB. If the system generates a large number of classes at run time, a relative match needs to be set up. Appropriate method area to avoid the problem of memory overflow in permanent area.
-XX:PermSize=64M -XX:MaxPermSize=64M

Direct memory configuration

Direct memory is also a very important part of java programs, especially widely used in NIO. Direct memory skips the java heap so that java programs can directly access the native heap space, thus speeding up the access speed of memory space to a certain extent. But it is not obvious that direct memory can improve the speed of memory access. Specific problems are analyzed in detail.
Relevant configuration parameters: - XX:MaxDirectMemorySize, if the default value is not set to the maximum space, that is - Xmx. When the direct memory usage reaches the upper limit, garbage collection will be triggered, and if the space is not effectively released, OOM of the system will also be caused.

Working mode of Client and Server Virtual Machine

The java virtual machine supports Client and Server modes, using - client can specify the use of Client mode, using - server can use Server mode. (jdk1.7 and beyond)
Difference: Client mode starts faster than server mode, but the performance of long-term running server mode is much better than Client mode.

JVM Blog: http://www.cnblogs.com/redcreen/tag/jvm/

Posted by tonga on Wed, 10 Jul 2019 14:17:49 -0700