The JVM is introduced

1. JVM architecture (memory model)

The green ones are thread private and the orange ones are thread common

2. Class Loaders

It is responsible for loading the.class file into memory and converting the data structures in the file into the data structures in the method area, generating a Class object

2.1 Class loader classification

  • Start the class loader from here. The Bootstrap ClassLoader ClassLoader. Responsible for loading packages that come with the JDK.

    • %JAVA_HOME%/lib/rt.jar%The JDK source
    • useC++write
    • Class loaders appear that directly get the classes loaded by that loader in the programnull
  • Extension ClassLoader. The package responsible for loading the JDK extension

    • Easy for future expansion
    • %JAVA_HOME/lib/ext/*.jar%
  • Application class loader or system class loader. AppClassLoader or SystemClassLOader

    • Loaders used to load custom classes
    • CLASSPATHUnder the path
  • Custom classloaders

    • By implementingClassLoaderAbstract class implementation

2.2 Parental delegation mechanism

When an application class loader receives a class load request, it does not immediately process the class load request. Instead, it delegates the request to its parent loader to load, and if the parent loader is unable to process the class load request, it passes it to the child loader. Step by step, pass guidance to class loaders that can load the class.

This mechanism is also known as the sandbox security mechanism. Prevent developers from corrupting JDK loading

2.3 Break the parental delegation mechanism

  • Custom class loaders, overriddenloadClassmethods
  • Use the thread-context classloader

2.4 Java virtual machine entry file

Sun. Misc. The Launcher class

3. Execution Engine

The execution engine is responsible for executing the interpretation command and handing it to the operating system for specific execution

4. Local interface

4.1 native method

A native method is an operation that cannot be handled at the Java level. It can only call a local library (C library) through a local interface.

4.2 Native Interface

A set of interfaces for calling function libraries

5. Local method stack

When loading native methods, the executed C function library methods will be executed in this stack area

6. Program counter

Each thread has a program counter, whose main purpose is to store code instructions, similar to an execution plan.

Multiple Pointers are maintained internally that point to the method bytecode in the method area. The execution engine gets the next instruction to execute from the program counter.

Because of the small space, it is a line number indicator/for the current thread executing code

Does not cause OOM

7. Method of area

An area of runtime memory shared by threads that holds structural information about each Class (a Class object), including fields, methods, constructors, and run-time constant pools.

Although the JVM specification describes the method area as a logical part of the Heap, it is also known by the alias non-heap, which is intended to keep it separate from the Heap

The main ones are: permanent generation or meta space. There is the GC

Due to the direct use of physical memory in the meta space, the default maximum meta space size is 1/4 of the physical memory size

8. The Java stack

It is mainly responsible for executing various methods. It is thread private and dies with the death of the thread. There is no garbage collection problem. Each of the eight data types and instance references allocates memory in the function’s stack memory.

The default size is between 512 and 1024K, modified with the -xss1024K parameter

8.1 Stack and queue data structures

Stack Filo: First in, then out

Queue FIFO: First in, first out

8.2 Stored data

  • The local variableLocal Variable. Contains the method’s parameters and return value
  • Stack,Operand Stack. Includes various push and push operations
  • The stack frame dataFrame Data. It’s just like one method at a time. In stack space, methods are called stack frames

8.3 Execution process

The unit of execution on the stack is the stack frame, and the stack frame is the method.

  • First of all tomainMethod, becomes a stack frame
  • The other method is then called, which pushes the stack again
  • Stack frames store the method’s local variables, operand stacks, dynamic links, method exits, and so on
  • The size of the stack depends on the implementation of the JVM and is usually in256K~756K

9. Method area, stack, heap relationship

10. Heap Heap

10.1 Heap memory structure

The default initial size is 1/64 of physical memory, and the default maximum size is 1/4. In actual production, these two values are usually set to the same to avoid the need for space expansion calculation after garbage collection by the garbage collector, which wastes resources.

Out-of-heap memory: Memory objects are allocated outside of the Java virtual machine’s heap, which is managed directly by the operating system (not the virtual machine). The result is to reduce the impact of garbage collection on the application to some extent. Use the undeclared Unsafe and the byteBuffer under the NIO package to create out-of-heap memory.

The default out-of-heap memory size is the size of the execution out-of-heap memory by -XX:MaxDirectMemorySize=

10.1.1 JDK1.7

Logically divided into three areas:

  • Young Generation Space.

    • Eden areaEden Space
    • Survival areaSurvivor 0 Space
    • Survival areaSurvivor 1 Space
  • Pension areaTenure Generation Space
  • Permanent areaPermanent Space(Method area)

On the physical level, it is divided into two areas:

  • New area
  • Old district
10.1.1.1 Heap memory GC procedure

The main process has three steps:

  • whenEdenWhen full, start a light GC(Minor GC), there is no object of death, age+ 1, deposit tofromarea
  • whenEdenTrigger again when full againGC, objects that are not dead are placed intoArea, and then thefromObjects in the region that are not dead are all placedtoRegion, age+ 1. After that, each GC will start againfromandtoThe exchange ofThe region that is empty is the region that is emptyto
  • When the survivor region is full, GC is triggered again, and when the age of the existing object is equal to 15, it is moved into the elderly region

    • MaxTenuringThresholdUse this parameter to set the age at which to move in
  • Trigger once when the old age area is fullFull GCIf the old age zone can no longer store the object, report it directlyOOM

Note: Each GC adds +1 to the age of the surviving object

10.1.2 JDK1.8

Compared to 1.7, just replace the permanent generation to the meta space. Metadata are stored in physical memory, not in the JVM.

In this way, the size of the meta space is no longer affected by the size of the virtual machine memory, but is controlled by the space currently available on the system.

The size ratio of the newborn area and the old area is 1:2, and the proportion of the new and old age is set by -XX:NewRatio=n, where n represents the proportion of the old area.

The ratio between Eden Space and Survivor Space defaults to 8:1, and the ratio between Eden and Survivor Space is set with -XX:SurvivorRatio

Logic level stratification:

  • Young Generation Space

    • Eden areaEden Space
    • Survival areaSurvivor 0 Space
    • Survival areaSurvivor 1 Space
  • Old districtTenure Generation Space
  • Meta space (method area)

Physical Layering:

  • He occupies a third of the freshman section
  • In the old age area he takes up two thirds of the pile

10.2 Heap parameter tuning

10.2.1 Common Heap Parameters
parameter role
-Xms Sets the initial heap size, which defaults to 1/64 of physical memory
-Xmx Sets the maximum heap size. Default is 1/4 of physical memory
-XX:+PrintGCDetails Output verbose GC logs

Simulation OOM

// Set the maximum heap size to 10M // -xms10M -xmx10M -xx :+PrintGCDetails

Now let’s take a look at what the GC process does. What about the GC log

Name :GC Before Occupy ->GC After Occupy (Total Occupy)

// "Allocation Failure" 1585k-> 504K(2560K)] 1585k-> 664K(9728K), 0.0009663 secs] //[New generation, former occupy-> thread occupy-> (total idle)] heap usage size -> heap current size (total occupy->) [Times: User =0.00 sys=0.00, real=0.00 secs] [Full GC (Allocation Failure) [PSYoungGen: 0k->k (2560K)] [ParOldGen: 590K->573K(7168K)] 590K->573K(9728K), [Metaspace: 3115K->3115K(1056768K)], 0.0049775secs] [Times: User sys = = 0.00 0.00, real = 0.01 secs]

Garbage collection algorithm

11.1 Garbage collection types

  • Ordinary GC (minor GC) It happens very frequently in the new district
  • Global GCmajor GCGarbage collection that occurs in the old days occurs oncemajor GCIt is often accompanied at least onceMinor GC

11.2 Garbage collection algorithm classification

11.2.1 Reference counting

Main idea: Each existing object reference is added to the object by one. When the reference to the object reaches zero, garbage collection is triggered. Generally not used

Disadvantages:

  • It is wasteful to add a counter every time a new object is created
  • Circular references are harder to handle
11.2.2 Replication Algorithm

Main idea: Make a direct copy of the object and place it in another area

Pros: No memory fragmentation

Disadvantage: take up space to compare big

Usage scenario: The replication of newborn areas is performed by the replication algorithm. After a Minor collection, a copy of the surviving object is placed in the to section

11.2.3 Tag scavenging algorithm

The main idea: Go through all references from the root of the reference, mark all objects that need to be cleaned up, and then clean them up. Two steps to complete

Disadvantages: Garbage collection interrupts the entire code. Memory fragmentation occurs

11.2.4 Tag collation algorithm

Main idea: As with the tag purge algorithm, the last addition is a step defragment that will defragment memory. Three steps to complete

Cons: Low efficiency, need to move objects.

11.3 Comparison of major garbage collection algorithms

11.3.1 Memory efficiency

Replication algorithm > mark elimination method > mark collation method

11.3.2 Memory uniformity

Replication algorithm = mark collation method > mark clearance method

11.3.3 Memory utilization

Mark sorting method = mark clearing method > copy algorithm

11.3.4 Optimal algorithm

Through the use of different algorithms in the scene, to achieve the optimal purpose

Young generation: Due to the long life period of the object, the object mortality is high, so the replication algorithm is generally used

Old age: the area is large, the survival rate is high, and the hybrid algorithm of mark clearance and mark collation is generally adopted.

The old age is generally implemented by tag purging or a mixture of tag purging and tag collating. Taking the CMS collector in HotSpot as an example, CMS is implemented based on mark-sweep, which has a high recovery efficiency for the object. For the fragmentation problem, CMS uses Serial Old collector based on mark-compact algorithm as a compensation measure: When memory collection is poor (when Concurrent Mode Failure is caused by fragmentation), Serial Old is used to perform Full GC to achieve collation of Old memory.

11.3.5 GCRoots

When we mentioned the tag removal algorithm above, we mentioned a noun, root node reference. So what is a root node reference?

The root node reference, also known as GCroots, refers to the root node where the garbage collection algorithm traverses the object. That is, you start with this object and iterate down, marking the objects that need to be recycled.

The process for garbage collection tags is to start the search down with a GCroots object, and if an object does not have any reference chain to GCroots, it is not available.

It is traversed from GCroots. The traversed is not garbage, and the untraversed is garbage, and it is judged dead

11.3.5.1 Reachable and Unreachable Objects

The accessibility object is a GCROOT reference at the top of the object’s link reference

An unreachable object is one that is not a GCROOT reference at the top level of the object’s link reference

Commonly: Reachable objects are objects that have a home, and that home has a term called GCROOT. Unreachable objects are objects that don’t have a home.

11.3.5.2 What references can be GCroots
  • Local variable references in the stack
  • Static attribute references in the meta space
  • Constant references in the meta space
  • Local method stacknativeMethod of modification

In plain English, all references that are exposed to the developer

Garbage collector

Garbage collector is implemented based on GC algorithm.

There are four main types of garbage collectors, but there are seven specific ways to use them

12.1 Four types of garbage collectors

12.1.1 Serial Garbage Collector (Serial)

A single thread is garbage collected while all other threads are paused

Through – XX: + UseSerialGC

12.1.2 Parallel garbage collector (Parallel)

Multiple threads are garbage collected, while all other threads are paused

12.1.3 Concurrent Garbage Collector (CMS)

The GC thread and the user thread run simultaneously

12.1.4 G1 Garbage Collector

Partitioned garbage collection. Physically, there is no distinction between newborn area and endowment area, and the heap memory is divided into 1024 small regions, each of which takes up a Space of 2-32m. Each region may be Eden Space, Survivor01 Space, Survivor02 Space and Old region.

Tag collation algorithm is used as a whole and copy algorithm is used as a part. The object after GC is migrated from one region to another by the replication algorithm, which causes the problem of memory fragmentation. The birth of memory fragmentation is avoided by the overall tag collation algorithm

During garbage collection, a region is directly collected, and the saved objects are copied TO the “TO” or “Old” region through the replication algorithm.

Logically, the heap has four extents, each of which has a variable size and can be allocated as needed. It is divided into Eden Space, Survivor01 Space, Old and Humongous. Humongous is used to store large objects, usually contiguous. When the contiguous region is insufficient, Full GC will be triggered to clear the surrounding region to store large objects

G1 heap memory schematic

G1 Garbage collection

FullGC with three regions cannot store large objects

Execute the process

  • Initial tag.The GC threads, the tagGCRoots
  • Concurrent flags.The user thread and the GC thread run simultaneously. The GC thread traversesGCRootsAll objects of the
  • Relabeling. Fix objects that have been tagged by concurrent tags and need to be untagged because the user program calls them again. The GC thread
  • Filter the collection. Cleanup marked objects. The GC thread
  • The user thread continues to run

12.1.4.1 case
  • Initial tag. G1 is thrown by a large object

  • Concurrent tags

  • Relagging, filtering cleanup, and large object causedFull GC

12.1.4.2 G1 common parameters
-XX:+UseG1GC Enables GC-XX :G1HeapRegionSize=n: Sets the size of G1 region. The value is a power of 2 and ranges from 1M to 32M. The goal is to partition approximately 2048 regions based on the minimum Java heap size -XX: maxGCPauseMillis =n: The maximum pause time, this is a soft target, the JVM will as far as possible (but not guarantee) pause time is less than the time - XX: InitiatingHeapOccupancyPercent = n the number of triggered when the GC heap usage, -XX:G1ReservePercent=n Sets the percentage of memory reserved as free space to reduce the risk of target space overflow. The default value is 10%

12.2 Common parameters

Defnew Default New Generation // Tenured Old // Parallel New Generation // Parallel New Generation // Defnew Default New Generation // Tenured Old Psyonggen Parallel Scavenge Psyonggen Parallel Scavenge Psyonggen Parallel Scavenge Psyonggen Parallel Scavenge Paroldgen Parallel Old Generation

12.3 A new generation of garbage collectors

The above diagram shows all the types of garbage collector available to the newborn and senior areas. Let’s go through them one by one

Serial GC(Serial/Serial Coping)

The new generation uses the Serial Coping garbage collector using the replication algorithm

By default, the elderly zone uses Serial Old garbage collector, using the tag removal algorithm and tag collation algorithm

Set it to -XX:+UseSerialGC

12.3.2 concurrent GC (ParNew)

The newborn uses the Parnew garbage collector, using the replication algorithm

The elderly zone uses Serial Old garbage collector (which is not recommended), using the tag cleanup algorithm and the tag collation algorithm

Start with -XX:+ UseParnewGC

12.3.3 Parallel Scavenge (Parallel/Parallel Scavenge)

The new generation uses parallel garbage collection

The old days used parallel garbage collection. The garbage collector used by default in Java1.8

One question: What is the difference between Parallel and Parallel Scavenge collectors?

Parallel Scavenge is a new generation garbage collector similar to Parnew, which uses a replication algorithm. It is also a Parallel, multithreaded garbage collector, commonly known as a throughputs first collector.

Parallel Scavenge is an adaptive collector that collects performance monitoring information based on the current state of the system and dynamically adjusts these parameters to provide the most appropriate Teton time or maximum throughput

His focus is:

Controlled throughput. Throughput = time to run user code /(time to run user code + garbage collection time),

Also, when the new generation selects Parallel Scavenge, it will enable Parallel garbage collection for the old by default

-XX:UseParallelGC or -XX: UseParalleloldGC will activate each other

-XX:ParallelGCThreads=nIndicates how many GC threads are started

CPU >8 N=5 or 8

N= actual number when CPU <8

12.4 Old Garbage Collector

12.4.1 Serial Garbage Collector (Serial Old/Serial MSC)

Serial Old is an older version of Serial garbage collector. It is a single-threaded collector that uses a tag collation algorithm that runs on the Client’s Old generation garbage collection algorithm

Associated with the new generation of Serial GC

12.4.2 Parallel Recall (Parallel Old/Parallel MSC)

Parallel Old/ is implemented using the tag collation algorithm

Associated with the Cenozoic Parallel Scavenge GC

12.4.3 Concurrent Mark Clears GC

CMS collector (Concurrent Mark Swep) : A collector that aims to obtain the shortest collection pause time

Suitable for application in the Internet station or B/S system server, pay attention to the server’s response speed

CMS is ideal for server-side applications with large heap memory and CPU cores, and was the preferred collector for large applications before G1

When marked, the GC thread runs; Cleanup runs with the user thread

Open with the -XX:+UseConcMarkSweepGC directive

Use with the Pallellal New GC collector in the New area

It is used when the CMS is unusable due to CPU pressureSerialGCAs a standby collector

12.4.3.1 CMS execution process
  • Initial tag (CMS initial mark). I go through and find all of themGCRoots.GCThe thread executes and the user thread pauses
  • Concurrent markup (CMS concurrent mark) and the user threadGCRoots, marks the object that needs to be cleared
  • Relabeling (CMS remark). During the correction mark, objects that do not need to be reclaimed because the user program continues to run are corrected
  • Concurrent purge (CMS concurrent sweepClear all tagged objects together with the user thread

Advantages and disadvantages 12.4.3.2

Advantages:

  • Concurrent collection pauses low

Disadvantages:

  • Concurrent execution, large pressure on CPU resources
  • The use of the tag – cleaning algorithm results in a large amount of memory fragmentation

Summary of Garbage Collector

Parameters (XX: +…) The new generation of garbage collectors New generation algorithm The old garbage collector Old age algorithm
UseSerialGC SerialGC Replication algorithm Serial Old GC The whole
UseParNewGC Parallel New GC Replication algorithm Serial Old GC The whole
UseParllelGC Parallel Scavenge GC Replication algorithm Parallel GC The whole
UseConcMarkSweepGC Parallel New GC Replication algorithm CMS and Serial Old GC Standard definition
UseG1GC Overall the whole Local copy

Garbage collection algorithm general logic

12.6 Differences between CMS and G1

  • The G1 does not cause memory fragmentation
  • G1 accurate control of memory, you can accurately to collect garbage. Collect the region with the most garbage according to the GC processing time set

13. JMM

The Java Memory Model. It’s a norm.

When a thread operates on a variable, it first copies a copy from physical memory into its working memory (stack memory) and then writes it to physical memory after updating it

Features:

  • atomic
  • visibility
  • order

For more original articles and tutorials, please follow the author’s official account of the same name @MakerStack