JVM JRE JDK relationship

  • The JVM (Java Virtual machine) recognizes the bytecode instructions in the.class file and calls the operating system’s API to complete the action. The JVM can run not only Java programs, but any file that can be compiled to a. Class.

  • The JRE (Java runtime environment), which contains the JVM and core lib.

  • JDK (Java development Kit), which integrates the JRE with some tools. Such as javac.exe, java.exe, jar.exe, etc. As you all know, in order to execute Java programs, you need to install the JDK.

The JVM first

The JVM is really a specification that provides a runtime environment that can execute Java bytecode. Different vendors provide different implementations of this specification. Common JVM implementations are

  • Hotspot Oracle official
  • TaobaoVM Ali deep custom version of Hotspot
  • J9 IBM implementation
  • Jrockit claims to be the world’s fastest JVM
  • openJDK
  • azul zing
  • LiquidVm targets hardware directly
  • Microsoft JVM
  • Etc.

The memory model of the JVM

When executing files, virtual machines divide memory into different areas that do their job.

  • Program counter
  • Java virtual machine stack
  • The heap
  • Methods area
  • Local method stack

Understanding the Java Virtual Machine

Program counter/line number indicator

Can be thought of as a line number indicator of the bytecode being executed by the current thread. The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to be executed. Branch, loop, jump, exception handling, thread recovery and other basic functions need to be completed by this counter. Multithreading in the Java virtual machine is implemented by alternating threads and allocating processor execution time, so that at any given time, only one processor executes instructions in one thread. Therefore, in order to restore the correct execution position after the thread switch, each thread needs to have an independent program counter, the counters between the threads do not affect each other, independent storage. So this type of memory area is "thread private" memory. -- Understanding the Java Virtual MachineCopy the code

It is a small space and the only one that does not define any OOM in the Java Virtual Machine specification. If the Java method is being executed, the counter records the address of the virtual machine bytecode instruction (the address of the current instruction). Null if it is the Natice method.

Java virtual machine stack

Also thread-private and with the same lifecycle as a thread, it describes an in-memory model of the execution of Java methods. Each method creates a stack as it executes, and each method called corresponds to a stack frame that moves from the virtual machine stack to the virtual machine stack. It stores information about local variable tables, operand stacks, method exits, and so on. The size of the local variable table is completed during editing, so the stack size is fixed when entering the execution method.

StackOverflowError is raised if the thread requests a stack depth greater than the virtual machine allows.

An OutOfMemoryError is raised when sufficient memory cannot be allocated when the virtual stack can be dynamically extended.

Refer to the Java Virtual Machine run-time stack frame structure for more information

The ** Native method stack ** is similar to the Java virtual machine stack, except that it represents Native methods.

The heap

This is the largest chunk of memory in the Java virtual machine, an area of memory that is shared by all threads and is created when the virtual machine is started. Almost all of the memory for object instances is here, which is why it exists. The Java heap can also be subdivided into new generation and old generation. The Cenozoic era can be divided into Eden, from Servivor, to Servivor.

According to the virtual machine specification, the Java heap can have physically discontinuous memory space, just like disk space as long as the logic is continuous. Its memory size can be set to a fixed size or expanded. -xmx and -xms can be set. If there is no memory in the heap to complete the instance allocation and the heap cannot be expanded, an OOM error will be reported.Copy the code

** Method area **

This is also a shared area. It stores information about classes that have been loaded by the virtual machine, constants, static variables, code edited by the editor, and so on. In older JDKS, the method area was also called persistent generation. “The HotSpot VIRTUAL machine implements the method area with persistent generation.” Jdk8 really starts to scrap permanent generations and use Metaspace instead.

** The above distinction is of course the JVM specification, and each virtual machine implementation may have different distinctions. Sometimes, we can roughly divide areas into heap and stack areas. These are the two areas that programmers care most about. **


Java Memory Model (JMM)

  • The JMM role
