Java runtime data area
Java runtime data area can be divided into: method area, virtual machine stack, local method stack, heap and program counter
Thread private: virtual machine stack, local method stack, program counter
Thread sharingMethod area, heap:
A small memory space. It is the line number indicator of bytecode executed by the current thread. It is the indicator of program control flow. Basic functions such as branch, loop, jump, exception handling and thread recovery need to rely on this counter.
Each thread has a separate program counter.
Java virtual machine stack
Threads are private and have the same life cycle as threads.
When each method is executed, the Java virtual machine creates a stack frame for storageLocal variable table, operand stack, dynamic link, method exitOther information
dynamic link: the process of converting a symbol into a direct reference during operation is dynamic connection (the corresponding static connection in advance refers to the conversion of a static symbol reference into a direct reference during class loading).
Native Method Stack
It is similar to the Java virtual machine stack, but it works on the local native method.
A memory area shared by threads to store object instances. “Almost” all objects are allocated in the heap.
Due to timely compilation, especially the increasing demand for escape analysis technology, objects are not necessarily allocated in the heap (possible)On stack allocationandscalar replacement ）。
The Java heap can be divided into multiple thread private allocation buffers (tlabs) to improve the efficiency of object allocation. This TLAB only ensures that the thread can allocate here, but all threads can access it.
Thread sharing, storing the data loaded by the virtual machineType information、constant、Static variable、Code cache compiled by instant editorAnd other data.
Runtime constant pool for method area: store the constant pool table in the class file loaded in the class loader.
Java object memory allocation
Bytecode new instruction – > check constant pool – > classloader (load, connect (check, prepare, parse), initialization)
After checking, it is time to allocate memory for the new object. Allocation policy:
Whether the scope of the analysis object is in this method. If it is only in this method, it can be allocated on the stack. After escape analysis JDK7, it is enabled by default.
The object of new is not necessarily in the heap. It may be allocated and replaced by scalar on the stack
On stack allocation: one of the JVM tuning methods. If the method object does not escape, it can be allocated on the stack. Its life cycle is consistent with the method call to reduce the pressure of GC.
scalar replacement : if there is no escape from the object, the JVM may not create the object, but decompose the object variable into several member variables for replacement, so that it can be allocated on the stack frame or register (without continuous space), JDK7 which is enabled by default. Scalar substitution takes precedence over allocation on the stack.
TLAB: thread allocates cache locally (also in heap)
When allocating memory in Eden, if multiple threads allocate memory at the same time, it will cause pointer collision. In order to improve the efficiency of object allocation, TLAB is used.
When a thread is initialized, it will apply for a little memory of the specified size, and only the current thread is provided for memory allocation, so that each thread has a separate space.
TLAB is a special space divided by virtual machine in Eden of heap memory.
When the TLAB does not have enough space to meet the operation, you need to re apply for a new TLAB from the current thread
Java class file structure
The file structure of class bytecode is analyzed in strict order
|u4||magic||Magic number, identifying class file format, value: 0xcafebabe|
|u2||minor_version||Sub version number|
|u2||major_version||Major version number, 45 -?, 57 for jdk13 and 52 for jdk8|
|u2||constant_pool_count||Constant pool calculator|
|u2||access_flags||There are 9 access flags, including public and final. There are 16 flag bits, and each bit identifies an access flag.|
|u2||this_flags||Class index, index value in constant pool|
|u2||super_class||Parent index, index value in constant pool|
|u2||interfaces||Interface index collection, index value in constant pool|
|u2||fields_count||Number of fields|
|field_info||fields||Field set, field flag (public, static, etc.), field name constant index, description constant index (type)|
|method_info||methods||Method set is similar to field set, including method flag, method name index and method description index (return type and method parameter list)|
|u2||attributes_count||Additional attribute counter|
|attribute_info||attributes||Additional attribute collection|
Constant pools are divided into:Literal andSymbol reference
Literal: text string, final constant value, etc
- Fully qualified names of classes and interfaces
- Names and descriptors of fields and methods
- Method handle and type
- Dynamic call points and dynamic constants
Constant pool item type:
Class files, field tables, and method tables can all carry their own set of attribute tables to describe information specific to certain scenarios
The attributes (parts) are:
For example, the code attribute,
Class loading mechanism
Class loading process:
Load – > link (verify, prepare, parse) – > initialize
Load: load bytecode with classloader
Verification: verify the legitimacy of bytecode (meet the constraints)
Preparation: allocate memory for static fields of the loaded class
Resolve: resolve symbolic references to actual references.
Initialization: initializes constants and static classes
Start the class loader: load the most basic and important classes, such as the classes in the jar package under JRE’s lib
Extension class loader: his Frey is the startup class loader, which mainly loads relatively minor but common classes, such as jar classes under lib / ext of JRE
Application class loader: its parent class is the extension class loader, which is responsible for loading classes under the application path. (refers to the path specified by the virtual machine parameter – CP / – classpath, system variable java.class.path or environment variable classpath)
If the same byte stream is loaded by different class loaders, two different classes will be obtained.
Parental delegation mode: let the parent loader load as much as possible
Destruction of parental delegation mode:
1) If the class loaded by the upper class loader loads the class loaded by the lower class loader
Java introduces the context class loader, which can get through Frey loader to request the loading behavior of subclass loader. For example, when JNDI calls the service code.
2) OSGi hot deployment uses mesh class loading mode.
The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer.