preface

JVM tuning is a required course for every advanced programmer, and in this chapter, I’ll take a look at the ARCHITECTURE of the JVM in terms of its evolution and core values. In order to give you a better understanding of how the JVM works, I will take a closer look at how the JVM works by going through the loading process of a class to its eventual storage in the runtime data area. Finally, the topic of performance tuning for the JVM is introduced through the introduction of memory collection mechanisms and garbage collection algorithms. This section focuses on garbage collection algorithms and the differences and usage scenarios of common garbage collectors.

JVM memory region partitioning

Program counter (thread private)

Program Counter Register (Program Counter Register), also known as PC Register. Are stored program of instructions executed when the former address (or save the next instruction in the address of the storage unit), when the CPU need of instruction, need from the program counter current needs to execute instructions where the address of the storage unit, then to get to the address of the access to the command, after get the instruction, The program counter is automatically incremented by one or the address of the next instruction based on the transfer pointer, and so on until all instructions have been executed. That is, to indicate which instructions to follow.

Java stack

The Java stack holds stack frames, each of which corresponds to a method being called. The stack frame contains a local variable table, a stack of operands, a reference to the run-time constant pool of the class to which the current method belongs, a method return address, and additional information. When a thread executes a method, it creates a corresponding stack frame and pushes the stack frame. When the method completes execution, the stack frame is pushed off the stack. Therefore, the stack frame corresponding to the method currently executed by the thread must be at the top of the Java stack.

Local method stack

The native method stack works and works very similar to the Java stack. The difference is simply that the Java stack serves the execution of Java methods, while the Native Method stack serves the execution of Native methods. The specific implementation methods and data structures developed here are not mandated in the JVM specification, and virtual machines are free to implement them. The HotSopt virtual machine directly combines the native method stack with the Java stack.

The heap

The heap in Java is used to store objects themselves and arrays (of course, array references are stored in the Java stack), the heap is shared by all threads, and there is only one heap in the JVM. All object instances and arrays are allocated memory on the heap, but with the development of JIT, allocation on the stack, scalar replacement optimization techniques, allocation on the heap becomes less absolute, and escape analysis can only be enabled in Server mode.

Methods area

In the method area, the information of each class (including the name of the class, method information, field information), static variables, constants and the compiled code of the compiler are stored. In addition to the description of a Class’s fields, methods, interfaces, and so on, another piece of information in the Class file is the constant pool, which stores literal and symbolic references generated during compilation.

Direct memory

NIO, a class that uses native libraries to directly allocate out-of-heap memory and access system physical memory without passing through JVM memory — DirectBuffer. The DirectBuffer class inherits from ByteBuffer, but unlike normal ByteBuffer, which still allocates memory on the JVM heap, its maximum memory is limited by the maximum heap memory; DirectBuffer is allocated directly in physical memory and does not occupy heap space. The maximum amount of memory that can be allocated is limited by the operating system

JVM Execution Subsystem

Class Class file structure

Bytecodes, the program storage format used by VMS on different platforms and all platforms, are the cornerstone of platform – and language-independence. The Java virtual machine does not bind to any language, including Java, but is only associated with a specific binary file format called a Class file, which contains the Java Virtual machine instruction set and symbol table, along with several other auxiliary information.

The foundation of Java cross-platform

Bytecodes, the program storage format used by VMS on different platforms and all platforms, are the cornerstone of platform – and language-independence. The Java virtual machine does not bind to any language, including Java, but is only associated with a specific binary file format called a Class file, which contains the Java Virtual machine instruction set and symbol table, along with several other auxiliary information.

The nature of the Class

Any Class file corresponds to a unique Class or interface definition information, but conversely, a Class file does not actually exist as a disk file. A Class file is a set of binary streams in 8-bit byte base units.

Bytecode instruction

Java virtual machine instructions consist of a byte number representing the meaning of a particular operation (called Opcode) followed by zero or more parameters representing the operation (called Operands). By limiting the length of Java virtual machine opcodes to one byte (that is, 0 to 255), this means that the total number of opcodes in the instruction set cannot exceed 256. Most instructions contain information about the data type to which they operate. For example, the ILoAD directive loads int data from a local variable table into the operand stack, while the FLOad directive loads float data. Most directives do not support the integer types byte, char, and short, or even Boolean. Most operations on Boolean, byte, short, and CHAR data actually use the corresponding int type as the operation type

Class loading mechanism

The entire life cycle of a class from when it is loaded into virtual machine memory to when it is unloaded from memory includes: 2, Loading, Verification, Preparation, Resolution, Initialization, Using and Unloading are two stages. The validation, preparation, and parsing parts are called Linking during initialization. The VIRTUAL machine specification specifies that classes must be “initialized” immediately in five cases (loading, validation, and preparation naturally need to start before this)

Garbage collector and memory allocation policy

Is value passing or reference passing in Java?

On the run stack, the base type and reference are treated the same, passing values, so a reference-passing method call can also be interpreted as a “reference-passing value” call, i.e. the reference is treated exactly the same as the base type. But when entering the called method, the value of the reference passed by the program is interpreted (or looked up) into an object in the heap, which corresponds to the real object. If you modify at this point, you are modifying the reference object, not the reference itself, that is, you are modifying the data in the heap. So this change can be maintained! Objects, in a sense, are made up of primitive types. You can think of an object as a tree, and if the object’s properties are still objects, it is still a tree (i.e., non-leaf nodes), and the base type is the leaf nodes of the tree. When a program parameter is passed, the value itself cannot be modified, but if the value is a non-leaf node (that is, an object reference), everything below that node can be modified.

