JVM- essays

 

1, the structure of FIG.

 

 

2, parsing

A process area (Area Method,)
Object Class the Data (data loaded class definition class) is stored in the method area.
In addition, constants, static variables, JIT (time compiler) compiled code is also in the method area.
Because the data stored in the method area and there is a stack of analogies, it is also referred to as Non-Heap.
The method of the memory region may be composed of discontinuous regions, and may be set to a fixed size may be set to be expanded, as this heap.
Garbage collection will appear in this relatively small area, the region's main purpose for garbage collection and recycling unload the class constant pool.
************************************************** ************************************************** *********
 two, Java heap (the Java heap at the)
heap (heap) is JVM memory data area.
Heap management is very complicated, it is shared by all threads of memory area, start time is created in the JVM, designed to save the instance of the object.
Heap allocation memory to store a certain object instances, in fact, is only stored attribute value of the object instance, the object type and attribute type of markings itself,
does not save the object method (in the form of frames stored in the stack in Stack ), allocating a memory object instance stored in the Heap.
After the object instance and a good distribution in the Heap, you need to be saved in the Stack Heap a 4-byte memory address used to locate the position of the object instance in the Heap, easy to locate the object instance, is the main place for garbage collection.
java heap is physically noncontiguous memory space, as long as can be logically successive.

************************************************** ************************************************** *********
three, Java stack (at The Java stack)
(0) Overview
when a thread is started, Java virtual machine to create a Java stack for him.
Some states with discrete frame class record of Java thread stack.
Java Virtual Machine for Java stack operation are only two: push and pop frames.
The method of threads being executed is referred to as the current method (current method), a method corresponding to the current frame is called the current frame (current frame).
The method defined in the current class is called the current class (current class), the constant pool of the current class is called the constant pool of the current (current constant pool.).
When the thread execution, Java virtual machine to track the current class and the current constant pool. When a thread manipulate data stored in the frame, he only operation data of the current frame.
When a thread calls a method, the virtual machine to generate a new frame, and pressed into the Java thread stack, this new frame becomes the current frame.
When performing the method, the method he used to save the current frame parameters, local variables, intermediate, and other data structures.
There are two ways to exit: Exit normal and abnormal launched.
No matter which way the launch method, Java virtual machine will pop-up and drop-frame method, a method of frame becomes the current frame.
All data is stored in the frame can only be owned by its thread access, the thread can not access data from other thread stack.
So, when a local variable access method, no need to consider multi-thread synchronization.
And a method area, the same stack, Java stacks do not require continuous memory space, it can be stored in a memory space or heap dispersion.
Stack specific data and the length of the Java virtual machine has its own definition of implementers.
Some implementations may provide a method of execution stack maximum and minimum values.

(1) the stack frame (The Stack Frame)
stack frame comprises three parts: local variables, and operand stack frame data.
Local variables and the operand stack size in bytes of all, they have been identified in the compilation.
Frame data size depending on the implementation.
When a program calls a method, the virtual machine type data acquired from the local variables and operand stack size, and creates a frame of suitable size, and then pressed into the Java stack.

(1.1) the local variable (Local Variables)
local variables in Java stack frames are stored in a tissue by providing their array index count from 0, to obtain the corresponding instruction from the local value of the variable region.
Int, float, reference, returnValue one byte, byte, short, char is then stored to int, long, and doubel two bytes.
Instruction value to obtain a long, doubel by providing the first two bytes in the index.
For example, a long values are stored in the index 3,4, instructions can be made by the value of the long type 3.
Local variable region contains parameters and local variables of methods. Compiler parameters of the method stated in the order they are placed in front of the array.
However, the compiler may be a local variable, but in any order in a local variable array, or even two local variables may be a public address, for example, when two local variables in the two regions do not overlap, as loop variable i, j .
Virtual machine implementations can use any data structure to describe the local variable region, the virtual machine specification does not define how long storage and doubel.

(1.2) the operand stack (Operand Stack)
with local variables, the operand stack also be organized as an array of bytes.
But unlike local variables as access through the index, but to achieve access through the push and pop values.
If a command stack to push a value, then the next instruction can be used and the pop value.
Unlike the operand stack as the program counter can not be directly accessed instruction, the instruction can directly access the operand stack.
Java Virtual Machine Is a stack-based rather than register-based instruction because it's made from the operand stack, rather than the same register.
Of course, instruction operands may be acquired from other places, such as the back of the instruction operation code, or the constant pool.
But the Java virtual machine instructions are mainly made of the number of operations they need from the operand stack.
Java Virtual Machine will operand stack regarded as a work area, a lot of command by starting with the operand stack pop value, and then after the processed results push back the operand stack.
Execution of an add instruction as shown below:
first two numbers iload_0 and performing two instructions require iload_1 added, removed from the local area method, and the operand stack push;
and instruction iadd execution, the first pop two values, and place the result into the operand stack pusp;
last instruction executed istore_2, pop results, assigned to the local region method.

