[[table of contents] 1.JVM Basic knowledge 2.Bytecode Technology (source code and compilation).class (conversion) 3.JVM Class loader* 4.JVM Memory model* 5.JVM startup parameter
Basic knowledge
Java is an object-oriented, statically typed, compiled and executed high-level language with VM/GC, runtime and cross platform
- The difference between static language and dynamic language
Difference 1: static languages can determine the data type of variables at compile time. Most static languages must declare the data type before using variables
Difference 2: type matching is checked during static language compilation, so variables cannot be assigned different type values; The dynamic type variable type is variable at run time, which means that the object is polymorphic
Difference 3: the static language is closed, which makes the third-party development package less intrusive to the code; Dynamic language conciseness
- Compilation execution and interpretation execution
Compilation execution: similar to "full text translation", Java classes are compiled into class bytecode file, compiled at one place and executed everywhere
Interpretation and execution: similar to "simultaneous interpretation", interpretation and execution line by line
The JVM is combined in two ways
Bytecode Technology
-
What is bytecode
Java Code is composed of single byte instructions and theoretically supports 256 opcodes; in fact, only about 200 opcodes are used, and some opcodes are reserved for debugging operationsAccording to the nature of the directive, the main categories are:
1. Stack operation instruction
2. Program flow control instruction
3. Object operation instructions, including method calls
4. Arithmetic operation and type conversion instructions
compile: javac demo/hello.java View bytecode: javap -c demo.hello View bytecode (more): javap -c -verbose demo.hello
2. Bytecode runtime structure
- The JVM is a stack based calculator
- Each thread has its own JVM Stack for storing stack frames
- With each method call, the JVM automatically creates a stack frame
- The stack frame consists of operands, an array of local variables, and a Class reference
- The Class reference points to the corresponding Class of the current method in the runtime constant pool
One by one mapping between mnemonics and secondary system codes is as follows:
Method call instruction:
invokestatic, as the name suggests, is used to call the static method of a class, which is the fastest method call instruction.
invokespecial is used to call the constructor, but it can also be used to call private methods in the same class and visible superclass methods.
Invokevirtual. If it is a specific type of target object, invokevirtual is used to call public, protected and package level private methods.
Invokeinterface. When a method is called through an interface reference, it will be compiled into an invokeinterface instruction.
invokedynamic, a newly added instruction in JDK7, is an upgrade and improvement to realize the support of "Dynamically Typed Language", and it is also the implementation basis of supporting lambda expressions after JDK8
JVM class loader
Class lifecycle
Class life cycle: load - {link} [verify - prepare - resolve] - initialize - use - uninstall
- Loading: find the Class file
- Verification: verify format and dependency
- Preparation: static field and method table
- Resolution: resolve symbols to references
- Initialization: constructor, static variable
Quantity assignment, static code block - Using
- Unloading
Class loading
Class loading timing
- When the virtual machine starts, initialize the main class specified by the user, that is, the class where the main method is started and executed;
- When a new instruction is encountered to create a new instance of the target class, the target class of the new instruction should be initialized when it is a new class;
- When an instruction calling a static method is encountered, initialize the class where the static method is located;
- When an instruction to access a static field is encountered, initialize the class where the static field is located;
- The initialization of the child class will trigger the initialization of the parent class;
- If an interface defines a default method, the initialization of the class that directly or indirectly implements the interface will trigger the initialization of the interface;
- When a reflection call is made to a class using the reflection API, the class needs to be initialized. In fact, as before, the reflection call either has an instance or is a static method, which needs to be initialized;
- When the MethodHandle instance is called for the first time, the class of the method pointed to by the MethodHandle is initialized
Not initialized (may load)
- Referencing the static field of the parent class through the child class will only trigger the initialization of the parent class, not the initialization of the child class.
- Defining an object array will not trigger the initialization of this class.
- Constants are stored in the constant pool of the calling class during compilation. In essence, there is no direct reference to the class that defines constants, and the class where constants are defined will not be triggered.
- Getting the Class object through the Class name will not trigger Class initialization, hello Cla ss does not initialize the Hello Class.
- Pass class When forname loads the specified class, if the specified parameter initialize is false, class initialization will not be triggered. In fact, this parameter tells the virtual machine whether to initialize the class. Class.forName ("jvm.Hello") loads the Hello class by default.
- Through the default loadClass method of ClassLoader, the initialization action will not be triggered (loaded, but not initialized)
Class loader
Class III loader:
- Start class loader (bootstrap classloader)
- Extended class loader (ExtClassLoader)
- Application class loader (AppClassLoader)
Characteristics of loader
4. Parental entrustment
5. Responsible for dependence
6. Cache loading
Custom class loader implementation
Several ways to add reference classes:
- Put it under lib/ext in JDK, or - DJava ext.dirs
- java - put cp/classpath or class files in the current path
- Custom ClassLoader loading
- Get the ClassLoader of the currently executing class, and call the addUrl method to add a Jar or path (JDK9 is invalid)
JVM memory model
Memory structure
Each thread can only access its own thread stack;
Each thread cannot access (cannot see) the local variables of other threads;
All native type local variables are stored in the process stack, so they are invisible to other threads;
A thread can transfer a copy of the value of a native variable to another thread, but cannot share the native local variable itself;
Heap memory contains all objects created in Java code, no matter which thread created them. Packaging types are also covered
(e.g. Byte, Integer, Long, etc.);
Whether you create an object and assign it to a local variable or assign it to a member variable of another object, the created object will be saved to heap memory;
If it is a local variable of native data type, its contents will be kept on the process stack;
If it is an object reference, the reference address of the object is saved in the local variable slot in the stack, and the actual object content is saved in the heap;
The member variables of the object are stored on the heap together with the object itself, regardless of whether the type of the member variable is a native value or an object reference;
The static variables of the class are stored in the heap like the class definition;
Summary:
- The native data type and object reference address used in the method are stored on the stack, and the object, object member and class definition, static variable
The quantity is on the pile; - Heap memory is also called "shared heap". All objects in the heap can be accessed by all threads as long as they can get the reference address of the object. If a thread can access an object, it can also access the member variables of the object;
- If two threads call the same method of an object at the same time, they can access the member variables of the object, but the local variable copy of each thread is independent;
Overall structure of JVM memory
Stack memory structure
Stack:
1. Every time a thread is started, the JVM will allocate the corresponding thread stack in the stack space, such as 1MB of space (- Xss1m)
2. The thread stack is also called the Java method stack. If the JNI method is used, a separate native stack is allocated
3. In the process of thread execution, there are usually multiple methods to form A Stack Trace. For example, A calls B, B calls C... each time A method is executed, the corresponding stack Frame will be created
Heap memory structure
Heap:
1. Heap memory is the memory space shared by all threads. The JVM divides heap memory into Young generation and Old generation (also known as Tenured);
2. The younger generation is also divided into three memory pools, the new generation (Edenspace) and the survival space. In most GC algorithms, there are two survival areas (S0, S1). At any time we can observe, one of S0 and S1 is empty, but it is generally small and does not waste much space;
3. Non Heap is essentially Heap, but it is generally not managed by GC. It is divided into three memory pools.
Metaspace was formerly called Permanent generation, and Java 8 changed its name to Metaspace
4. CCS, Compressed Class Space, which stores class information, intersects with Metaspace.
5. Code Cache, which stores the local machine code compiled by the JIT compiler
CPU and memory behavior
CPU out of order execution (instruction rearrangement)
volatile keyword
Atomic operation
Memory barrier
summary
- JMM: the Java Memory Model and Thread Specification clearly defines how and when different threads can see the values saved to shared variables by other threads; And how to synchronize access to shared variables when necessary. The advantage of this is to shield the memory access differences between various hardware platforms and operating systems, and realize the authenticity of Java Concurrent Programs
Positive cross platform; - All objects (including internal instance member variables), static variables, and arrays must be stored in heap memory;
- Local variables, formal / input parameters of methods, and input parameters of exception handling statements are not allowed to be shared among threads, so they are not affected by the memory model;
- When multiple threads access a variable at the same time [read / write], as long as a thread performs write operations, this phenomenon is called "conflict";
- Operations that can be affected or perceived by other threads are called inter thread interaction, which can be divided into read, write, synchronous operation, external operation and so on. Synchronous operations include reading and writing volatile variables, locking and unlocking monitor s, starting and ending operations of threads, starting and ending threads, etc. external operations refer to operations outside the environment for threads, such as stopping other threads, etc;
- JMM regulates the interaction between threads, regardless of the operation of local variables within the thread;
JVM startup parameters
Start parameter classification
1. System attribute parameters
-Dfile.encoding=UTF-8 -Duser.timezone=GMT+08 -Dmaven.test.skip=true -Dio.netty.eventLoopThreads=8
2. Operating mode parameters
1. -server: set up JVM use server Mode, which is characterized by slow startup speed, but high runtime performance and memory management efficiency. It is suitable for production environment. On a 64 bit capable JDK This mode is enabled by default in the environment and is ignored -client Parameters. 2. -client : JDK1.7 Previously in 32-bit x86 The default value on the machine is -client Options. set up JVM use client Mode, which is characterized by fast startup speed, but low runtime performance and memory management efficiency. It is usually used for client applications or PC Application development and debugging. Besides, we know JVM After the bytecode is loaded, it can be interpreted and executed, or compiled into local code for execution, so it can be configured JVM Processing mode of bytecode: 3. -Xint: In interpretation mode( interpreted mode)Run under,-Xint Tags force JVM Explain the execution of all bytecodes, which of course reduces the running speed, usually 10 times or more. 4. -Xcomp: -Xcomp Parameters and-Xint On the contrary, JVM When used for the first time, all bytecodes will be compiled into local code, resulting in maximum optimization. [pay attention to preheating] 5. -Xmixed: -Xmixed It is a mixed mode, which mixes interpretation mode and compilation mode. There are JVM Decide for yourself, this is JVM The default mode is also the recommended mode. We use java -version Can see mixed mode Other information
3. Heap memory setting parameters
Out of heap (XMS Xmx) | non heap + out of heap
-Xmx, Specifies the maximum heap memory. as -Xmx4g. It's just a limitation Heap The maximum value of the section is 4 g. This memory does not include stack memory or memory used outside the heap. -Xms, Specifies the initial size of heap memory space. as -Xms4g. Moreover, the specified memory size is not the initial value actually allocated by the operating system, but GC Plan well before you allocate it. Need to keep on dedicated server –Xms and –Xmx Otherwise, there may be several applications as soon as the application starts FullGC. When the two configurations are inconsistent, heap memory expansion may cause performance jitter. -Xmn, Equivalent to -XX:NewSize,use G1 The garbage collector should not set this option, which can be set in some other business scenarios. The official recommendation is set to -Xmx Of 1/2 ~ 1/4. -XX: MaxPermSize=size, This is JDK1.7 Previously used. Java8 Allowed by default Meta The space is infinite, this parameter is invalid. -XX: MaxMetaspaceSize=size, Java8 Default unlimited Meta space, This option is generally not allowed to be set. -XX: MaxDirectMemorySize=size,The maximum out of heap memory that the system can use. This parameter is the same as -sun.nio.MaxDirectMemorySize The effect is the same. -Xss, Sets the number of bytes per thread stack. for example -Xss1m Specifies that the thread stack is 1 MB,And-XX:ThreadStackSize=1m equivalence
4.GC setting parameters
XX: +UseG1GC: use G1 Garbage collector -XX: +UseConcMarkSweepGC: use CMS Garbage collector -XX: +UseSerialGC: Using the serial garbage collector -XX: +UseParallelGC: Using a parallel garbage collector // Java 11+ -XX: +UnlockExperimentalVMOptions -XX:+UseZGC // Java 12+ -XX: +UnlockExperimentalVMOptions -XX:+UseShenandoahGC
5. Branch diagnosis parameters
-XX: +-HeapDumpOnOutOfMemoryError option, When OutOfMemoryError Generated, i.e. memory overflow(Heap memory or persistent generation)When, automatic Dump Heap memory. Example usage: java -XX:+HeapDumpOnOutOfMemoryError -Xmx256m ConsumeHeap -XX: HeapDumpPath option, And HeapDumpOnOutOfMemoryError Use with, When the specified memory overflows Dump Purpose of the document Record. If not specified, it defaults to start Java The working directory of the program. Example usage: java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/local/ ConsumeHeap automatic Dump of hprof The file is stored in /usr/local/ Directory. -XX: OnError option, When a fatal error occurs( fatal error)Script to execute. for example, Write a script to record the error time, Execute some commands, perhaps curl Tell me about an online alarm url. Example usage: java -XX:OnError="gdb - %p" MyApp You can find one %p A formatted string representing the process PID. -XX: OnOutOfMemoryError option, Throw OutOfMemoryError Script to execute when an error occurs. -XX: ErrorFile=filename option, Log file name of fatal error,Absolute path or relative path. -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1506,Remote debugging
6.JavaAgent parameters
Agent yes JVM A black technology in, Many things can be done in a non intrusive way, such as injection AOP Code, execution statistics and so on. The permissions are very large. Here is a brief introduction to the configuration options. The detailed functions need to be specifically described. set up agent The syntax is as follows: -agentlib:libname[=options] Enable native Mode agent, reference resources LD_LIBRARY_PATH route. -agentpath:pathname[=options] Enable native Mode agent. -javaagent:jarpath[=options] Enable external agent library, such as pinpoint.jar wait. -Xnoagent Disable all agent. The following example turns on CPU Use time sampling analysis: JAVA_OPTS="-agentlib:hprof=cpu=samples,file=cpu.samples.log"