2018.4.23 Notes on "In-depth Understanding of Java Virtual Machine: JVM Advanced Features and Best Practices"

1. Java memory area and memory overflow

1. The program counter is a small memory space that can be seen as a line number indicator of the bytecode executed by the current thread. When the bytecode interpreter works, it selects the next bytecode instruction to be executed by changing the value of this counter. Each thread needs to have an independent program counter, which does not affect each other and is stored independently. This memory area is the only one that does not specify any oom case in the java virtual machine specification.

2. The virtual machine stack describes the memory model of java method execution. Each method will create a stack frame at the same time as it is executed, which is used to store the local variable table, operand stack, dynamic link, method exit and other information. If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a soe exception will occur, and if enough memory cannot be applied for dynamically expanding the virtual machine stack, an oom exception will occur. The memory space required by the local variable table is allocated at compile time, and it is completely determined how much local variable space a method needs to allocate in the frame.

3. The function of the local method stack is similar to that of the virtual machine stack. The local method stack serves the native methods used by the virtual machine.

4. The java heap is the largest piece of memory managed by the virtual machine for most applications. The java heap is a memory area shared by all threads. The sole purpose of this memory is to hold object instances. All object instances and arrays are allocated on the heap, but not absolutely. The java heap is the main area managed by the garbage collector. The java heap can be in a physically discontinuous memory space, as long as it is logically contiguous.

5. The method area, like the Java heap, is a memory area shared by each thread, which is used to store data such as class information, constants, static variables, and code compiled by the real-time compiler that have been loaded by the virtual machine. The 1.7 hotspot has moved the string constant pool that was originally placed in the permanent generation.

6. The runtime constant pool is part of the method area. The intern method of the String class uses the runtime constant pool.

7. Direct memory is mainly an IO method based on channels and buffers introduced by NIO. It can directly allocate off-heap memory using the native function library.

2. Object creation (for HotSpot virtual machines)

1.Java allocates memory for objects in the Java heap in two ways: pointer collision and free list.

2. There is a way of allocation called local thread allocation buffer (TLAB), which is set by -XX:+/-UseTLAB.

3. After the memory allocation is completed, the virtual machine needs to initialize the allocated memory space to a zero value.

4. The method is executed after the new instruction, and the object instance is completed.

5. The layout of objects stored in memory can be divided into 3 areas: object header, instance data and alignment padding.

6. The object header includes two parts of information, the first part is used to store the runtime data of the object itself. Another part of the object header is the type pointer, which is the pointer to the object's class metadata.

7. The instance data part is the valid information that the object actually stores. In an allocation strategy, fields of the same width are always allocated together. When this precondition is met, variables defined in the parent class will appear before the child class. If the value of the compactFields parameter is true (the default is true), then the narrower variables in the subclass may also be inserted into the gaps in the parent class variable. The third part of alignment padding is not necessarily present.

8. Our java program needs to operate specific objects on the heap through the reference data on the stack. The current mainstream access methods are the use of handles and direct pointers.

3. Is the subject dead?

1. (Reference Counting Algorithm) The reference algorithm adds a reference counter to an object. Whenever there is a place to reference it, the counter value is incremented by 1; when the reference is invalid, the counter value is decremented by 1; any object whose counter is 0 at any time It just can't be used anymore. The reference counter algorithm is not used in the mainstream Java virtual machine, the main reason is that it is difficult to solve the problem of circular references between objects.

2. (Reference counting algorithm) Reachability algorithms, in mainstream implementations, use reachability analysis to determine whether an object is alive. The basic idea of ​​this algorithm is to start a downward search through a series of objects called GC Roots as the starting point.

3. Objects that can be used as GC Roots include the following: objects referenced in the virtual machine stack; objects referenced by class static attributes in the method area; objects referenced by constants in the method area; objects referenced by JNI in the local method stack.

4. Strong reference, soft reference, weak reference, virtual reference.

5. To truly declare an object dead, at least two marking processes are required.

6. When the object does not override the finalize method, or the finalize method has been called by the virtual machine, the virtual machine regards both cases as "unnecessary to perform" finalize.

6. The finalize method of any object will only be automatically called once by the system. Don't use the finalize method, it is inefficient and uncertain.

7. The garbage collection of the permanent generation mainly recycles two parts: discarded constants and useless classes.

8. The most basic collection algorithm is the "mark-clean" algorithm, followed by the replication algorithm, the mark-sort algorithm, and the generational algorithm.

4. Algorithm implementation of HotSpot

1. Knowledge points: enumerate root nodes (OoMap), safe points, safe areas

2. Garbage collector:

The Serial collector is the most basic and oldest collector, single-threaded, stop the world, simple and efficient, and a good choice for virtual machines running in client mode.

