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:

! [](https://pic2.zhimg.com/80/v2-c387d0662e1d84de80481ef0793ee885_720w.jpg)

View JVM memory

! [](https://pic4.zhimg.com/80/v2-4e8555736829e89812d2629a201c1b5b_720w.jpg)
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

! [](https://pic3.zhimg.com/80/v2-53836f2951f424ef8e38e6e2a80c7e3a_720w.jpg)

Run our Spring Boot project and visit

http://localhost:8080/gc

But it didn’t take long to report an anomaly

! [](https://pic2.zhimg.com/80/v2-1e7158e28bbace9326724d38b9c2d831_720w.jpg)

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?

! [](https://pic4.zhimg.com/80/v2-4e8555736829e89812d2629a201c1b5b_720w.jpg)

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

! [](https://pic4.zhimg.com/80/v2-4e8555736829e89812d2629a201c1b5b_720w.jpg)

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

! [](https://pic2.zhimg.com/80/v2-bac05b9705c954a5433c77158a897fcd_720w.jpg)

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;

! [](https://pic2.zhimg.com/80/v2-453eea717f9065b5fdc2defebf893d35_720w.jpg)

Easiest to use

! [](https://pic4.zhimg.com/80/v2-e408147aff1dfb58636912a5d09b8597_720w.jpg)

JPS -q lists only process ids and nothing else

! [](https://pic3.zhimg.com/80/v2-d30dbc901156287ba963270c4758d29e_720w.jpg)

JPS -m lists the classes that the code passes to the main method

! [](https://pic3.zhimg.com/80/v2-afb02256733488113fe5583422da1a0a_720w.jpg)

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

! [](https://pic3.zhimg.com/80/v2-28f20c5e5175749630606684d52d867e_720w.jpg)

JPS -v outputs JVM parameters when a vm process starts

! [](https://pic4.zhimg.com/80/v2-4de1f7d3bc3bd4c6045b4e6dc3d7485b_720w.jpg)

Jstat tools

! [](https://pic4.zhimg.com/80/v2-4e8555736829e89812d2629a201c1b5b_720w.jpg)

Jstat is a lightweight widget that comes with the JDK. Java Virtual Machine Statistics Monitoring Tool, same as JPS, in the bin directory.

! [](https://pic4.zhimg.com/80/v2-947dc430865974a9b52e64792075a493_720w.jpg)

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.

! [](https://pic4.zhimg.com/80/v2-8f7c97aa2dff702fb0d702c1c13cfc1f_720w.jpg)

Jstat-gc Parameter description

  • S0C: The size of the first survival zone
  • S1C: Size of the second survival zone
  • S0U: Use size of the first survival zone
  • S1U: Usage size of the second survival zone
  • EC: Size of Eden Park
  • EU: Usage size of Eden Park
  • OC: Old age size
  • OU: Used size in the old days
  • MC: Method area size
  • MU: Method area usage size
  • CCSC: Compresses the class space size
  • CCSU: Compressing the size of class space usage
  • YGC: Garbage collection times of young generation
  • YGCT: Young generation waste recycling consumption time
  • FGC:Full GcThe number of
  • FGCT:Full GcElapsed time
  • GCT: Total garbage collection time

To see why GC occurs:

1jstat -gccause pid 1000 10
Copy the code

! [](https://pic1.zhimg.com/80/v2-3677e97263f6ce845b54a5a7afa5929c_720w.jpg)

jstat -gcutil 5552 1000 20

jvm

! [](https://pic2.zhimg.com/80/v2-213813cff17e0ae8fc691798d424b7b5_720w.jpg)

View the loading class of the process ID: jstat-class PID

! [](https://pic3.zhimg.com/80/v2-40069fe34b070f22db4f28e077164502_720w.jpg)

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 ~