Reference types

Object reference types are divided into strong reference, soft reference, weak reference and virtual reference. Strong reference: The object is generally declared to be a reference generated by the VM. In a strong reference environment, the current object must be checked strictly during garbage collection. If the object is strongly referenced, it will not be garbage collected

Soft references

Soft references are usually used as a cache. The difference between a soft reference and a strong reference is that during garbage collection, the VM determines whether to reclaim a soft reference based on the available memory in the system. If the remaining memory is insufficient, the VM reclaims the space referenced by the soft reference. If the remaining memory is relatively rich, it will not be reclaimed. In other words, the virtual machine must have no soft references when OutOfMemory occurs.

A weak reference

Weak references, like soft references, are used as a cache. However, unlike soft references, weak references are always recycled during garbage collection, so their lifetime only lasts for one garbage collection cycle. Strong references Needless to say, our system is usually used with strong references. Soft references and weak references are rare. They are usually used as caches, and they are usually used as caches when memory size is limited. Because if the memory is large enough, you can use strong references as a cache directly, with much more control. As such, they are commonly used as caches in desktop applications.

Basic garbage collection algorithm

Reference Counting:

Older collection algorithms. The principle is that the object has a reference, that is, one count is increased, and one count is decreased by removing a reference. In garbage collection, only objects with a count of 0 are collected. The most deadly aspect of this algorithm is its inability to handle circular references.

Accessibility analysis cleanup

Mark-sweep:

The algorithm is implemented in two stages. The first phase marks all referenced objects starting from the root node, and the second phase walks through the heap to clean up unmarked objects. This algorithm requires the suspension of the entire application and generates memory fragmentation

Copying:

This algorithm divides the memory space into two equal regions and uses only one of them at a time. Garbage collection iterates through the current area of use, copying the object in use into another area. The second algorithm only deals with the objects that are in use each time, so the replication cost is relatively small. Meanwhile, after the replication, the corresponding memory can be arranged, and there will be no “fragmentation” problem. Of course, the disadvantage of this algorithm is also obvious, that is, it requires twice the memory space.

Mark-compact:

This algorithm combines the advantages of “mark-erase” and “copy” algorithms. The first stage marks all referenced objects starting from the root node, and the second stage traverses the heap, clearing the mark pairs, unmarked objects and “compressing” the surviving objects into one of the blocks of the heap, in order. This algorithm avoids the fragmentation problem of “mark – clean” and the space problem of “copy” algorithm.

Performance optimization

A Web application is not an isolated entity, it is a part of a system, each part of the system will affect the performance of the whole system

Common performance evaluation/test metrics

The response time

The time used between submitting a request and the response that returns it is generally concerned with the average response time. A list of response times for common operations:

concurrency

The number of requests actually interacting with the server at any one time. Association with the number of online users on the website: with 1000 concurrent online users, it can be estimated that the number of concurrent users is between 5% and 15%, that is, the number of concurrent users is between 50 and 150.

throughput

A measure of the amount of work (requests) completed per unit of time

Relationship between

The relationship between system throughput and system concurrency and response time is understood as the traffic condition of the expressway: throughput is the number of vehicles passing the toll station every day (which can be converted into the high speed fee charged by the toll station), concurrency is the number of vehicles running on the expressway, and response time is the speed. The car travels fast when there is little traffic. But receive the high-speed fee correspondingly less; With the increase in the number of cars on the highway, the speed is slightly affected, but the high-speed fee received increases very quickly; As the vehicle continues to increase, the speed becomes slower and slower, the highway is more and more blocked, the toll is not increased but decreased; If traffic continues to increase, beyond a certain limit, mission contingencies will cause the highway to break down completely, leaving cars unable to move and, of course, unable to recover, and the highway to become a parking lot (resource exhaustion).

Common performance optimization methods

Avoid premature optimization

You shouldn’t spend a lot of time on small performance improvements, and thinking about optimization too early is the mother of all nightmares. Therefore, we should write code that is clear, straightforward, easy to read, and easy to understand, and the real optimization should be left for later, when performance analysis shows that the optimization measures have significant benefits.

Perform system performance tests

All performance tuning should be based on performance testing. Intuition is important, but use data, guess, but test.

Look for system bottlenecks, divide and conquer, and gradually optimize

After the performance test, analyze each link of the entire request experience, identify performance bottlenecks, locate problems, and analyze the main factors affecting performance. Memory, disk IO, network, CPU, or code issues? Inadequate architecture? Or is the system really underresourced?

summary

Due to the length of the article, more details have been written, I all summarized in the following [JVM and performance tuning knowledge points] inside, if you need to pay attention to my public numberFuture is lightFree to receive

The last

Welcome to pay attention to the public number: future Youlight, receive this [JVM and performance tuning knowledge points] + a large factory Java interview summary + all knowledge points learning thinking guide + a 300 page PDF document Java core knowledge points summary!