This article will help you understand JVM tuning

Table of contents

1. Memory leaks and memory overflows

1. Memory leak

2. Memory overflow

2. Methods to improve garbage collection efficiency

3. Commonly used commands

4. Commonly used JVM parameters

1. Set the heap memory size

2. Set the new generation memory size

3. Set the metaspace memory size

4. Set which garbage collector to use

5. Record garbage collection logs


1. Memory leaks and memory overflows

The difference between memory leak and memory overflow:

  • Memory leak: The memory space occupied by objects that are no longer used should be released, but is not garbage collected.
  • Memory overflow: When the program is running, sufficient memory resources cannot be applied for.

1. Memory leak

cause:

  • Use static collection classes. Static variables will not be garbage collected, and collections generally occupy a large amount of memory.
  • Various connections are not closed in time, such as database connections and IO connections.
  • Some strongly referenced objects are not set to null after they are no longer used, resulting in the inability to be recycled.
  • The scope setting of the variable is unreasonable and the survival period is too long.
  • Too many singleton pattern classes

solution:

  • Avoid creating objects in loops and try to reuse objects
  • Release useless object references promptly
  • Use less static collections
  • Close the connection promptly
  • For String operations, use StringBuilder or StringBuffer instead of concatenating strings directly.

2. Memory overflow

Memory overflow situation:

  • Virtual machine stack and local method stack overflow
    • If the stack depth requested by the thread is greater than the stack depth allowed by the virtual machine, a StackOverflowError is thrown.
    • If the virtual machine cannot apply for enough memory space when expanding the stack, an OutOfMemoryError is thrown.
  • Heap overflow
    • When encountering a heap overflow, you must first determine whether a memory leak or a memory overflow has occurred.
    • If it is a memory leak, use tools to view the reference chain from the leaked object to GC Roots to find out why the leaked object cannot be automatically recycled by the garbage collector.
    • If there is no memory leak and no problem is found in the code, consider increasing the memory parameters of the virtual machine (-Xmx and -Xms).
  • Method area overflow
    • Consider modifying the memory size occupied by the method area
  • Runtime constant pool overflow

Reasons for memory overflow:

  • The data loaded in memory is too large
  • There is an infinite loop in the code, or a large number of objects are created
  • Virtual machine memory parameter settings are too small

Solution to memory overflow:

  • Modify JVM memory parameters. Generally, -Xms and -Xmx are set to the same value to avoid adjusting the heap size after each GC. The heap is generally set to 80% of physical memory
  • Check the error log to see the actual reason behind the memory overflow
  • Conduct code walkthrough and analysis to identify error-producing codes
  • Use memory viewing tools to dynamically monitor memory usage, try to reproduce memory overflow scenarios, and then discover problems.

2. Methods to improve garbage collection efficiency

  • When an object is no longer used, explicitly set it to null promptly to facilitate quick judgment by the garbage collector.
  • Use System.gc() less as it triggers Full GC
  • Use less static variables. Static variables will serve as GC Roots and will never be recycled.
  • For String operations, use StringBuilder or StringBuffer instead of concatenating strings directly. Because a lot of extra string objects will be generated on the heap
  • Spread the timing of object creation and destruction. Do not suddenly create or destroy a large number of objects, as this may trigger Full GC.
  • Use less finalize() method, which will increase the burden of garbage collection.
  • Make good use of weak references and soft reference types, do not use all strong references
  • If you can use basic data types, don't use wrapper types. Because the wrapper type takes up more memory
  • Increasing the heap memory can reduce the frequency of garbage collection.

3. Commonly used commands

4. Commonly used JVM parameters

1. Set the heap memory size

-Xms<heap size>[unit] 
-Xmx<heap size>[unit]
  • heap size: memory size
  • unit: unit, such as k, m, g

For example, to set the minimum to 2G and the maximum to 5G:

-Xms2g -Xmx5g

2. Set the new generation memory size

The default new generation memory is about 1G, with no upper limit.

There are two ways to set the young generation memory size:

Specify minimum and maximum values

-XX:NewSize=<young size>[unit] 
-XX:MaxNewSize=<young size>[unit]

Specifies that the new generation memory is a fixed value

-Xmn256m 

Set the ratio of the new generation to the old generation

-XX:NewRatio=1
  • The meaning is, the memory ratio of the entire new generation (including Eden, From Survivor, To Survivor) and the old generation

3. Set the metaspace memory size

Because the metaspace is on direct memory, and direct memory corresponds to the host's physical memory.

Therefore, if there is no limit on the size of the metaspace, a large number of classes will be created during operation, which may exhaust all the memory of the host.

Set the minimum and maximum values ​​of the metaspace

-XX:MetaspaceSize=N
-XX:MaxMetaspaceSize=N

4. Set which garbage collector to use

-XX:+UseSerialGC
-XX:+UseParallelGC
-XX:+UseParNewGC
-XX:+UseG1GC

5. Record garbage collection logs

-XX:+UseGCLogFileRotation 
-XX:NumberOfGCLogFiles=< number of log files > 
-XX:GCLogFileSize=< file size >[ unit ]
-Xloggc:/path/to/gc.log

Guess you like

Origin blog.csdn.net/m0_62609939/article/details/131919548