As you all know, when the JVM starts, it executes some of the default parameters. In general, the default parameters of these Settings are sufficient for some mundane projects. However, if the project is very large and needs to increase the size of the heap, or if the system is constantly failing without explanation and you want to check the GC log to see what is wrong, you need to set these parameters manually.

Parameter Description

1.verbose:gc

Output GC information from the JVM when the JVM is started. The format is as follows:

Full GC indicates that a Full GC operation has been performed. 178K and 99K indicate the amount of memory before and after GC. 1984K is the total memory capacity. The latter one is the time, in seconds, taken to execute the GC.

2.-XX:+printGC

This printed GC information is the same as the previous one, so I won’t introduce it.

3.-XX:+PrintGCDetails

Print the GC details. The format is as follows:

New generation refers to the new generation of heap memory. Total means total, so this is the total memory size of the new generation. “Used” is how much memory is used. The three that start with 0x represent the bottom boundary, the current boundary, and the high boundary. That’s where the new generation of memory starts, where it’s currently used and where it’s largest.

Eden Space, which is often translated as the Garden of Eden, was created in the new generation, where some objects are created first. The 12288K is the total amount of memory in the Eden region. 91% used, obviously, is how much memory has been used. The next 0x is explained the same way as the previous line.

From Space and to Space are two zones for survivors. It belongs to the new generation. The size of the two sections must be the same. Because the new generation of GC adopts the replication algorithm, only one surviving zone is used at a time. When one surviving zone is full, the surviving objects are copied to another surviving zone, and the last surviving zone is directly emptied. This way there is no memory fragmentation.

Tenured generation means old.

Compacting perm means permanent. Since these two are almost the same format as the one I introduced earlier, I don’t need to introduce them.

4.-XX:+PrintGCTimeStamps

Prints the timestamp of the GC occurrence. The format is as follows:

289.556: [GC [PSYoungGen: 314113K -> 15937K] 405513K -> 107901K, 0.0178568 secs] [Times: User = 0.06 sys= 0.00, real= 0.01secs]

293.271: [GC [PSYoungGen: 300865K -> 6577K (310720K)] 392829K -> 108873K (417472K), 0.0176464 secs] [Times: User = 0.06 sys= 0.00, real= 0.01secs]

Interpretation: 289.556 represents the time elapsed between JVM startup and garbage collection. GC indicates that this is a Minor GC. The Younger generation uses the Parallel Avenge, a multithreaded garbage collector. 314113K->15937K(300928K)] The last one is the size of the entire heap, before and after GC. Times, obviously, represents the time spent by the GC, the time spent by the user garbage collection and the time spent by the system, and ultimately the actual time spent.

5.-X:loggc:log/gc.log

This will specify the location of the output gC.log file. (I write log/gc.log to indicate that the gc log is written to a file called gc.log in the current log directory.)

6.-XX:+PrintHeapAtGC

Indicates that heap information is printed after each GC. (The basic format of this print is basically similar to the second one above, so I don’t need to say more.)

7.-XX:+TraceClassLoading

Monitor the loading of classes. The format is as follows:

Using this parameter, you can clearly see which classes are being loaded.

8.-XX:+PrintClassHistogram

Trace parameters. Press Ctrl+Break to print the following message:

– Display the serial number, number of instances, total size, and type.

The types in here, B and C are actually byte and char.

9.-Xmx -Xms

This means setting the maximum and minimum value of heap memory. Setting the maximum and minimum values does not directly increase heap memory to the specified maximum value when the JVM starts. Instead, the specified minimum heap memory will be allocated first, and if after several GC sessions, the program is not satisfied, then the heap size will be gradually increased, but not directly to the maximum memory.

10.-Xmn

Sets the memory size of the new generation.

11.-XX:NewRatio

The ratio of the new generation to the old. For example: 1:4, that is, the new generation accounted for one-fifth.

12.-XX:SurvivorRatio

Set the ratio of two Survivor zones to Eden zones. For example, 2:8 is a Survivor zone of one tenth.

13.-XX:+HeapDumpOnOutMemoryError

Export heap information to file when OOM occurs.

14.-XX:+HeapDumpPath

Represents the file path to export heap information.

15.-XX:OnOutOfMemoryError

When the system generates OOM, a specified script is executed. This script can be any function. Such as generating a dump file for the current thread, or sending emails and restarting the system.

16.-XX:PermSize -XX:MaxPermSize

Set the memory size and maximum value of the permanent area. Running out of permanent memory can also cause OOM to occur.

17.-Xss

Set the stack size. Stacks are unique to each thread and are typically several hundred k in size.

No one can fail completely as long as he is not afraid to try.