Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points of JVM memory partition and GC

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

JVM memory partition and GC knowledge point is what, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

Java Memory Partitioning

Native method stack: method call stack when native method is invoked, storing native stack frames

Virtual machine method stack: Java stack, each thread has a thread call stack, stack elements are stack frames. Stack frames include: local variable tables, operand stacks, references to objects in the heap, return addresses, and additional information. When each method is invoked, a stack frame is pushed back to the top of the currently pointed thread stack. The size of the stack frame is fixed, and the virtual machine can know it by parsing the.class file.

Heap: Heap is a memory area shared by all Java threads and used to allocate objects.

Method area: stores class information (class name, method information, field information), static variables, constant pool.

Program counter: used to record the next instruction to be executed, each thread has its own program counter, with the thread stack used in thread scheduling thread context switch. No value or undefined in program counter when executing native method.

HotSpot heap memory

1.8 Previously, heap memory in HotSpot was divided into new generation, old generation and permanent generation. A permanent generation is an implementation of a method area that takes up a portion of heap memory, but does not participate in garbage collection. 1.8 HotSpot divides heap memory into new generation and old generation. Method area is implemented by metadata area, and metadata area occupies memory outside heap.

Common GC algorithms

Reference Count: Reference count +1 for each object reference held. Reference count-1 when reference assignment is null or destroyed. A reference count of 0 indicates that it is no longer used and can be recycled. Unable to resolve circular reference problem.

Marking-cleaning algorithm: the first stage marks, marking out the living objects; the second stage is cleaning, recycling the memory occupied by the dead objects.

Mark-sort algorithm: the first node mark, mark the inventory of objects; the second stage of sorting, the survival of the object into another space to sort out, and then the current space of all memory recycling.

Copy algorithm: each time only use half of the memory, when the memory is full, the inventory object copied to the other half, the half of the memory recovery.

Garbage collection algorithm is not the best one, in order to achieve the purpose of reducing the overall GC time, it is generally adopted to garbage collection memory generation, each generation uses a different collection algorithm.

HotSpot heap memory generation

The default memory ratio between Cenozoic and Old Generation is 1:2. Cenozoic can be divided into Eden area, Survivor0 and Survivor1 area, and the default ratio of Cenozoic memory is 8:1:1.

Old age Old age is a large block of continuous memory, no subdivision.

HotSpotGC Type

MinorGC, also known as Young GC, YGC, is a GC that occurs in the Cenozoic era and is triggered when the Eden area is full. Newly allocated objects are generally in the Eden region, unless the condition is met, the objects are allocated directly to the heap. When Eden becomes full, Eden survivors are copied to survivor0. When survivor0 runs out of memory, copy the surviving objects in survivor0 to survivor1 area, recycle survivor0 area, and then exchange the names of survivor0 and survivor1. Also called from and to. Each copy from from to records the age of the object +1. When the age of the object reaches a set value (default 15), the object is assigned to the old age. If the copy from to finds that there is not enough memory in the to, the object will also be dumped to the old age.

Direct old generation allocation memory situation:

The object requires memory size that exceeds the Eden zone size and is supported by all collectors.

Eden area memory is insufficient, and the object required memory size is more than half of Eden area, directly allocated to the old age, and does not trigger MinorGC. Supported when using ParallelScavenge.

When the object size exceeds XX:PretenureSizeThreshold setting, it directly enters the old generation allocation. Serial, ParNew, CMS collector support.

FullGC FullGC is a GC that occurs in the old age. The trigger timing is:

When System.gc() is called, GC is recommended, but not immediately.

When objects promoted from a new generation to an older generation require more memory than is available in the older generation.

GC throughput = user code execution time/ (user code execution time + GC time)

HotSpot Garbage Collector

jdk 1.7 and 1.8 default to Parallel GC, which is Parallel Scavenge +Parallel Old GC. Jdk9 uses G1 garbage collector.

Other garbage collectors include CMS, ParaNew, Serial, etc. View java default garbage collector commands:

java -XX:+PrintCommandLineFlags -version-XX:+UseParallelGC means Parallel Scavenge + Parallel Old-XX:+ UseConcepMarkSweepGC When launching an application, adding this parameter means CMS garbage collector-XX:+UseG1GC When launching an application, adding this parameter means G1 garbage collector is used. Serial and Serial Old are supported in jdk8.

Using single thread for garbage collection, GC stops working for threads. It performs well in a single-CPU environment because there is no GC thread interaction. Serial is used for the Cenozoic generation, replication algorithm. Serial Old for older generations, marker-based-finishing

ParNew

Serial multithreaded version, using replication algorithm. It is not as good as Serial in single CPU, and the effect of multiple CPUs is better. ParNew is used for the Cenozoic generation.

Parallel Scavenge