Popular terms: the operand stack is the value calculated during storage operations, such as 3 + 5, the stored value is the value 3 and 5.
For details, see: https: //www.cnblogs.com/chendongfly/p/4189707.html

 

(1.3) frame data (Frame Data)

Stack frame (Stack Frame) is used to support virtual machine data structures and methods for performing the method calls.
It is a virtual machine runtime data area of the virtual machine stack stack elements.
Storing a local variable stack frame methodology, the operand stack, and a method for dynamic link return address and other information.
Each method call process from the beginning to the completion of the execution, the process corresponds to a virtual machine inside the stack frame from the stack to the stack.
When the compiler code, stack frame requires much local variable table, how deep the operand stack have been completely determined.
So a stack frame how much memory needs to be allocated, the program will not be affected runtime variable data, but only depending on the particular virtual machine implementation.
In active threads, only the top of the stack is located in the stack frame is valid, it called the current stack frame, and this method is called a stack frame associated with the current method.

Local variable table []:
the local variable table (Local Variable Table) is a set of variable values storage for storing local variables defined within the method parameters and method.
And when compiled into Java Class files, it has been determined that the maximum capacity of the local variables of the method needs to be allocated for the table.
The capacity of the local variable table to a variable groove (Variable Slot) as a minimum unit, each variable slot length may be stored in memory 32, e.g. boolean, byte, char, short, int, float, reference.
For data types (long, double) 64-bit length, the alignment of the high order virtual machine assigned to Slot two consecutive spaces, that is equivalent to a long and double data types read two 32-bit write to become divided .
When performing the method, the virtual machine uses the local variable table to complete the transfer process parameter value of the parameter variables list, if the example method is performed, then the local variable table index of Slot 0 is the default delivery method for the object instance belongs references.
(In the method may be accessed through the keyword this to the implicit parameter).
Other parameters according to the parameter table is arranged in order, from a local variable occupancy starting Slot.
In order to save space stack frame as much as possible, the local variable table Slot is reusable, that is to say when the PC counter instruction that has been beyond the scope of a variable (finished), then the corresponding variable can be Slot to other variables. 
Advantages: space-saving stack frame. 
Cons: affects garbage collection behavior of the system. (Such as large method consumes more Slot, Slot assignment no set null or empty value after completion of the implementation of the scope of the method, the garbage collector can not reclaim the memory in time.)

reference (reference object instance) In general, a virtual machine can find the referenced directly or indirectly from the subject to the following two points: 
A, data stored in the Java heap starting address index. 
b, relevant data type stored in the data type area method.


[Dynamic link]
constant pool in the Class file contains a lot of symbolic reference.
The method of the bytecode instruction to call a symbol reference constant pool directed to a method as a parameter.
The reference symbol part (resolved) or the first time used in the loading phase is converted to a direct reference type (like directional data stored address pointer or handle), this conversion is called static linking.
But on the contrary, the other part is converted to a direct reference during operation, it is called dynamic linking.
Dynamic expansion is bytecode can be modified dynamically at runtime, i.e. reflection and cglib


