Common JVM configuration parameters include: Trace Trace parameter, heap allocation parameter, stack allocation parameter.

The Trace parameter is used to Trace and monitor the JVM, and is used by developers for JVM tuning and troubleshooting.

Verbose: gc-xx :+PrintGC These two configuration parameters have the same effect. They both print brief information, such as executing code, when GC occurs

Public static void main(String[] args) {byte[] bytes = null; for (int i = 0; i < 100; i++) { bytes = new byte[1 * 1024 * 1024]; -xx :+PrintGC or -verbose:gc;}} This program creates 100 consecutive 1M array objects.

We can see that the program performed two minor GCS, both of which are new generation GCS.Copy the code

33038K Space occupied by objects before reclamation. 1760K indicates the space occupied by the reclaimed object. 125952K indicates how much space is available. 0.0044838 secs indicates the time taken for this garbage collection.

-xx :+PrintGCDetails -xx :+PrintGCDetails -xx :+PrintGCDetails -xx :+PrintGCDetails -xx :+PrintGCDetails View the console print result:

The following conclusions can be drawn from printing GC details:

(1) The Heap is divided into three main chunks: PSYoungGen New generation, ParOldGen, Metaspace Metaspace.Copy the code

Note that there is no permanent section here. The permanent section was removed in java8, and constants, string static variables that were in the permanent section were moved into the meta-space and used local memory.

(2) The Cenozoic era can be divided into two groups: Eden and From and to.Copy the code

Eden +from+to=total This is because the new generation of garbage collection algorithms use replication algorithms, which simply copy from and to back and forth (and recycle unreachable objects in the replication process).

Therefore, it is necessary to ensure that one area is empty so that there is reserved space to store the copied data. Therefore, the total size of the new generation is actually equal to Eden +from (or to) =33280K+5120K= 38400K.

Think: Where do different objects go?

(1) The new generation mainly stores objects that will soon be recycled by GC or are not particularly large. (2) The old age is for objects that are still alive or very large after being recycled several times in the program. (3) The meta space is used to store some information, constant pool, method data, method code and so on. Things in this area are much harder to recycle than older and newer generations.Copy the code

-xloggc :gc.log I configured in Eclipse.ini

-XX:+PrintGC -Xloggc:gc.log

When you run Eclipse, you get a gC.log file for logging

-xmx — Xms: specifies the maximum heap and minimum heap.

-Xms256m -Xmx1024m

Then we run the following code in the program:Copy the code

System.out.println(“Xmx=” + Runtime.getruntime ().maxMemory() / 1024.0/1024 + “M”); Println (“free mem=” + Runtime.getruntime ().freememory () / 1024.0/1024 + “M”); system.out.println (“free mem=” + Runtime.getruntime (). Println (“total mem=” + Runtime.getruntime ().totalMemory() / 1024.0/1024 + “M”); // Total available space:

2. -xMN, -xx :NewRatio, -xx :SurvivorRatio -Xmn Sets the Cenozoic age size

-xx :NewRatio The ratio of the Cenozoic era (Eden +2*s) to the old age (not including the meta-space). For example, 4 indicates the Cenozoic era. The old age =1:4, that is, the New generation accounts for 1/5 of the whole heap

-xx :SurvivorRatio Sets the ratio of two Survivor zones to Eden. For example, 8 indicates two Survivor zones: Eden =2:8, that is, one Survivor accounts for 1/10 of the young generation

For example:

-Xmx20m -Xms20m -XX:NewRatio=1 -XX:SurvivorRatio=3

Indicates that the maximum and minimum heap memory is 20M, and the generation: old generation is 1:1. The ratio of two Survivor zones to Eden is 2:3

-xx :PermSize -xx :MaxPermSize — Set the initial space and maximum space of the metadata area

Summary of optimization:

(1) The Cenozoic generation is officially recommended to account for 3/8 of the heap. (2) The surviving generation accounts for 1/10 of the Cenozoic generationCopy the code

1. -xSS is usually only several hundred K

Determines the depth of a function call

Each thread has its own stack space

Local variables and parameters are allocated on the stack