The Java Memory Model (JMM) is defined in the Java Virtual Machine specification, which is used to screen out the differences of Memory access between different hardware and operating systems, so that Java programs can achieve the same concurrency effect on all platforms. The JMM regulates how the Java virtual machine and computer memory work together: it specifies how and when one thread can see the value of a shared variable that has been modified by another thread, and how shared variables can be accessed synchronously if necessary.Copy the code

Refer to the Java Memory Model (JMM) summary for details

** JVM GC **

How do I find garbage

It mainly includes reference counting and root reachability analysis.

  1. Reference counting

Add a reference counter to an object that is +1 each time it is referenced, -1 when it is invalidated, and 0 when it is garbage collected.

Advantages: simple implementation, high judgment efficiency.

Disadvantages: It is difficult to solve the problem of circular references between objects. Because of this shortcoming, mainstream JVMS do not use this counting method.

  1. Root searching

Start with the GC Roots object and go down and search for all the leaves. Those leaves without roots are garbage. GC Roots objects include:

  • Object referenced in the virtual machine stack
  • Objects referenced by static properties and constants in the method area
  • Objects referenced in the local method stack

How to clean up garbage? The algorithm for garbage collection

  1. Mark-sweep

As its name suggests, it is divided into two phases, marked first and then cleared. This is also the most basic algorithm on which all other algorithms are optimized.

Advantages:

1. High efficiency when there are many living objects (old generation). 2. Simple algorithm.

Disadvantages:

  1. Scanning twice is inefficient.

Mark what works the first time and clear what doesn’t the second time.

  1. Prone to fragmentation.
  1. Copy (Copying)

In order to improve efficiency, the replication algorithm appeared. It divides memory into two pieces, uses one piece at a time, and copies the surviving objects to the other. And then I’m going to clear out all the objects in this block of memory.

Advantages:

It is suitable for the situation of relatively few living objects (Cenozoic era), only scan once, improve the efficiency, no debris.

Disadvantages:

1. waste of space;

2. You need to adjust the object reference to move the replication object.

  1. Mark-compact algorithm

If there are many live objects, moving objects becomes inefficient. So as not to waste another chunk of memory. It is difficult to have an extreme case where 100% of the objects survive while the program is running. The algorithm of tag collation is introduced. It’s an extra step of collation on top of the mark-clear algorithm. Garbage objects are marked, and then all surviving objects are moved to a contiguous block of memory, which is then cleared of any unexpected exceptions.

Advantages:

No fragmentation, easy object allocation, no memory halving.

Disadvantages:

Scan twice, moving the object.

What garbage collectors are available

Above introduced the garbage collection algorithm theory, garbage collector is their concrete implementation. There is nothing in the Java Virtual Machine specification about how the garbage collector is implemented. So different vendors and different versions of JVMS provide different garbage collectors. They provide parameters for users to customize their own combination of garbage collectors. Below is the garbage collector for the HotSpot VIRTUAL machine. Source from In-depth Understanding of Java Virtual Machines

3-5 In Figure 3-5, if two VMS are connected, they can be used together. There is no best garbage collector, only the best garbage collector for you. Parallel Avenge + ParallelOld

  • Serial

    Single-threaded execution, using only one CPU or one collection thread area to complete the garbage collection. While it performs garbage collection, it must suspend other worker threads until the collection is complete. It uses a copy algorithm. The memory requirements are tens of megabytes.

    Application scenario: No overhead of thread interaction is required to achieve the most efficient collection efficiency, suitable for the environment with a limited CPU.

    Explicitly use the setting parameter: “-xx :+UseSerialGC”

  • ParNew is a multithreaded version of Serial, which is basically the same as Serial except that it uses multiple threads. Both garbage collectors also share a lot of code.

    Application scenario: In Server mode, the ParNew collector is a very important collector because it is currently the only one besides Serial that works with the CMS collector;

    Set the parameters

    -xx :+UseParNewGC” : specifies ParNew forcibly. -xx :ParallelGCThreads” : specifies the number of garbage collection threads. ParNew enables the same number of garbage collection threads as the CPU by default.

    -xx :+UseConcMarkSweepGC” : After CMS is specified, ParNew will be used as the new generation collector by default.

  • Parallel Scavenge