Methods return address]
When a method to begin, only two ways to exit this method:
--- method returns the command: execution engine encounters a bytecode instruction method returns, this time there may be a return value is passed methods caller to the top of this exit is called normal completion exports.
--- quit unexpectedly: an exception is encountered during the execution process, and does not handle the exception, it will cause the method to quit.
No matter which way exit, after the method exits, need to return to the position method is called, the program can continue, it may need some of the information stored in the stack frame when the method returns.

] [Operand stack
operand stack and local variable table, as determined at compile time has a maximum capacity of the process required for the local variables allocated table.
Each operand stack elements are available arbitrary Java data types, including long and double. 32-bit data type stack occupied capacity of 1,64-bit data type capacity occupied by the stack 2.
When beginning a method of execution, the operand stack is empty this method, during the process execution, bytecode instructions are written to have a variety of operand stack and to extract the contents, i.e. the pull / stack operation.
For example, when doing the arithmetic operation is performed by the operand stack, or other methods when calling is carried out by passing the parameter operand stack.
Two dull as a virtual machine stack stack frame elements are completely independent, but the virtual opportunity to make the appropriate optimization, so that the overlapping part of the two stack frame appears.
Part operand stack local variable stack frame on a table and the stack frame overlap, can share a portion of the data is performed such method calls, without the need for additional copy transfer parameters.

 

************************************************** ************************************************** *********
Fourth, the program counter PC (the program counter)
when each thread begins execution will be to create a program counter.
The program counter is only a word length (word), so it is possible to save a local pointer and returnValue.
When a thread of execution, a program counter address stored in the instruction is being executed, the address may be a local pointer, a pointer may be offset from the start of the method bytecode.
If the native method execution, the program counter is not defined.

Like the local variables and the operand stack is organized into a word-wide array.
But the former and different is that it is not accessible through the index, but by the standard stack operations - push and pop - to visit.
For example, if a command to put a value onto the operand stack, pop up later by another instruction can use this value.

************************************************** ************************************************** *********
five, native method area
native method is written by other languages and compiled into machine code associated processor.
Simply put, a Native Method is a non-call interface java java code.
Why use Native Method

java very convenient to use, but some level tasks using java is not easy, or we are very concerned about the efficiency of the program, the problem came.
Interaction with the external environment java:
sometimes java application needs to interact with the environment outside of java. This is the main reason for the existence of local methods, you may need to think about the situation at the time of java with some of the underlying system hardware such as operating system or some exchange of information.
Local exchange method is such a mechanism: it provides a very simple interface for us, and we do not need to understand the complicated details beyond the java application.
Interact with the operating system:
the JVM and runtime library supports java language itself, it is a java program for the survival of the platform, which consists of an interpreter (bytecode interpretation) and some connections to native code libraries.
In any case, however, it is after all not a complete system, which often rely on some of the underlying (underneath below) support system. These systems are often strong underlying operating system.
By using native methods, we were able to achieve with java jre interaction with the underlying system, and even some parts of the JVM is written in C, and, if we want to use some java language itself does not provide the operating system package features we also need to use native methods.
The Java's Sun
Sun interpreter is implemented in C, which makes it like some ordinary C as interaction with the outside.
Most jre is the realization of java, it also adopted a number of local methods to interact with the outside world.
For example: class java.lang.Thread of setPriority () method is the realization of java, but it is the type of implementation calls in the local method setPriority0 ().
This local approach is implemented in C, and is implanted inside the JVM, on the Windows 95 platform, the native methods will eventually call Win32 SetPriority () API.
This is a concrete realization of a native method provided directly by the JVM, the situation is more local methods provided by an external dynamic link libraries (external dynamic link library), and then call the JVM.

How JVM to run up the Native Method:
We know that when a class is first to use the bytecode of the class will be loaded into memory, and will only return once the carrier.
Maintained at this inlet is loaded with a byte code class descriptors List all methods, these methods descriptor contains messages such as: a method where the code is stored in, what parameters it has, the method descriptor (public of class) and so on.
If there is a native method descriptor, the descriptor block will have a pointer pointing to realize the method.
These implementations in a number of DLL files, but they will be loaded into the address space of the operating system java program.
When a class is loaded with the local-method, which is not related to the DLL is loaded, a pointer so implemented method will not be set.
These DLL load is achieved by calling System.loadLibrary (String filename) or System.load (String filename) method.

Finally, it should be reminded that, using the local approach is overhead, it lost a lot of benefits of java. If you have no choice, we can choose to use native methods.

 

 

Six other relevant

-------------------------------------------------- ----- element space ------------------------------------------- -------------------------------------------------- ---------------------------------------------
1.1, why is removed permanent generation

- Its size is fixed at startup good - very difficult to tune. -XX: MaxPermSize, set how much good?
Internal type --HotSpot also Java objects: it may be moved in the Full GC while its application opaque, and non-strongly typed, it is difficult to trace debugging, but also metadata information stored metadata (meta-metadata) .
- Optimization Full GC: Each collector specialized iterator metadata.
- class data can be concurrently released in the case GC without pause.
- makes some improvements originally limited to the permanent generation of future possible.

1.2, after removing the permanent generation, status PermGen space

- This memory space will be fully removed.
- metadata information like still, but no longer stored on contiguous heap space, but to move to is called "Metaspace" local memory (Native memory) in.
--String types exist heap directly.
--JVM parameters: PermSize and MaxPermSize will be ignored and a warning (if you set these two parameters when enabled).


1.3 yuan space features

- take full advantage of the benefits of the Java language specification: the same life cycle class and the associated metadata and class loader.
- Each loader has a dedicated storage space
- only for linear distribution
- not separately recovered a class
- eliminating the need for scanning and compression time GC
- dimensional space position of the object is fixed
- - If the GC found that a class loader is no longer viable, and will related to space out the entire recovery

1.4, memory allocation model metadata space

- most of the space class from the metadata allocated local memory
- used to describe the class-based metadata (klasses) has been deleted
- a plurality of points assigned metadata virtual memory spaces
- for each a class loader to allocate a memory block list. The size of the block depends on the type of class loader; Sun / reflector / proxy class loader corresponding to the block will be smaller
- the return of a memory block, the memory block list is released
- upon dimensional space data are cleared, virtual memory space will be recycled out
- to reduce fragmentation of policy

Guess you like

Origin www.cnblogs.com/huazai1024/p/11077420.html