The ParNew collector is the multi-threaded version of the Serial collector and is the preferred new generation collector for many virtual machines running in Server mode. A very important reason is that, apart from the Serial collector, currently only it can work with CMS Collectors work together.

The Parallel Scavenge collector is a new generation collector whose goal is to achieve a controllable throughput. The so-called throughput is the ratio of the time the CPU spends running user code to the total CPU consumption time.

Serial Old is the old version of the Serial collector. It is also a single-threaded collector. It uses the mark-sorting algorithm. The main significance of the collector is to use it for virtual machines in Client mode.

Parallel Old is an older version of the Parallel Scavenge collector. The CMS collector is a collector with the goal of obtaining the shortest collection pause time. It is based on the mark-and-sweep algorithm. It has the advantages of concurrent collection, low pause, and is very sensitive to CPU resources, but cannot handle floating garbage. Lots of space debris.

The G1 collector is currently an excellent commercial collector.

5. Memory allocation

1. In most cases, objects are allocated in the new generation Eden area. When the Eden area does not have enough space for allocation, the virtual machine will initiate a Minor GC.

2. Large objects enter the old age directly.

3. Long-lived objects will enter the old age.

4. Dynamic object age determination.

5. Space allocation guarantee.

Six.class class file structure

1. According to the Java virtual machine specification, the Class file format uses a pseudo-structure similar to the C language structure to store data. There are only two data types in this pseudo-structure: unsigned numbers and tables. To be based on these two data types.

2. Unsigned numbers are basic data types that can be used to describe numbers, index references, quantity values, or to form string values ​​according to UTF-8 encoding.

3. A table is a composite data type composed of multiple unsigned numbers or other tables as data items, and all tables habitually end with "_info". The table is used to describe the data of the compound organization with the hierarchical relationship, and the whole class file is essentially a table.

4. The first 4 bytes of each Class file are called magic numbers, and its only function is to determine whether the file is a Class file that can be accepted by the virtual machine. The 4 bytes following the magic number store the version number of the Class file: the 5th and 6th bytes are the minor version number, and the 7th and 8th bytes are the major version number. The version number of Java starts from 45. The major version number of each major JDK release after JDK1.1 increases by 1. The higher version of JDK can be backward compatible with the class file of the previous version, but cannot run the class file of the later version. , the virtual machine must refuse to execute class files older than their version number even if the file format has not changed.

5. Next to the major and minor version numbers is the entry of the constant pool. The constant pool can be understood as the resource warehouse in the Class file. It is the data type most associated with other projects in the Class file structure, and it also occupies the largest space in the Class file. One of the data items, and it is also the first table-type data item in the Class file. Constant pool capacity counts start at 1 instead of 0. The constant pool mainly stores two types of constants: literals and symbolic references. If a variable or method name with more than 64KB English characters is defined in a Java program, it will not compile.

6. After the constant pool ends, the next two bytes represent the access flag, which is used to identify some access information at the class or interface level, including: whether the Class is a class or an interface, whether it is defined as a public type, and whether it is defined For the abstract type, if it is a class, whether it is declared final, etc.

7. Class index, parent class index and interface index set: both the class index and the parent class index are a u2 type of data, and the interface index set is a set of u2 type data sets, which are determined by these three data in the class file The inheritance relationship of this class. The class index, parent class index, and interface index are all ordered after the access flag.

8. The field table is used to describe the variables declared in the interface or class. Fields include class-level variables as well as instance-level variables, but not local variables declared inside methods.

9. Method table collection: The description of methods in the Class file storage format is almost identical to the description of fields. After the java code in the method is compiled into bytecode instructions by the compiler, it is stored in an attribute named "code" in the method attribute table collection.

10. Property table collection: In the Class file, field table, method table can carry their own property table collection to describe the specific information of some scenarios.

7. Introduction to bytecode instructions

1. The instruction of the Java virtual machine consists of a byte length, a number representing the meaning of a specific operation, and zero or more parameters that follow to represent the required parameters of the operation.

2. In the instruction set of the Java virtual machine, most of the instructions contain data type information corresponding to their operations.

3. Load and store instructions.

4. Operation instructions.

5. Type conversion instructions.

6. Object creation and access instructions.

7. Operand stack management instructions.

8. Control transfer instructions.

9. Method call and return instructions.

10. Exception handling instructions: In the Java virtual machine, handling exceptions is not implemented by bytecode instructions, but by an exception table.

11. Synchronization instructions.

8. Virtual machine class loading mechanism

1. The virtual machine loads the data describing the class from the Class file into the memory, and verifies the data, converts, parses and initializes the data, and finally forms a Java type that can be directly used by the virtual machine. This is the class loading mechanism of the virtual machine.

