GC introduction and related algorithms

tags: JVM  jvm.gc  java

GC

Action area:

image-20210815101348942

When the JVM performs GC, it does not uniformly recycle these three areas. Most of the time, recycling is the new generation

  • Cenozoic

  • Survival area (form, to)

  • elderly area

    Two types of GC: light GC (ordinary GC) (new generation, survivor area), heavy GC (global GC) (all)

    GC topic:

    1. The memory model and partition of the JVM, what is placed in each area in detail?

    Depend onstack, heap, native method stack, method area, program counter

    • Stack: method, object reference, 8 basic data types

    • Heap: object of instance, member variable (non-static)

    • Method area: The method area is shared by all threads, and the information of all defined methods is stored in this area, statically modified variables and methods, final modified, class information, constant pool

    • Program Counter: Stores the address that points to the next instruction

    • Native method stack: It registers native methods and loads Native Libraies (native libraries) when (Execution Engine) executes the engine.

    2. What are the partitions in the heap? talk about their characteristics

    Eden, from, to, old age,

    • Eden: All objects come out of this new, which is where objects are born, grow, and possibly die. After each GC, Eden becomes empty again, because most of the objects we create are temporary objects, so the elimination rate of objects is still very high, which is suitable for the GC replication algorithm (copy the objects from from to to, and then two exchange identities).

    • from and to: After being cleared by GC, the surviving object will be moved to to, it is dynamically exchanged with from, and to is always empty

    • Old age: Objects in the survivor area will be moved to the old age after a certain number of clears. Objects in this area will be difficult to kill, and the cost of using the copy algorithm is too high. It is suitable for GC's mark clearing algorithm and mark Combination of compression algorithms

    3. What are the GC algorithms?

    • replication algorithm
    • mark clear
    • tag compression

    4. When do light GC and heavy GC happen?

    When the Eden object is full, a light GC will be triggered, and when the objects stored in the old age are full, a heavy GC will be triggered

reference counting

Each object has a counter, space consumption

image-20210815102836729

Because it's not efficient, the jvm doesn't use it

replication algorithm

The question of who is to, who is free and who is to

1. Each GC will move the living objects of the Eden area to the survivor area.Once the Eden area is GC, it will be empty

2. In order to ensure that the to area is clean, the copy algorithm moves the objects in the form to to, and after copying, the two identities are exchanged immediately, dynamically

3. When an object has undergone 15 (default) GCs and has not died, it will enter the old age. , the default times can be modified

-XX: -XX:MaxTenuringThreshold=20 (jvm heap memory tuning) This parameter can be used to set the time to enter the old age

image-20210815150110806

Movement of surviving objects while demonstrating GC

During the first GC, the ones that survived from Eden will go into to, and those from from will also be copied into to.

image-20210815104732222

The result after this GC: Eden becomes empty, from and to identities are swapped

That is, the Eden and to areas are empty after each garbage collection.

Assuming that from is full and the number of times is reached, it will go to the old age

image-20210815105006055

Pros and cons of replication algorithm:

  • Benefit: no fragmentation of memory
  • Disadvantage: waste of memory space (more than half of the space is always empty to). Assuming that the object is 100% alive (extreme case), in this case the cost of the replication algorithm is relatively high

The best usage scenario of the replication algorithm: when the object survival rate is low (the new area), the replication algorithm is used in the new area

Mark Sweep Algorithm

Make a mark on the surviving object, and clean up the unmarked object

image-20210815110049616

The mark occupies memory, there are gaps after clearing, and the time consumption of two scans

  • Pros: No extra space required (destroys the downside of the copy algorithm)
  • Disadvantages: Two scans, a serious waste of time and memory fragmentation.

tag compression

Optimize the problem of memory fragmentation.

Compression: Prevents memory fragmentation. Scan again, moving live objects to one end, but with a time cost.

mark-sweep compression algorithm

image-20210815110613769

mark clear compression

First clean the markup a few times, this will generate a lot of debris,

image-20210815110952984

then use compression

image-20210815111040136

This will save a little time and cost

Summarize

Memory Efficiency: Copy Algorithm > Mark Sweep Algorithm > Mark Compression Algorithm (Time Complexity)

Memory Uniformity: Copy Algorithm = Mark Compression Algorithm > Mark Clear Algorithm

Memory Utilization: Mark Compression Algorithm = Mark Sweep Algorithm > Copy Algorithm

Consider a question: Is there no optimal algorithm?

Answer: No, there is no best algorithm, only the most suitable algorithm. soGC: Generational Collection Algorithm

which isyoung generationBecause the object survival rate is low, it is suitable for usereplication algorithm

andold ageBecause the area is large and the survival rate is high, it is suitable for useMarker Clear + Marker Compression Hybrid accomplish,

It involves tuning, and in the case of ensuring that the memory fragmentation is not too much, it will be compressed again after reaching an order of magnitude.

Answer: No, there is no best algorithm, only the most suitable algorithm. soGC: Generational Collection Algorithm

which isyoung generationBecause the object survival rate is low, it is suitable for usereplication algorithm

andold ageBecause the area is large and the survival rate is high, it is suitable for useMarker Clear + Marker Compression Hybrid accomplish,

It involves tuning, and in the case of ensuring that the memory fragmentation is not too much, it will be compressed again after reaching an order of magnitude.

Intelligent Recommendation

GC-related

Determining whether the object is recoverable 1. Reference counting: If the number determined by the determination target object reference recyclable Each object instance has a reference counter, the ...

GC related

table of Contents What is garbage? How to locate garbage Common garbage collection algorithm Different model Object distribution process Common garbage collection algorithm JVM common garbage collecto...

Introduction to image enhancement related algorithms ------ 1

1. Understanding of reinforcement Image enhancement is to emphasize certain information in the image, enhancing the overall or local features of the image. Common methods are: statistical square image...

Introduction to HTTPS tomcat configuration and related encryption algorithms

  HTTPS = SSL + HTTP 1. First use jdk's own tool keytool to generate a "server certificate" Enter your cmd command line, in the jdk installation directory bin directory (if no environme...

Introduction to Data Structure - Tree Related Algorithms (1)

Written in front tool Traversal of binary tree Definition of binary list Recursive implementation Non-recursive implementation Reference book Written in front This series is an article with a record a...

More Recommendation

Introduction to GC

1. How to judge an object as garbage In order to view the garbage collection information -verbose:gc -XX:+PrintGCDetails You can see the gc information   1.1 Reference counting method Add a refer...

Classification of GC algorithms

Classification of GC algorithms Serial GC (-XX:+UseSerialGC) The Serial Collector is a new generation collector that executes in a single thread and uses a replication algorithm. It must suspend all o...

GC concepts and algorithms

The full name of GC is Garbage Collection, which is garbage collection. In C++, the programmer collects useless garbage objects and releases the memory space. In Java, this part of the work is automat...

GC analysis of algorithms

Article Directory GC jdk1.7 jdk1.8 Heap memory tuning Automatically trigger garbage collection Three GC algorithm Replication algorithm Advantages and disadvantages scenes to be used component Clear l...

GC common algorithms

1.GC (Garbage Collection) java garbage collection is an automatic process to manage the runtime memory used by programs.by doing it automatic JVM relieves the programer of the overhead of assigning an...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top