Parallel Scavenge uses a replication algorithm. The algorithm aims to improve throughput, reduce GC time (but possibly increase GC count), and give user code more opportunity to execute. Parallel stands for parallel, that is, multiple GC threads are running at the same time, but the user thread will still be suspended, and the user thread will continue to execute after the GC is completed. Compared with ParNew collector, you can add startup parameter XX+UseAdaptiveSizePolicy. After adding parameters, you can not set Eden and Survivor area proportion, nor set detailed parameters such as age of promoted old age objects. This algorithm will automatically adjust parameters dynamically according to system operation.

Parallel Old

Parallel Old is an older version of Parallel Scavenge that uses a tag-sort algorithm. Concurrent GC is possible, and the entire GC process is suspended from user thread execution.

CMS Garbage Collector

CMS full name Concurrent Mark Sweep, Concurrent Mark-Recycle Garbage Collector. It's an old GC processor. CMS is divided into four steps:

Initial tagging: tag only GC Roots and older age objects that Cenozoic can directly reference, faster, trigger STW.

Concurrent tagging: This phase of the GC tagging thread executes simultaneously with the user thread, traversing the initially tagged objects and recursively tagging all objects referenced by those objects, a slower process but without triggering STW. Because this stage is concurrent tagging, it may occur that objects are promoted from the new generation to the old age, objects are directly allocated in the old age, object references in the old age change, and references to objects in the old age change in the new generation. For these objects, they need to be re-tagged. This is done by setting the bits in the Card Table where these objects are located to dirty, and recording the newly generated objects and changes in the object reference relationship during the concurrent marking phase (record to Mod-Union Table).

Card Table: Divide the old generation memory into CardPages of equal size. Each CardPage uses a binary bit to indicate whether the internal objects have changed references during the concurrent marking stage. This binary bit array is CardTable. Mod-Union Table: is a structure similar to CardTable; 3. Concurrent pre-cleaning: Find memory regions marked dirty in the concurrent marking phase from Mod-Union Table and re-mark objects whose reference relationships have changed. This phase can be turned off with the parameter CMSPrecleaningEnabled, which is turned on by default.

4. Concurrent interruptible pre-cleaning: loop through From and To area objects, marking reachable old age objects; and loop through DirtyCard markers, not triggering STW. There are three exit conditions for the loop

The number of cycles exceeds CMSMaxAbortablePrecleanLoops setting, default 0, no limit;

Cycle time exceeds CMSMaxAbortablePrecleanTime setting, default 5s, exceeding will exit;

Eden usage rate is lower than 10% during concurrent pre-cleaning, and Eden usage rate will exit after reaching CMSScheduleRemarkEden Penetration (default 50%) after a certain cycle;

The goal of this phase looping is to minimize the instances where the next relabeling phase references older objects, since the next phase triggers STW, and therefore the dirty-state memory should be kept as small as possible to improve throughput. If YGC is triggered within exactly 5 seconds of this loop execution, and then a reference to dirty is processed concurrently at this stage, the object to be re-tagged at the next node is not that large, and the STW time is shorter. The reason why the previous concurrent pre-cleaning node can be omitted is that this step can actually achieve the effect of pre-cleaning, and it is a circular operation. 5. Relabeling: slower, CMS bottleneck, trigger STW, concurrent relabeling. Although Preclean and AbortablePreclean have tried their best to dispose of DirtyCard, they cannot guarantee that DirtyCard will be completely disposed of, so they need to be re-labeled as follows:

Traversing Cenozoic objects, relabeling; this node has to traverse Cenozoic objects, which can take a long time to traverse if Cenozoic usage is high and there are many objects, so if YGC is triggered before relabeling, this step will take a lot less time. CMS can force a YGC before the relabeling phase via the parameter CMSScavengeBeforeRemark, which is turned off by default. If it is turned on, it may appear two consecutive YGC, which is also a waste of time. After a YGC, DirtyCard is not handed over to 5.3 of the relabeling phase for execution, but to 4 interruptible preprocessing for execution.

Traversing GCRoots, relabeling;

DirtyCard traversal, re-label, this time after the first two stages of processing, DirtyCard has been reduced a lot.

Concurrent cleanup: cleanup garbage objects according to the result of marking, without triggering STW, slower.

Concurrent reset: Reset CMS internal data, such as CardTable, Mod-Uniod Table, etc., in preparation for the next GC, without triggering STW.

STW will be triggered in both marking phases of CMS. The difference lies in that the initial marking only marks the old age objects in GC Roots and Cenozoic reachables, and there is no recursive traversal marking, which is faster to complete; while the re-marking phase marks the old age objects in GC Roots, Cenozoic reachables and DirtyCard, and recursively traverses the marking, which is more time-consuming.

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report