Home > Back-end >  The JVM memory model
The JVM memory model

Time:09-25

The JVM is we to become an architect must master the knowledge of a system, we and can reflect the general will only knock code programmers specific gap, and the data structure of the JVM foundation, also is the memory model, is the first lesson of its introduction, I will be divided into three aspects to introduce below the JVM memory model
Introduce the JVM
The JVM overall architecture
The JVM memory structure
Introduce the JVM
1. The virtual machine is what
Virtual Machine (Virtual Machine) through software simulation, which has the function of a complete hardware system, running on a complete computer system is completely isolated environments, popular speak is to use the software to simulate a physical Machine,
Most of the time, when we need to use multiple computers, but money is tight, you can use a virtual machine to simulate the new computer use,
Commonly used Virtual Machine VMWare (general multi-purpose simulation Linux systems), Virtual Box simulation (App), Java Virtual Machine (JVM)
2. What the JVM is
JVM full name is the Java Virtual Machine (Java Virtual Machine), you can understand it: the JVM is the Java bytecode (class files) for physical Machine of Machine code, Java through it implements a compile, run everywhere grandiloquence, any Machine installed JVM, as long as I can put the compilation of packaged good Java code up and running!!!!!! ,
3. The JVM on market
Sun HotSpot VM, BEA more about JRockit over VM, IBM J9 VM, Azul VM, Apache Harmony, Google Dalvik VM, Microsoft JVM
There with a lot of have a Sun HotSpot VM, BEA more about JRockit over VM, IBM J9 VM, the first two of them have acquired by Oracle, the latter, heard that Oracle also want to buy, but wanted to think, it's too expensive, also gave up,
The JVM overall architecture
1. The internal structure of the JVM
The JVM is divided into three parts, as follows:

Class loader: responsible for loading the file into the runtime data area
Runtime data area (memory structure) : responsible for store where runtime data
Execution engine: input bytecode, executing the corresponding instructions, the output
We can put their relationship as 3, CPU (execution engine), memory (runtime data area), the file system driver (class loader), first by the file system driver (class loader) to read a file, in the memory (runtime data area), CPU (execution engine) interact through and memory (runtime data area), get the calculation results,
2. The runtime data area structure
Runtime data area in Java design specification is divided into five parts, respectively is the program counter, Java stack, local method stack, the Java heap and method, is introduced as follows:

Thread the program counter (private) : is a pointer to point to the next instruction address, namely tell execution engine, this thread to execute next instruction, need little space
Java stack threads (private) : memory model of Java thread execution method, a thread corresponding to a stack, used to store the private data of a Java method, its life cycle and thread is consistent, as long as the thread one end of the stack will release, don't need to waste recycling,
Local method stack threads (private) : similar to the Java stack, local method (native method), is the early use of C/C + + implementation methods also have a stack, the Execution Engine executes load local method library
Java heap threads (common) : store those for address is bigger, create data with higher cost, we create the object instance,
Method area (thread of) : all of the fields and methods of the class bytecode, and some special methods such as constructor, interface code is also in this definition, in a nutshell, all the methods defined the information is stored in the area, is the class loader stored information of class files, static variables + constant + class information (constructor/interface definition) + runtime constants are pool area,
Everyone can see that the mentioned threads inside the private and Shared by thread, refers to a new thread for each, will create a program technology, Java and native methods stack, once the end of the thread, these will be destroyed, and the Java heap and method of area only when all the threads are performed will be destroyed,
3. The Java stack memory model
Stack is a data structure of first in first out, within the Java stack, can be carried for each to the Java method will create a stack frame, placed in the stack, stack will emerge when the stack frame has been completed, continue to implement the outermost layer of the stack frame, until all the stack frame is out of the stack, a thread is completed,
For each stack frame, there are four parts, respectively is local variables, the operand stack, dynamic link and the return address;

Local variables: record the current method parameters and local variables defined inside the
The operand stack: record the current operand
Dynamic link: said calls to the method of internal, not in the class loading stage or directly determine when used for the first time, determined by each invocation method reference, called dynamic link
The return address: method over, return to the upper method and restoring the upper information needed for the execution state
4. Introduce the Java heap
In order to be able to better to garbage collection of objects, Java heap for the generational design, so that we can convenient to deal with survival time of different objects, divided into two parts:

Young generation: used to hold the new creation of objects, the young generation is divided into two parts: the Eden area (Eden space) and survivors area (Survivor pace), all classes are in Eden area, by new survival area, there are two: 0 area (space) Survivor 0 and 1 area (Survivor 1 space), as the garden of Eden space is used up, the program also need to create the object, the JVM garbage collector are in the garden of Eden area for garbage collection (a Minor GC), will be the garden of Eden area will no longer be referenced by other objects in the object for destruction, then the remaining object moves to survive in the garden of Eden 0, if surviving 0 area also is full, again on the garbage collection, and then moved to the district 1, if it's full, go into old age,
Old s: used to store objects are linked to long-term, new area after many GC still alive object moves to old district, if the old district is Full, so this time will produce MajorGC (FullGC), for old area clear memory, if the old area to perform a Full GC after the found object could not be saved, still can produce abnormal OOM OutOfMemoryError,
"5. Introduce method area
Although the Java virtual machine specification, described the method of area as a logical part of the Heap, but it has an alias called Non - Heap Heap (not), the purpose should be apart from the Java Heap, so this part I also on separate,
May everyone a name to this hear not many, but you probably heard permanent or persistent generation will be a little more, before permanent generation is the HotSpot JVM java8 the implementation of the method area, the name is also relatively young generation, and the old s their FullGC will be garbage collected to eternal life, and
After java8, permanent generation was replaced, switch to the meta space area, the nature is the implementation of the JVM specification method, the difference is that the metadata area is not in a virtual machine, it USES the local physical memory, in this way, size also is less susceptible to limit the method, and the permanent generation in a virtual machine, the permanent generation logic structure belongs to the heap, but physically do not belong to the heap, the heap size=new generation + old s, metadata area may also be OutOfMemory exceptions occur,
The dynamic extension of metadata area, the default - XX: MetaspaceSize value is 21 MB of high water level, once touched, Full GC will be triggered and uninstall useless class (class corresponding to the class loader no longer survival), and high water level will be reset, the new value of the high water level depends on the dimension GC after release, if the release of the space is little, this is a high water level rise, if there is excessive release of space, the high water level is reduced,
Postscript
1. Why jdk1.8 metadata area to replace the permanent generation?
First of all, let's take a look at the official explanation: remove the permanent generation is to merge the HotSpot JVM and more about JRockit over VM efforts, because there is no permanent generation more about JRockit over, don't need to configure the permanent generation;
In addition, I think there are other consideration: remove the permanent generation after generational GC can reduce complexity, without considering the permanent generation GC; Don't have to specify PermSize, specified is too small easy to cause permanent generation OOM
2. Given the dynamic linking, and the static link?
Resolution calls must be a static process, is completely determined at compile time, in the phase of class loading will involve symbols referenced into can determine the direct reference, not to delay until run time to complete, that is static link in Java,
Constants are in the Class file references, there is a large number of symbols in the bytecode method calls in instruction as a symbolic reference to the method as the parameters in constant pool, these symbols refer to part of the Class loading stage (parsing) or translate when I was first used to direct reference data (pointing to the stored address pointer or a handle, etc.), this transformation is called static link, but on the contrary, the other part during the run into direct reference, is called dynamic link,
Popular point is at the stage of class loading can determine method calls, is static link, if want to determine at run time, that is dynamically linked, through such features can we realize polymorphic,
Later we will continue to write a few JVM class loading, and master relevant knowledge, please look oh, if I have write wrong place, welcome to correct me, let us in the Internet the architect road more walk more far, thanks for your company, thank you ~

CodePudding user response:

This should be tidy,
Send a card to share with everyone,

CodePudding user response:

The stack is advanced, after you have a slip of the pen

CodePudding user response:


This kind of post is generally going to see the bottom

CodePudding user response:

CodePudding user response:

Small sprout new said  good meng force while finished but many don't understand

CodePudding user response:

Throughout all talking about the JVM memory structure, this is not the memory model, it is suggested that the building to see what is the JMM

CodePudding user response:

After stack data structure is advanced out???????
  • Related