This is the second day of my participation in the August More text Challenge. For details, see: August More Text Challenge
GC
Introduction (master skills)
Familiar with common GC algorithms, familiar with common garbage collectors, with actual JVM tuning experience
Basics of GC
What is garbage
One or more objects without any reference (circular reference)
Garbage collection (Java vs c++)
- java
- Garbage disposal by GC
- High development efficiency, low execution efficiency
- C++
- Manual disposal of garbage
- Low development efficiency, high execution efficiency
Automatic memory recovery, programming is simple, the system is not prone to error, manual release of memory, prone to two types of problems:
- Forget to reclaim (memory leak)
- Multiple reclaim (illegal access)
How to Locate garbage
- Reference counting (ReferenceCount)
Can’t solve the circular reference problem
Common garbage collection algorithms
- Mark sweep
- Position discontinuous, easy to produce debris, low efficiency
- Double scan: The first scan is for unrecyclable objects, and the second scan is for recyclable objects
- [Efficiency is high when there are more surviving objects]
- Copying algorithm
- No fragmentation, wasted space, moving copies of objects, need to adjust references to objects
- [Applicable to the case where there are few surviving objects]
- Scan only once, improve efficiency, suitable for Eden zone
- Mark Compact
- No fragmentation, no memory halving, easy object allocation
- The object needs to be moved and the pointer needs to be adjusted, so the efficiency is low
JVM memory generational model (for generational garbage collection algorithms)
An object goes from birth to death
The concept of GC
-
Part of the model used by garbage collectors
All GCS except Epsilon ZGC Shenandoah use the logical generational model
G1 is a logical generation, not a physical generation
In addition not only logical generation, but also physical generation
-
Cenozoic + Old + permanent (1.7) Perm Generation/ Metaspace (1.8
- Permanent generation metadata – Class
- Permanent generation must specify size limit, metadata can be set or not set, no upper limit (limited to physical memory)
- String constants 1.7 – permanent generation, 1.8 – heap
- MethodArea Logical concept – permanent generation, metadata
-
Cenozoic = Eden + 2 Suvivor
- After YGC collection, most of the objects will be collected and enter S0 alive
- YGC again, the living object Eden + s0 -> s1
- Again YGC, Eden + s1 -> s0
- Adequate age -> old age (15 CMS 6)
- Block S won’t fit -> Old age
-
The old s
- diehard
- The old age is Full FGC Full GC
-
GC Tuning (Generation)
- Minimize FGC
- MinorGC = YGC
- MajorGC = FGC
-
Object allocation process diagram
-
Dynamic age: (not important) www.jianshu.com/p/989d3b06a…
-
Distribution during the period of guarantee: (not important) YGC survivor area space is not enough space guarantee directly into old age Reference: cloud.tencent.com/developer/a…
Object allocation
-
On the stack
- Thread private small objects
- There is no escape
- Support for scalar substitution
- Don’t need to adjust
-
TLAB (Thread Local Allocation Buffer)
- Eden (1% by default) (Each thread has a private space in Eden, which is allocated when the thread is created)
- When multithreading, you can apply for space without competing for Eden, improving efficiency
- Small objects
- Don’t need to adjust
-
The old s
- The big object
-
eden
- New object
Mock instance code
package com.mashibing.jvm.c5_gc;
//-XX:-DoEscapeAnalysis -XX:-EliminateAllocations -XX:-UseTLAB -Xlog:c5_gc*
Escape analysis scalars replace thread-specific object allocations
public class TestTLAB {
class User {
int id;
String name;
public User(int id, String name) {
this.id = id;
this.name = name; }}void alloc(int i) {
// The object has no reference elsewhere, so no escape occurs
new User(i, "name " + i);
}
public static void main(String[] args) {
TestTLAB t = new TestTLAB();
long start = System.currentTimeMillis();
for(int i=0; i<1000 _0000; i++) t.alloc(i);
longend = System.currentTimeMillis(); System.out.println(end - start); }}Copy the code