First of all, you have to know that JVM heap memory is divided into old and young, young is composed of eden and survivor, and survivor is composed of s0 and s1
The picture is more obvious: (The ratio of 7/3 is not fixed)
Or use another picture to make it more obvious
When the Eden area reaches a certain amount, the Minor GC is triggered, that is, the surviving objects in Eden and S0 are copied to S1 and then Eden and S0 are cleared, and then the next time the Minor GC is triggered, the surviving objects in Eden and S1 are copied In S0, then clear Eden and S1. When the number of copies of an object reaches 16 times, the object will be sent to Old. The animation below will be more intuitive.
If we manually set the JVM memory to be smaller, GC will be more obvious,
Set in eclipse
You can see that the initial heap size of the JVM we set is 128M, and then look at the memory
Its GC will be more frequent
Look closely at this picture and you will find a formula
30.5 + 6 +6 + 85.5 = 128
The 128 here is the heap memory size set in eclipse
What objects are there in the heap of memory?
It can be viewed through the tool JavaVisualVM that comes with the JDK (under the JDK installation directory, for example: jvisualvm.exe under D:\java\jdk1.8\bin)
There are so many objects, and the objects we may be most concerned about are those written by ourselves, then we can use the following regular expression to filter,
For example, if the package name of my project is com.sc.xxxx, then search: ^com.sc.*$ will get the object written by ourselves
Dynamically look at the relationship between objects and memory before GC
This is a spring project. If you look at it several times, you will find that the Student instance will increase every time you request, but the number of StudentServiceImpl instances has always been 1.