Is also a new generation collector, using the copy algorithm. While other collectors focus on minimizing the pause time of user threads during garbage collection, their goal is to achieve a manageable throughput.

Application scenario High throughput is the goal, that is, reduce garbage collection time, so that user code can obtain longer running time; When the application runs on multiple cpus and does not have a particularly high pause time requirement, that is, the program mainly performs calculations in the background without much interaction with the user; For example, applications that perform batch processing, order processing, payroll, scientific computing, data mining.

  • Serial Old

Older implementations of Serial, also single-threaded, used a mark-collation algorithm.

  • Parallel Old

Parallel older algorithm, using mark-collation algorithm.

  • CMS

Is a collector whose goal is to get the shortest pause time. Some apps pay special attention to the speed of the service, hoping to have the shortest pause time to give users a better experience. Based on mark-clear algorithm.

There are four stages of recycling:

  1. Initial mark. Find GC Roots, this stage is STW, but STW time is very short.
  2. The concurrent mark is a concurrent mark, which takes the most time and produces no STW. Mark garbage.
  3. Re-mark remark, the garbage that was marked last time may have been modified to become available and needs to be re-marked. There are fewer changes, and this process is also STW.
  4. Concurrent sweep of concurrent sweep

It serves as a link between the preceding and the following.

Disadvantages:

  1. With the mark-clear algorithm, memory fragmentation occurs
  2. High CPU requirements
  3. Unable to handle floating garbage. At the time of garbage collection, the worker thread is also executing and the new garbage cannot be cleaned up.
  • G1

Previous memory partitioning was done in large chunks. The G1 uses the idea of divide and conquer, a radical departure from previous generations. Regions (with the fewest living objects). The G1 doesn’t have a fixed E or O region of memory.

The characteristics of

  • Concurrent tag and recycle
  • Compress the free space, do not extend the GC pause time, can control the GC time
  • More predictable GC pause times
  • High response times and scenarios that do not require high throughput

The ratio of old to new

5%-60% is generally not and should not be manually specified, which is the baseline for G1 to predict pause times.

When does GC trigger

  • YGC Eden has insufficient space. Multithreaded parallel execution
  • Insufficient FGC Old space. System.gc();

* You should do this if G1 produces Fgc

FGC occurs when memory is not allocated.

  • Expand the memory
  • Improve CPU performance
  • Lower the threshold for MixedGC to occur earlier (default 45%)

See Java Virtual Machine Garbage Collection (3) for 7 garbage collectors

** Tricolor tag **

  • White: Unmarked object
  • Grey: self is marked, member variables are not marked
  • Black: self and member variables are marked

tuning

Let’s start with two concepts

  1. Throughput :(user code time/user code time + garbage collection time). Most of the virtual machine time is spent on user time, not garbage collection.
  2. Corresponding time: The shorter the SWT, the better the response time.

The first step in tuning is to determine whether throughput or response time is a priority. Or finding a balance between them. They are contradictory; you can’t have your cake and eat it.

Purpose of tuning

  1. Plan and pre-tune the JVM as required
  2. Optimizing the JVM runtime environment (slow, slow)
  3. JVM runtime problems (OOM)

Tuning starts with planning

  1. Starting with business scenarios, tuning without business scenarios is rogue
  2. No monitoring (pressure test, can see results), no tuning.

Optimization steps

  • Select the collection combination
  • Computational memory requirements
  • Select CPU (the higher the better)
  • Set age and upgrade age
  • Setting Log Parameters