2. In the Java language, the type loading, connection and initialization process are all completed during the program runtime.

3. The whole life cycle of a class starts from being loaded into the virtual machine memory and unloads the memory. Its entire life cycle includes seven stages: loading, verifying, preparing, parsing, initializing, using and unloading. Among them, the three parts of verification, preparation and analysis are collectively called connection.

4. When using the new keyword to instantiate an object, read or set a static field of a class (modified by final, except for the static field that has put the result into the constant pool at compile time), and call the static field of a class method time. Trigger initialization.

5. When using the method of the java.lang.reflect package to make a reflection call to a class, if the class has not been initialized, its initialization needs to be triggered first.

6. When initializing a class, if you find that its parent class has not been initialized, you need to trigger the initialization of its parent class first.

7. When the virtual machine starts, the user needs to specify a main class to be executed, and the virtual machine initializes this main class first.

8. When the dynamic language support of JDK1.7 is used, if the final analysis result of the MethodHandle instance is a specific method handle, and the class corresponding to this method handle has not been initialized, the initialization will be triggered.

9. Referencing the static fields defined in the parent class through the subclass will only trigger the initialization of the parent class but not the subclass.

10. Referencing a class through an array definition does not trigger initialization of this class.

11. Constants will be stored in the constant pool of the calling class during the compilation phase. Essentially, there is no direct reference to the class that defines the constant, so the initialization of the class that defines the constant will not be triggered.

12. When an interface is initialized, it is not required that all its parent interfaces have been initialized. It will only be initialized when the parent interface is actually used.

13. Loading: Obtain the binary byte stream through the fully qualified name of a class; convert the static storage structure represented by the byte stream into the runtime data structure of the method area; generate a java.lang. Class object, as the access entry.

14. Validation: file format validation; metadata validation; bytecode validation; symbol reference validation.

15. Preparation: The preparation phase is the extreme of formally allocating memory for class variables and setting the initial value of class variables. The memory used by these variables will be allocated in the method area.

16. Parsing: The parsing phase is the process in which the virtual machine replaces the symbolic references in the constant pool with direct references.

17. Initialization: The class initialization phase is the last step in the class loading process. At this step, the Java program code defined in the class is actually executed.

18. In the static statement block, only the variables defined before the static statement block can be accessed, and the variables defined after it can be assigned values ​​in the preceding static statement block, but cannot be accessed.

19. Class loader: For any class, its uniqueness in the Java virtual machine needs to be established by the class loader that loads it and the class itself. Each class loader has an independent class namespace .

20. Parent delegation model

9. Virtual machine bytecode execution engine

1. Run-time stack frame structure: The stack frame stores the method's local variable table, operand stack, dynamic connection and method return address and other information. The process from the start of the call to the completion of the execution of each method corresponds to the process of a stack frame from being pushed to the stack in the virtual machine stack.

2. Local variable table: A set of variable value storage spaces for storing method parameters and local variables defined within the method.

3. Operand stack: Also known as the operation stack, it is a last-in, first-out stack.

4. Dynamic connection: Each stack frame contains a reference to the method to which the stack frame belongs in the runtime constant pool. This reference is held to support dynamic connection during method invocation.

5. Method invocation is not the same as method execution. The only task in the method invocation phase is to determine the version of the called method, and the specific running process inside the method is not involved for the time being.

6. The parsing call must be a static process, which is completely determined during compilation. In the parsing phase of class loading, all the involved symbol references will be converted into determinable direct references. According to the number of assignments, it can be divided into single dispatch and multiple dispatch.

7. All dispatch actions that rely on static types to locate the execution version of a method become static dispatch. A typical application of static dispatch is method overloading.

8. We call the dispatching process of determining the execution version of the method according to the actual type at runtime as dynamic dispatching.

As of 9.1.7, the java language is a language with static multiple dispatch and dynamic single dispatch.

10. The key feature of a dynamically typed language is that the main body of its type checking is at run time rather than at compile time. The fact that variables are untyped and variable values ​​have types is also an important feature of dynamically typed languages.

11. The difference between MethodHandle and Reflection

12.java syntactic sugar: generics, auto-boxing, auto-unboxing, traversal loops, variable-length parameters, conditional compilation, etc.

10. JAVA thread

1.volatile variables can only guarantee visibility. The second semantic of using volatile is to disable instruction reordering optimizations.

2. The principle of happening first.

3. KLT and LWP; user thread; mixed thread.

4. Cooperative thread scheduling; preemptive thread scheduling.

5. Thread status: new, running, waiting indefinitely, waiting indefinitely, blocking, ending.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693874&siteId=291194637