Click “like” to see again, form a habit, wechat search [Three prince Aobing] to pay attention to the Internet tools of survival.

This article has been included in GitHub github.com/JavaFamily, there are a line of large factory interview complete test sites, information and my series of articles.

Last time I told my friends about the process of checking the high CPU in an infinite loop. Today I will take you to see how we usually check the overflow of heap memory.

  • Cpu100 % check article

Before checking, I think you should know the basics of the JVM, right?

Wife, I just don’t understand. They want you to talk to me.

The JVM memory model is not the same as the JAVA memory model JMM.

I’m going to say heap today, because an overflow is sent in the heap.

The JVM heap memory is divided into two parts: the Young Generation and the Old Generation.

The young generation

The young generation is where all the new objects are created. Garbage collection is triggered when young generation memory space is used up. This garbage collection is called the Minor GC.

The young generation was divided into three sections — the Enden zone and two Survivor zones.

Key points of young generation space:

  1. Most of the new objects are located in Eden.
  2. When the Eden region is filled with objects, a Minor GC is performed and all surviving objects are moved to one of the survivor regions.
  3. The Minor GC also checks the surviving objects and moves them to another survivor zone. There will always be an empty survivor zone for a period of time.
  4. After multiple GC cycles, objects that are still alive are moved to the aged memory space, usually by setting an age threshold before the younger generation is eligible to be promoted to the aged.

Old generation

Tenured memory, which contains long-lived objects and objects that have survived multiple Minor GC’s, is typically garbage collected when tenured memory is full.

GC type

Major GC

In the old days, garbage collection was called Major GC, which is usually equivalent to full GC, collecting the entire GC heap.

Generational GC

  1. Young GC: Only GC of the Young generation is collected
  2. Old GC: Collects only Old generation GCS (only concurrent collections from CMS are in this mode)
  3. Mixed GC: Collect the entire YOUNG Gen and some old Gen GC(only G1 has this mode)

Full GC

Full GC is defined as a relatively unambiguous, global-scoped GC for the entire new generation, old generation, metaspace (java8 + replaces Perm Gen).


As can be seen from the figure above, the young generation is divided into one Eden zone and two survivor zones (S1, S2). One survivor zone is filled and the other is empty at the same time, alternating.

Then it’s GC to a certain threshold to the old age, so we’re not going to talk about permanent generation today so we ignore Mataspace.

Wife: that how analysis?

Today I will use a JDK built-in tool jVisualVM to show you how to operate a wave, because this stuff everyone has, you go to the command line to click jVisualVM out (Mac is such, do not know how Windows is like).

Operation interface:


In general, when might overflow occur?

Abnormal problems such as timeout, no service, service hanging, interface not in service.

So the simulation is pretty simple, so I’m going to do a loop and I’m going to keep throwing data into the List, and I’m not going to use the List to see what happens


As you can see, the graphical interface is pretty clean. This is a plug-in for Visual GC


Click on the menu bar for plugins, and then install them. When you’re done, click activate.


As you can see, the heap space will continue to increase until OOM (out of Memory).


This time we dump the heap information to see


This will dump a hprof snapshot file, which can be analyzed using jVisualVM’s own system, I recommend MAT here, because I am used to this.


MAT: download address

Come down and we can see that MAT has analyzed our file


You see he is a warm man, have helped us analyze a problem, let’s click in to see


He found the ArrayList problem, so let’s go ahead and see


See, we’ve got the exact location of the code, so we should be able to get a clean sweep.


Extension point

Dump > dump > dump > dump > dump

jmap -dump:format=b,file=<dumpfile.hprof> <pid>

Copy the code

Some friends may ask, not all the faults we were present at the time, can not timely dump, that is also simple

-XX:+HeapDumpOnOutOfMemoryError

Copy the code

After configuring this device, the oom will automatically dump, then take a snapshot analysis wave.

There are many other functions of MAT. There are too many tools on Baidu and Google, so I will not do repetitive work, such as checking strong and weak references of objects and checking reference chains.

There is also a reason why I only write this point is that it is a little late and I can’t stand it. The reason why I didn’t shoot the video recently is that I have too many things to do and I am a little busy. I hope you are all right.

I am Aobing, a tool to survive on the Internet.

The best relationship is mutual achievement, everyone’s “three lian” is the biggest motive force of bing Bing’s creation, we will see you next time!

Note: If this blog has any mistakes and suggestions, welcome to leave a message, you quickly say something ah!


[Information] [interview] [resume] I prepared the interview materials and resume template, this article GitHub github.com/JavaFamily has been included, there are big factory interview complete test site, welcome Star.


The more you know, the more you don’t know