Said to the JVM, the brother for many years, a lot of work may be a little feel timid, because move brick for many years, has been using Java this tool, for the JVM did not understand, have words interview build aircraft carriers, screw to work, to what the bike, know how to move brick is ok, why want to know so much, if you have a very strong business acumen, Don’t need to know too much in-depth things, as long as the completion of business functions can, if you eloquence is not good, only a programming brain, old iron sink heart let’s understand, you usually screw the wrench structure, this is really useful. Because it allows you to go further and earn more!
###JVM
Some concepts of JVM
- What is the JVM
The JVM, short for Java Virtual Machine, is a specification for computing devices. The JVM is an imaginary computer that is implemented by emulating various computer functions on an actual computer. The Java virtual machine consists of a set of bytecode instructions, a set of registers, a stack, a garbage collection heap, and a storage method field. The JVM shields platform-specific information, allowing Java programs to run unmodified on multiple platforms by generating object code (bytecode) that runs on the Java VIRTUAL machine. When the JVM executes bytecodes, it actually ends up interpreting bytecodes as platform-specific machine instructions.
- JVMS and common VMS
1.JVM is a Java Virtual Machine (Java Virtual Machine), which executes Java bytecode environment, a program’s own independent environment, must contain stacks, registers, bytecode instructions. Java, Android, Scala, Groovy, and so on all run on the JVM, and they all follow the JVM’s instruction set, the Class specification. It is a complete complete can provide a virtual host PC, this virtual machine side must install the operating system, it is the simulation of the physical host CPU instruction set.
- Relationship between JVM, JDK, and JRE
The JVM is the basic unit, the JRE is the environment in which Java is run, and the JDK is the development tool set. The JVM is smaller than the JRE than the JDK
1.JRE(JavaRuntimeEnvironment), which is the Java platform. All Java programs must run under JRE. Common users only need to run the developed Java program and install the JRE.
2. Java Development Kit (JDK) is a Development Kit used by program developers to compile and debug Java programs. JDK tools are also Java programs that require JRE to run. To maintain JDK independence and integrity, the JRE is also part of the JDK installation. Therefore, in the JDK installation directory, there is a directory named JRE to store the JRE file.
3. The JVM(JavaVirtualMachine) is a part of the JRE. It is an imaginary computer by simulating various computer functions on an actual computer. The JVM has its own complete hardware architecture, such as processors, stacks, registers, and the corresponding instruction system. The most important feature of the Java language is that it runs across platforms. The PURPOSE of using the JVM is to enable cross-platform implementation independent of the operating system.
- What are the JVM products
HotSpot, Jrockit, J9
- Why the JVM
1.C, C++ is based on OS architecture and CPU architecture. The 64-bit version will not run in 32-bit. Very high performance, write the underlying implementation. 2.JAVA can be written at a time to run everywhere, portability is good.
JVM Running Process
1. Java is compiled into class files, bytecode files, not pure binary bytecode files, which cannot be run directly on the operating system, but pure binary files can be run directly. 2. The JVM has an execution engine that, when compiled by the JVM, translates instructions to different operating systems. Send it to the operating system. All operating systems can recognize their own instructions. Windows is a DLL program, Linux is a.o dynamic link library.
The JVM structures
1. Classloader 2. Execution engine 3. Runtime data area 4
- ClassLoader ClassLoader
The JVM loads a.class file. Actually, loading a class means reading the binary data from the class’s.class file into memory, placing it in the method area of the runtime data area, and then creating a java.lang. class object in the heap that encapsulates the class’s data structure in the method area. The end product of Class loading is the Class object in the heap, which encapsulates the Class’s data structure in the method area and provides the Java programmer with an interface to access the data structure in the method area.
At the same time, the JVM specification allows classloaders to preload a class when they expect it to be used. If a.class file is missing or an error is encountered during preloading, the classloader will generate a LinkageError report when the program first actively uses the class. If the class has not been actively used by the program, the classloader will not report an error.
- Parental delegation model
- Class loading process
- When a classloader receives a class-loading task, it does not immediately expand the load. Instead, it delegates the loading task to its parent classloader, and the classes at each level do the same until the delegate is delegated to the topmost bootloader. If the parent class loader is unable to load the class entrusted to it, the class loading task is returned to the next class loader to perform the loading.
- The working process of the parental delegation model is as follows: If a classloader receives a classload request, it does not try to load the class itself. Instead, it delegates the request to the parent classloader. This is true at every level of classloaders, so all load requests should eventually be sent to the top level of the starting classloader. Only when the parent class loader reports that it cannot complete the load request (it did not find the required class in its search scope) will the child loader attempt to load it itself.
- The advantage of using the parent delegate mechanism is that the global uniqueness of a class is effectively guaranteed. When multiple classes with the same qualified name appear in the program, the class loader will always load only one of them.
- One of the obvious benefits of using the parent delegate model to organize relationships between class loaders is that Java classes have a hierarchical relationship with priority along with their classloaders. For example, the java.lang.Object class, which is stored in rt.jar, is ultimately delegated to the boot class loader at the top of the model by whichever class loader loads the class, so the Object class is the same class in each loader environment of the program. On the other hand, instead of using the parent delegate model and loading by class loaders, if a user writes a class called java.lang.Object and places it in the program’s ClassPath, there will be multiple different Object classes. The most basic behavior in the Java type system is not guaranteed, and the application becomes a mess. If you were to write a Java class with the same name as an existing class in the Rt.jar library, you would find that it would compile properly, but would never be loaded and run.
- The parent delegate model is very important to ensure the stable operation of Java programs, but its implementation is very simple. The code to implement the parent delegate is centralized in the loadClass() method of java.lang.classLoader, and the logic is clear and easy to understand: Check whether it has been loaded. If not, call the loadClass() method of the parent class loader. If the parent class loader is empty, start class loader is used as the parent by default. If the parent class loader fails to load, throw a ClassNotFoundException and call your own findClass method to load.
- loading
Simply put, the classloader is responsible for reading the binary byte stream of a class based on its fully qualified name into the JVM and storing it in the method section of the runtime memory area. It then converts it to an instance of a Java.lang. Class object corresponding to the target type (the Java Virtual Machine specification does not specify that it must be stored in the heap, only Hotspot chooses to store the Class in the method area). This Class object will later serve as an access point for the various data of that Class in the method area.
- link
The linking phase incorporates the class data information from the binary byte stream loaded into the JVM into the JVM’s runtime state through three phases: validation, preparation, and parsing. (1) Verify whether the class data information conforms to JVM specifications and is a valid bytecode file. The verification content covers the format verification, semantic analysis and operation verification of the class data information. Semantic validation: Checks whether a type marked final contains subclasses; Check that final method videos ina class are overridden by subclasses. Make sure there are no incompatible method declarations between the parent and subclass (for example, the method signature is the same but the method return value is different). Data must be in the operand stack for the correct operation, the constant pool in various symbols refer to perform validation (usually enforced in the parsing stage, check whether the rich by the fully qualified name location described in reference to the specified type, as well as a class member information access modifier whether to allow access, etc.) (2) all the static variables are allocated memory space in preparation for the class, And sets an initial value for it (since no object has been generated, the instance variable is outside the scope of this operation). Static variables that are modified by final are given their original value directly. (3) Parse symbolic references in the constant pool to direct references (get the in-memory pointer or offset of a class or field or method, so that the method can be called directly). This can be done after initialization. It can be considered that some static bindings are resolved, while dynamic bindings are resolved only at run time; Static bindings include final methods (which cannot be overridden),static methods (which only belong to the current class), and constructors (which cannot be overridden)
- Initialize the
Execute all code in a class that is identified by the static keyword. If static variables are executed, the user-specified value overrides the initial value set in the preparation phase. If a static code block is executed, then during the initialization phase, the JVM performs all the actions defined in the static code block. All class variable initializers and static code blocks are placed in the collector by the front-end compiler at compile time in a special method called a method, the class/interface initialization method. This method initializes a variable in a class, overwriting the initial values set in the preparation phase with user-specified values. Any bytecode like Invoke cannot call a method, because the method can only be called by the JVM during class loading. If the parent class has not already been initialized, the parent class is initialized first, but the method calling the parent class is not shown inside the method, and the JVM is responsible for ensuring that a class’s methods are executed before they are. The JVM must ensure that when a class is initialized, if multiple threads need to initialize it at the same time, only one of the threads is allowed to initialize it. The remaining threads must wait until the active thread has completed the initialization of the class before notifying the waiting threads.
JVM runtime data area
When executing Java code, the JVM uses memory in several parts, called data areas, which have their own purposes and are created and destroyed as the JVM process is started or the user thread is started and terminated.
- Program Counter Register
role
The line number indicator of the bytecode executed by the current thread, which can be changed to select the next bytecode instruction to execute
Characteristics of the
1. Created when a thread is created 2. Each thread has an address that points to the next instruction
- Method Area(Non-Heap)
Threads share
storage
1. Class information 2. Constants 3. Static variables 4
- VM Stack / Native Method Stack
Thread private
Method execution creates a Stack Frame to store information about local variables, operand stacks, dynamic links, method exits, and so on
The process from method invocation to completion corresponds to the process of a stack frame being pushed into and out of the virtual machine stack
The memory space required by the local variable table is allocated at compile time, and how much local variable space is allocated is completely determined and does not change its size during the method run
Space is released after the stack is removed
- Heap
Threads share
Store objects or arrays
Heap division
- JMM
Thread working memory, main memory
The JMM model
PS: Sort out the concept of JVM, and the relationship between JVM and JDK, why to use JVM, THE understanding of JVM, the running process of JVM, class file by JVM execution engine, loaded into the data area, compiled into the operating system recognized instructions, different instructions for the operating system. The JVM has only one class for each operating system. Cross platform. The JVM’s running data is divided into thread-shared data areas (methods, heap) and thread-independent data areas (stack, program counters). The JVM really is basically all text stuff. It might be a little hard to understand. It’s a little easier to understand if you have work experience. It’s really too low.