Understand the Java memory distribution in three minutes, and never be afraid again! ! !

table of Contents

One, heap memory

1.1 Young Generation

1.2 Old Generation

1.3 Meta data (Meta space)

1.4 Summary

Two, off-heap memory

Three, garbage collection

3.1 Garbage Collection (GC)

3.2 GC root

3.3 Common garbage collectors

Four, summary


image
Like it and look again, make it a habit

Most Java developers don’t have the trouble of allocating memory like C++, because Java helps us manage memory, but this does not mean that we don’t need to understand Java’s memory structure, because memory problems often appear online. Let’s talk about memory problems today. .

The memory is divided into in-heap memory and off-heap memory from the big aspect

One, heap memory

The in-heap memory is divided into three parts, young generation, old generation and meta space, so in-heap memory = young generation + old generation + meta space, let's talk about the three parts in detail below

1.1 Young Generation

Stores the objects generated by new

The young generation is to reclaim those objects with a short life cycle as quickly as possible

Eden

Most objects are generated in the Eden area

When the Eden area is full, a young gc will be performed, and the surviving objects will be copied to the Survivor area. When a Survivor area is full, the surviving objects in this area will be copied to another Survivor area

Survivor (usually 2)

When both Survivor areas are full, the objects that are copied from the first Survivor area and are still alive will be copied to the old age (Tenured) if they exceed a certain age

The two areas of Survivor are symmetrical, and there is no precedence relationship. There may be objects copied from Eden and objects copied from the previous Survivor in the same area.

Promote objects whose age is greater than -XX:MaxTenuringThreshold to the old age; (every time the object survives in the Survivor area, its age increases by one year);

1.2 Old Generation

It stores objects that survived N garbage collections in the young generation, and they are some objects with a long life cycle.

Store those objects that take up a lot of space when they are created. These objects do not experience eden and directly enter the old age, large objects (objects with a size greater than -XX:PretenureSizeThreshold)

1.3 Meta data (Meta space)

Storage class data

Store static files, such as static classes and methods. The persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as Hibernate, Mybatis, etc. At this time, a larger persistent generation space needs to be set up to store these newly added classes during operation.

Set persistent generation size parameters: -XX:MetaspaceSize, -XX:MaxMetaspaceSize

1.4 Summary

image

1. Default parameters:

The old age occupies 2/3 of the entire heap memory
The young generation accounts for 1/3 of the entire memory
The Eden area accounts for 80% of the entire young generation, and the From and To survivor areas each account for 10%

2. JVM parameters related to the new and old ages

-XX:NewRatio sets the ratio of the new and old generations, such as -XX:NewRatio=5 means the ratio of the new and old generations is 1:5, the new generation occupies 1/6 of the heap memory, and the old generation occupies 5/6;

-XX:SurvivorRatio Set the ratio of the size of eden and two Survivo areas in the new generation, such as -XX:SurvivorRatio=8, then eden:s1:s2=8:1:1, the default ratio is 8:1:1 .

3. Young GC occurs in the new generation, and FUll GC occurs in the entire heap space. Generally, FULL GC will be launched if the space in the old generation is not enough.

Two, off-heap memory

Our game server uses netty, so just say netty. Netty's ByteBuffer uses DIRECT BUFFERS, and uses off-heap direct memory for Socket read and write. There is no need to perform a second copy of the byte buffer and zero copy of off-heap memory. . Improved efficiency. Because the operating system kernel directly writes the data to the off-heap memory, there is no need to cache a copy of the operating system kernel like ordinary APIs, and copy it to the program space when the program is read.

2.1 There are two ways to open up memory outside the heap in java

Use DirectBufferByteBuffer.allocateDirect(size)

Use JNI to write java's c/c++ extension, without involving jvm in the extension to get the memory out of the system.

2.2 Advantages of using off-heap memory

Reduce garbage collection because garbage collection will suspend other work.

Speeds up copying When flushing from the heap to the remote, it will first be copied to the direct memory (non-heap memory) and then sent; while the off-heap memory is equivalent to omitting this work.

2.3 Disadvantages of off-heap memory

Memory is difficult to control. The use of off-heap memory indirectly loses the feasibility of JVM to manage memory. It is managed by oneself instead, and it is very difficult to troubleshoot when memory overflow occurs.

Three, garbage collection

3.1 Garbage Collection (GC)

Minor GC

Generally, when a new object is generated and fails to apply for space in Eden, MinorGC is triggered, GC is performed on the Eden area, non-surviving objects are cleared, and the surviving objects are moved to the Survivor area, and then the two Survivor areas are sorted.

This method of GC is performed on the Eden area of ​​the young generation and will not affect the old generation.

Since most objects start from the Eden area, the GC in the Eden area will be very frequent.

Major GC / Full GC

Tenured is full

Permanent generation (Permanent) is full

System.gc() is called explicitly

The Heap domain allocation strategy changes dynamically after the last GC

Sort the entire heap.

It takes a long time, so try to reduce the number of Full GC

The occurrence of Full GC is often accompanied by at least one Minor GC (not absolute, the Parallel Sacvenge collector can choose to set the Major GC strategy);

Major GC speed is generally slower than 10 times the Minor GC on.

3.2 GC root

The program regards all reference relationships as a graph, starting from a node GC ROOT, looking for the corresponding reference node, after finding this node, continue to look for the reference node of this node, when all reference nodes are searched, the remaining nodes It is considered a node that is not referenced, that is, a useless node, and an object that needs to release memory.

The objects that can be used as GC Root in java are

1. Objects referenced in the virtual machine stack (local variable table)
2. Objects referenced by static properties in the method area
3 Objects referenced by constants in the method area
4. Objects referenced in the native method stack (Native objects)

3.3 Common garbage collectors

The garbage collector is the concrete realization of memory recovery. Here are some garbage collectors provided by the virtual machine

Serial collector (replication algorithm)

The new generation of single-threaded collectors, marking and cleaning are single-threaded, the advantage is simple and efficient.

Serial Old Collector (Marking-Sorting Algorithm)

The old single-threaded collector, the old version of the Serial collector.

ParNew collector (stop-copy algorithm)

The new generation collector can be considered as a multi-threaded version of the Serial collector, which performs better than Serial in a multi-core CPU environment.

Parallel Scavenge collector (stop-copy algorithm)

Parallel collector pursues high throughput and efficient use of CPU. Throughput is generally 99%, throughput = user thread time/(user thread time + GC thread time). It is suitable for scenarios where interaction requirements are not high, such as background applications.

Parallel Old collector (stop-copy algorithm)

The old version of Parallel Scavenge collector, parallel collector, throughput priority

CMS (Concurrent Mark Sweep) collector (mark-sweep algorithm)

High concurrency, low pause, the pursuit of the shortest GC recovery pause time, high cpu occupancy, fast response time, short pause time, multi-core cpu pursues the choice of high response time

G1(Garbage-First)

The latest collector is now available for both the new generation and the old generation

Collectors used by new generation collectors: Serial, PraNew, Parallel Scavenge

Collectors used by the old collector: Serial Old, Parallel Old, CMS

Our online server uses G1 collector

Four, summary

There are a lot of things listed above, but the following points need to be remembered.

1. The migration path of the object: born in Eden, then migrate back and forth in the Survivor area. The migration will increase the age one at a time, and those who are too old will go directly to the old age.
2. The Eden area is full to produce minor Gc
The old age is full to produce full gc
3. Remember that the collector executes gc, just choose the latest G1 collector
image
Like, one-click triple connection