Tune common commands

Summary of common JVM monitoring and tuning command tools

Common JVM commands and performance tuning recommendations

The interview

  • How to troubleshoot the cause of slow JVM interface

The reasons for the slow online interface are as follows: 1. High memory usage and CPU usage due to frequent GC 2. 3. When a deadlock occurs, run the top-c command to check whether the CPU usage is full, and then run the free-m command to check the memory usage. Determine which of the preceding problems may occur, and then troubleshoot the problem in depth. How can I rectify the fault when the online interface request is too slow?

  • Memory structures, those are thread private, those are common

Method area and heap are common; Program counters, virtual machine stacks, and local method stacks are private.

  • Memory model, CPU have this architecture, why JVM implementation again to cross platform

  • Java object structure

  • Do you have any experience with JVM tuning, how to tune the JVM, and how to analyze Dump logs

  • Why avoid FullGC

FullGC scans the entire old region, which is inefficient. So the JVM designed the CardTable. If the O block CardTable points to the Y block. I’m going to make it Dirty. Only Dirty Card tables need to be scanned next time. Structurally, card is implemented with bitmap. Rset records the references of objects in other regions to this Region. The value of the Rset is that the garbage collector does not need to scan the entire heap to find out who references objects in the current partition, just scan the Rset.

  • New generation garbage collection algorithm, will STW (Stop the World)

Will the

  • The relationship between GC and FullGC in old age

The old gc is fullGC

  • Online CPU alarm, cause and troubleshooting method (system CPU is always 100%, how to tune)
  1. Find which process has the highest CPU (top)
  2. The process whose CPU is high (top-hp) can be queried based on the process ID.
  3. Export the stack for this thread (jStack)
  4. Find which method consumes time (jStack)
  5. A high proportion of worker threads or garbage collection threads
  • What triggers fullGC? How many heaps does it trigger?

  • G1 garbage collector introduction, what is the collection algorithm used? Benefits of configuring reclaim Time? And CMS how to choose? Why is that?

G1 has a shorter response time than CMS, but CMS+PN has a higher throughput than G1.

  • Difference between OOM and Stack over Flow
  • Can the production environment dump at any time

Small heaps don’t matter much, big heaps can have service pauses or freezes, and FGC can occur before dump

  • What are common OOM problems

Heap stack methodArea direct memory

  • How are Java objects created
  1. Check whether the object exists on the VM. If not, perform class loading
  2. Allocate memory
  3. Member variable assignment
  4. Execute constructor < init >
  • Class The class loading process
  1. Loading Loads a class file into memory
  2. Verification verifies that the file complies with JVM specifications
  3. Static int I =10; static int I =10; static int I =10; Instead of assigning I to 10, this step assigns the default value 0;
  4. Resolution is used to convert symbolic references used in class file constant pool into direct memory address, accessible content.
  5. Initializing: Static variables are initialized at this point;
  6. gc
  • Layout of objects in memory

object

  1. Mark word object header, including object hashcode, lock, object GC generation age, bias lock.
  2. Klass Pointer Class Pointer
  3. data
  4. The number of bytes used to align an object must be a multiple of 8

Arrays have more array length than objects

  • Object access location

There are currently two ways

1. Objects are accessed through handles. Refernce in the stack stores a reference to the object, pointing to a list in the handle pool that holds the actual address of the Java object. The advantage is that when objects move, you only need to change the address of the handle pool, not the refernCE. 2. Objects are accessed by direct Pointers, and refernCE stores the object’s memory address. Its advantage is fast access.

— Understanding the Java Virtual Machine

  • When will the object be in the old section
  1. The big object

  2. The maximum age, 15 by default, can be set using -xx :MaxTenuringThreshold.

  3. Dynamic allocation, such as G1 s1-> S2 more than 50% put the oldest into Old

reference

“Understanding the Java Virtual Machine in Depth”

“Horse Soldier Class”