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 JVM architecture and GC commands?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces you to JVM architecture and GC commands, the content is very detailed, interested partners can refer to, I hope to help you.

1. Overview

2. JVM architecture

1)garbage collection

- Object survivability judgment

- garbage collection algorithm

- Garbage collector (concrete implementation of the recycling algorithm)

2)class loading subsystem

- Start Class Loader

- extended class loader

- application classloader

- custom class loader

3)execution engine

- interpreter

- just in time compiler

4)runtime data area

- Heap

- method area

- program counter

- virtual machine stack

- native method stacks

3. Garbage Collector On Command

4. ReferenceOverview

Java virtual machine(JVM) is a software implementation of a physical machine. java compiler javac compiles the source file.java into a bytecode file.class, which is then placed into the JVM, loaded and executed.class. The JVM architecture is shown below.

JVM Architecture

JVM architecture

Garbage collection: responsible for collecting unused objects in heap memory

Determine whether the object is alive, reachability detection

Reference counting algorithm: Add a reference counter to an object, increment it by 1 whenever it is referenced somewhere, and decrement it by 1 when the reference expires. Any object with a time counter of 0 is an object that is unlikely to be used again

Root search algorithm: through a series of objects named "GC Roots" as a starting point, starting from these nodes down search, search through the path called reference chain, when an object to GC Roots without any reference chain connection, it proves that this object is unavailable

garbage collection algorithm

Mark-Sweep algorithm: first mark out all the objects to be recycled, and then uniformly recycle all marked objects after marking

Efficiency issues: neither mark nor clear actions are efficient actions

Space problem: a large number of discontinuous memory fragments are generated after the tag is cleared, and too many fragments lead to failure to find enough continuous memory when allocating large objects, so another gc has to be triggered in advance.

Copying algorithm: It divides the available memory into two blocks of equal size by capacity, using only one of them at a time. When this block of memory runs out, copy the surviving objects to another block, and then clean up the used memory space at once

Available memory is reduced to half its original size

All tagged objects are copied at each collection, resulting in some long-lived objects being copied back and forth multiple times, consuming a lot of time-> generational recycling to solve this problem

Generational Collecting: Divide memory into blocks according to the life cycle of the object

Cenozoic: Memory is divided into a larger Eden space and two smaller Survivor spaces. New object triggers YGC when Eden is full, the surviving objects in Eden are moved to S0 area, and Eden is cleared; when Eden is full again, the surviving objects in Eden and S0 are copied to S1 (the copy algorithm ensures that the surviving objects from Eden and S0 in S1 occupy continuous memory space, avoiding fragmentation); Eden and S0 are cleared. S0 and S1 switch roles in the next round, and so on. If an object has been copied 15 times, it is sent to the old age.

Old age: high subject survival rate

Garbage Collector (concrete implementation of garbage collection algorithm)

Parallel and concurrent

Parallel: Multiple garbage collection threads work in parallel, but the user thread is still waiting

Concurrent: When the user thread and garbage collector thread execute simultaneously, the user program continues to run while the garbage collector runs on another CPU

Cenozoic minor gc: Cenozoic memory is not very large, mianor gc recovery speed is generally faster

Old age major gc/ full gc: older generation memory is generally larger, used to cache large objects, so the recovery speed is about 10 times more than minor gc

Throughput: JVM runs for 100 minutes, garbage collection takes 1 minute, throughput is 99%,(all-gc)/all (for batch pipeline)

pause time: pause time of the application while the garbage collector is running (for streaming pipeline)

Reclaimer type

NEW

serial, single thread

parNew, serial multithreaded edition

Parallel Scavenge, replication algorithm, parallel, priority to ensure throughput, regardless of user STW feelings, throughput optimization system, long pause time can be received.

OLD/tenured

Serial Old, older version of Serial Collector, single thread, tag-defragmentation algorithm

Parallel Old, Parallel Scavenge Older versions of Reclaimer, multithreaded, mark-clean algorithm

Concurrent Mark Sweep, CMS: priority shortest recovery pause time, good user experience, mark-sweep algorithm

NEW and OLD

Key points: region memory division, priority region recycling method. Prioritize the region with the greatest value

Garbage first, G1: It divides the entire Java heap into multiple independent regions of equal size. Although the concept of Cenozoic and Old Age is still retained, Cenozoic and Old Age are no longer physically isolated. They are all collections of partial Regions (which do not need to be continuous).

classloader subsystem: locate and import binary class files, verify correctness of imported classes, allocate and initialize memory for class variables, resolve symbolic references

Start the bootstrap classloader, responsible for loading legal class files under JAVA_HOME/lib

Extension classloader, responsible for loading legal class files under JAVA_HOME/lib/ext

Application classloader, responsible for loading legal class files under the user path classspath

custom classloader user defined classloader, class MyClassLoader extends ClassLoader

JVM loads classes through the parent delegation model, first handing them over to their lowest parent class to load, and then trying to load them when the parent class cannot be loaded. if(parent != null) parent.loadclass(name) recursion, application classloader -> extension classloader -> bootstrap classloader

First see if the user defined classloader is cached. If it is cached, it will return directly. If not, delegate the parent classloader to load. If the parent cache is cached, it will return directly. Otherwise, delegate it to the parent classloader. Until the last bootstrap classloader, if its cache cannot be found, it will be found under its path. If it is found, it will return to the second level. Otherwise, it will return to its subclass and let its subclass find it under its path. Finally, return to the custom classloader. If it cannot be found, throw an exception.

Execution engine: Executes method instructions in classloader

Interpreter: reads source code or bytecode and executes it directly one by one (javac is outside the JVM)

Just-In-Time compiler/JIT: reads source code, more often bytecode, then compiles it to machine code on the fly and executes it

Java virtual machine is a hypothetical computer that can run Java code, java source files (.java) are generated by java compiler javac byte code files (.class), byte code files (.class) are translated into machine code on a specific machine by an interpreter in JVM

i.e. source code/source code/.java -> bytecode/.class -> machine code/native code/Native Code

Runtime data areas: JVM runtime needs to carve out a memory area from the whole computer memory to store what jvm needs

Heap: shared within JVM/shared between threads, saves all class instances/object instances themselves, does not store basic data type objects and custom object references (these exist in JVM Stack of each thread), is the main recycling area of gc

Method Area/Permanent Generation: shared within JVM/shared between threads, saving information of each class (class name, field information, method information) class, static variables, etc.

Program Counter Register/PC Register: shared within threads, holding the address of the VM bytecode instruction being executed by each thread; if the method is Native, the counter is set to null Undefined

JVM Stack/Thread Stack: shared within threads, holding references to objects of primitive data types and custom objects, context of execution environment

Native Method Stacks: shared within threads, very similar to JVM Stack, except that VM Stack executes Java methods (i.e. bytecode) services for VMs, while Native Method Stacks serve Native methods executed by VMs.

Garbage Collector Start Command line New Generation gc mode Old gc mode-XX:+UseSerialGCserial serial collector serialOld serial collector-XX:+UseParNewGCparNew parallel collector default-XX:+ UseParallelOldGCdefaultparallel collector-XX: +UseConcMarkSweepGCdefaultCMS parallel collector-XX:+UseG1GCG1 collector G1 collector

According to the above table, the gc mode of the new generation and the old generation are matched together to serve the garbage collector.

What about JVM architecture and GC commands to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

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