Memory overflow, commonly understood, is when you ask for more memory than the JVM can give you, the JVM can’t meet the demand, and an overflow occurs. For ease of understanding, this article uses an example to illustrate memory overflow.
First, let’s take a look at the main framework of this article:
View JVM memory
1public class PrintGCDetailsDemo {2 public static void main(String[] args) {3 system.out.println ("Xmx=" + Runtime.getruntime ().maxMemory() / 1024.0/1024 + "M"); Println ("free mem=" + Runtime.getruntime ().freememory () / 1024.0/1024 + "M"); Println ("total mem=" + Runtime.geTruntime ().totalMemory() / 1024.0/1024 + "M"); 10 9}}Copy the code
Running the above code outputs the memory status of the corresponding JVM heap.
1Xmx= 1796.0m 2free mem= 119.08892822265625m 3total mem= 123.0mCopy the code
Memory overflow code cases
Let’s take a look at a demo case with the following code:
1@RestController 2public class GCController { 3 4 List<Object> strings = new ArrayList<>(); 5 6 @GetMapping("/gc") 7 public String addObject() { 8 System.out.println("-------gc-------"); 9 for (int i = 0; i < 1000000; i++){ 10 try { 11 Thread.sleep(20); 12 } catch (InterruptedException e) { 13 e.printStackTrace(); 14 } 15 int [] a=new int[500000]; 17 strings. Add (a); 18 } 19 return "ok"; 20}} 21Copy the code
Set the maximum heap memory to 10m using -xmx10m
Then set it in IDEA
Run our Spring Boot project and visit
http://localhost:8080/gc
But it didn’t take long to report an anomaly
This OOM is the spot where problems can be found at a glance. It’s pretty easy to look at it that way.
However, in our business code, OOM may be caused by a while or if condition judgment, which is not so easy to check, because you need to figure out which part of the code to clear the error, and what business scenario will be implemented in such code.
What are the heap parameters?
The following are the most common heap JVM-related parameters
-xx :PrintFlagsFinal :PrintFlagsFinal :PrintFlagsFinal :PrintFlagsFinal :PrintFlagsFinal :PrintFlagsFinal :PrintFlagsFinal :PrintFlagsFinal :PrintFlagsFinal :PrintFlagsFinal Maximum heap size memory (default: 1/4 of physical memory) -xmn: Set new generation size (initial and maximum) -xx :NewRatio: -xx :SurvivorRatio: set the ratio of Eden and S0/S1 Spaces in the new generation. -xx :MaxTenuringThreshold: The biggest age 15) (the default setting new generation garbage – XX: + PrintGCDetails: output detailed GC log printed the brief information of GC: (1) – XX: + PrintGC (2) – verbose: GC – XX: HandlePromotionFailure: Whether to set space allocation guarantee
The new generation memory is too small
Parameter Settings, the new generation set to 1M
-Xmx200m -Xms200m -Xmn1m -XX:+PrintGCDetails
Continue to visit the above
http://localhost:8080/gc
Then continue to look at the garbage collection log
Our new generation memory setting was too small, so the code above started Full GC, and then there was not enough space in the new generation to store new data.
The JPS utility
JSP query all HotSpot processes in the system, it is located in the Java bin directory, in this bin directory has a lot of useful tools, here talk about JPS;
Easiest to use
JPS -q lists only process ids and nothing else
JPS -m lists the classes that the code passes to the main method
JPS -l displays the full name of the currently running main class. JPS can only see the class name, but cannot see the fully qualified class name
JPS -v outputs JVM parameters when a vm process starts
Jstat tools
Jstat is a lightweight widget that comes with the JDK. Java Virtual Machine Statistics Monitoring Tool, same as JPS, in the bin directory.
Real-time command line monitoring of Java application resources and performance using JVM built-in instructions, including Heap size and garbage collection status monitoring. As you can see, Jstat is a lightweight, JVM-specific tool that works just fine. The Jstat tool is particularly powerful, with numerous options to look in detail at how much each part of the heap is being used and how many classes are being loaded. To use this parameter, add the process ID of the process you want to view.
We often use the command to view GC information
1jstat -gc pid 1000 10
Copy the code
Displays GC information about the PID process once every second (1000 ms) for 10 times.
Jstat-gc Parameter description
S0C
: The size of the first survival zoneS1C
: Size of the second survival zoneS0U
: Use size of the first survival zoneS1U
: Usage size of the second survival zoneEC
: Size of Eden ParkEU
: Usage size of Eden ParkOC
: Old age sizeOU
: Used size in the old daysMC
: Method area sizeMU
: Method area usage sizeCCSC
: Compresses the class space sizeCCSU
: Compressing the size of class space usageYGC
: Garbage collection times of young generationYGCT
: Young generation waste recycling consumption timeFGC
:Full Gc
The number ofFGCT
:Full Gc
Elapsed timeGCT
: Total garbage collection time
To see why GC occurs:
1jstat -gccause pid 1000 10
Copy the code
jstat -gcutil 5552 1000 20
jvm
View the loading class of the process ID: jstat-class PID
conclusion
I encourage you to combine what you’ve learned with the PARAMETERS of the JVM and take the time to play around with them so you can understand them better.
Come on! While young, do not refueling on the old cough ~