Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

In general, we know that when The Eden field of The new generation is full, minor GC will be triggered, and when The Eden field of The old generation is full, full GC will be triggered, and full GC will Stop The World. Did you know that there is another field that is full for a while that will trigger full GC? And when this area is full, it directly affects our development efficiency.

1. Parameter tuning of method area

We can set memory parameters for the runtime data area. This is the focus of JVM tuning. Changes in parameters will affect the overall efficiency

Core parameters are set as follows:

java -Xms2048M -Xmx1024M -Xss512k -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M -jar microservice-eureka-server.jar
Copy the code

This is a universal setting. The specific meanings in the figure are as follows:

  • -Xms: indicates the minimum heap space
  • -Xmx: indicates the maximum heap space
  • -Xmn: the space occupied by Cenozoic generation
  • -xx :MetaspaceSize: indicates the initial value of the method area (meta-space)
  • -xx :MaxMetaspaceSize: indicates the maximum value of the method area (meta-space)
  • -Xss: indicates the space size of each thread

The parameter setting of the method area is mainly studied below

1. Set method area (meta-space) parameters

Before JDK8 there was a region called permanent generation, but in JDK8 and later it was renamed metacolor. This space occupies direct physical memory.

Meta-space has one feature: it can be dynamically expanded. If we do not set the upper limit of the meta space, then it can be expanded to the entire memory. For example, if the memory is 8GB and the heap and stack are allocated 4G of space, then the metadata space can be used up to 4G.

We can set the amount of meta-space memory to be used with parameters.

For 64-bit JVMS, the default size of the meta-space is 21M, and the default maximum size of the meta-space is unlimited

  • -XX:MetaspaceSize: The initial size of the meta-space, in bytes, defaults to 21M. Full GC is triggered when this value is reached, and the collector adjusts this value. If a large amount of space is freed, this value is reduced appropriately. Increase the value up to a maximum of -xx :MaxMetaspaceSize
Such as: The initial value is 21M, the first reclaim 20M, then only 1M is not reclaimed, the next time, the meta space will automatically adjust the size, may be adjusted to 15M, the initial size is still 21M, the second reclaim found 1M, 20M is not reclaimed, it will automatically expand the space, It could be 30M, it could be 40MCopy the code
  • -xx :MaxMetaspaceSize: Sets the maximum value of the metasize space. The default value is -1

Since resizing the metaclass requires full GC, which is a very expensive operation, if an application has a large number of Full GC at startup, it is usually due to a permanent generation or metaclass resizing. In this case, It is generally recommended that the JVM parameters -xx :MetaspaceSize and -xx :MaxMetaspaceSize be set to the same value and larger than the initial value. For a machine with 8 GIGABytes of physical memory, either 256MB or 512MB will work

2. It is recommended to set the meta space value. What if you do not set the meta space value?

If the meta space is not set, the default is 21M, which can easily fill up. Usually, our war may be dozens of M, or even several G. If we start the program, it will start for a few minutes. This is most likely due to not setting the size of the meta-space.

Full GC occurs when it is full, and then it expands a bit to 25M and starts again. After a while it is full again, and then it expands a bit to 30M, and so on. Until the size of the expanded meta-space is sufficient to prevent full GC from occurring, the program will start up and run properly. This is a time-consuming and performance-intensive operation, and a full GC like this is unnecessary.

If the project starts slowly and repeatedly, consider whether the meta-space Settings are incorrect or the memory is insufficient.

Two. Thread stack parameters tuning

-xSS512K: sets stack space parametersCopy the code

This parameter is used to set the stack space. This is the amount of space taken up by a single thread stack. A program may have multiple thread stacks after startup, so they all take up 512K space.

After a program is started, the system allocates a fixed amount of stack space to it. In theory, if each thread takes up less space, more threads can be allocated. Otherwise, the opposite is true. But this is not certain, and the system CPU, memory is related.

When the thread runs out of space, a stack overflow exception is thrown. Let’s look at an example

package com.lxl.jvm; Public class StackOverflowTest {/** * static int count = 0; /** * static int count = 0; public static void redo(){ count ++; redo(); } public static void main(String[] args) { try { redo(); }catch (Throwable e) { e.printStackTrace(); System.out.println(count); }}}Copy the code

There is a variable called count, and the main method calls redo(). What does the thread stack model look like when we execute main?

When the program executes to the main method, it opens a frame for the main method in the thread stack

To continue, redo() opens a redo stack frame in the thread stack

The redo method is called from the redo method. Continue to create a redo stack frame,

.

Stack frames take up memory space. At some point the stack will run out of memory. I’m going to run out of stack memory

We saw 16,979 stack overruns in the program.

What happens when the stack space is set smaller? Such as 256 k

Let’s run it and see what happens

When running 2079 times, a stack overflow occurred.