Five of the JVM memory area is divided Detailed and fast Literacy

First, the rapid literacy

1. JVM is what

  JVM is Java Virtual Machine acronym, that we often referred to the Java Virtual Machine. The virtual machine is an abstract computer, complete with its own hardware architecture, such as processors, stacks, and specific understanding of what we do. Now we just need to know that you want to run Java file, you must first pass a call javac compiler, the code is compiled into class files, and then interpreted by the JVM class files into machine code for each platform can be identified, and ultimately achieve cross-platform run code .
image.png

2. The relationship between the JDK, JRE, JVM

  • JDK: called the Java Development Kit, java development kit for the Chinese, that is, all things related to the java are included in it, such as the operating environment JRE, java core code, JVM, and so on.
  • JRE: called the Java Runtime Environment, Chinese as java runtime environment that you want to run java file must have java environment job, jre is to provide such an environment.
  • JVM: As already mentioned JVM, which is the core part of java.
    Simply use a diagram to understand the relationship between these three:
    FIG one: the relationship between the JDK, JRE, JVM

3. jvm composition of

Figure 2: Composition of the JVM FIG.
  Jvm students do not understand after seeing this picture might be a bit ignorant force, but that's okay, just want you to put this picture jvm understand there are three very important content, how 1.java code execution? 2. How memory management? 3. How thread resource use? There is the impression to head, with questions to learn.

4. Run the java process probably file

  Want to run java source files, you must go through javac compiler into .class file, that is, bytecode files. Jvm then the interpreter interpreted into machine code on a particular machine. Interpreter on each machine is not the same, we often use is windows and linux system, which is the reason why the java platform can cross. When running a program from the beginning, began to instantiate a virtual machine, running multiple programs will have multiple virtual machine instances, the program quit or shut down, the virtual machine instance will also die, data between multiple virtual machines is not shared.

Two, JVM run-time data area

1. Composition runtime data area

  Virtual Machine in the implementation of java program, will divide their own memory management for several regions, each with its own use, and create time and time destroy not the same. In the memory area of ​​the main program runs it can be divided into five, namely: the method area, stack, stack virtual machine, native method stacks, the program counter. Can be described by the following diagram:
Java virtual machine runtime data area

2. Java heap

  Java heap is the largest piece of memory java virtual machine management is shared by all threads of memory area. The sole purpose of existence is to store the object instance, almost all object instances are here allocated memory. But now with the continuous development of technology, not all object instances are allocated on the heap memory, there may also be allocated on the stack. Due to the large footprint, and store a variety of instances of objects, so the main management of garbage collection java virtual machine is in this area, after detailed garbage collection method will be mentioned. JVM specification stack may be in a predetermined discontinuous physical memory space, as long as can be logically contiguous. And it can be extended by -Xmx and -Xms heap memory size, if there is not enough memory on the heap allocation for the instance, and when the heap can not be extended, will be reported OutOfMemoryError exception.

3 method area

As with the Java heap, the method area is shared by the various threads of memory area, this area is used to store information class (class name, field information, method information), code static variables, constants, and compiler . JVM specification does not distinguish between heap and method area, only the zone method is described as part of the logic stack, but it has a non-alias called stack (Non-Heap), the purpose is to separate from the Java heap. According to carve on behalf of recycled garbage collection mechanism of thought, if developed in the HotSpot virtual machine, the method area may be called "permanent generation" (but can be understood, but the essence is not the same), garbage collection heap in Java divided into a section called permanent generations, this area used to implement the method area, such HotSpot garbage collector can manage the Java heap like this as part of memory management, without having to develop a special memory manager for the method area.

Runtime constant pool

Runtime constant pool is a part of the method of the area, in addition to the class file versions classes, fields, methods, and interface description information, the information is also a constant pool for a variety of literal generated during compilation and storage symbol references, this part will enter the zone in the operating method of the class loading time constant pool. Java virtual machine formats for each part of the Class file (naturally including the constant pool) has strict rules, each byte is used to store data which must meet the requirements of the specification, this will be recognized by the virtual machine, loading and execution.

4. Program Counter

Although a large area in the figure above the program counter, but it's actually a smaller memory space, can be seen as the current thread line number indicator byte code executed. Next the bytecode interpreter when doing work, where a, which is determined by. We all know that in the case of multi-threaded, CPU in the thread of execution is achieved by alternately switching threads, that is to say a CPU processor (single-core hypothesis) have a thread of instructions will only be executed, so in order to thread switch after the execution can be restored to the correct position, each thread should have a separate program counter, between the threads independently of each other, independent store, we call this type of memory area for the "thread private" memory. Obviously, the program counter is thread private. If the thread is executing a java method, program counter records the address of the virtual machine bytecode instruction being executed; Native method if execution, a program counter value is null record (Undefined), this memory area is only java It does not provide for a situation in any area OutOfMemoryError java virtual machine specification.

5. Java Virtual Machine stack

We often put java memory rough stack is divided into two parts this part of the stack, heap and stack, Java virtual machine, or a virtual machine stack, local variables table section. Like the program counter, the virtual machine thread stack is private, its life cycle with the same thread. In each method are performed simultaneously to create a stack frame (Stack Frame), each stack frame a corresponding method is invoked, the stack frame for storing local variable table, the operand stack, the dynamic information list, the method exports. Each method performed from the beginning to the end of a stack frame corresponds to a virtual machine from the stack to the stack of a process stack.

  • Local variable table: As the name suggests, he is used to store a local variable in the method (including non-static variable life and function parameter in a method), the basic data types, direct deposit value for a reference type variable, which stores pointers object. Since only the basic data types of variables stored reference type of address and return values of these types of space required for known and fixed, so that when entering a method, this method requires much local variables allocated in the stack frame space is fully determined during the method of operation will not change the size of the local variable table.
  • Constant pool reference point to Run: inevitably used during the execution of the process constants defined in the class, thus stored to a stack frame points to a reference runtime constant pool.
  • Methods return address: After the end of the implementation of a method call to return to its place before, so you need to save a stack frame method return address.

6. The native method stacks

Native method stacks and stacks of virtual machine is very similar, but the difference is the virtual machine as a virtual machine implementation of java stack method services, native method stacks of virtual machines are Native method service. Some virtual machine and does not distinguish between native method stacks and stack virtual machines, such as Sun HotSpot virtual machine directly to the two combined.

7. Use a picture summary

JVM memory summary

 

The original address: https: //www.cnblogs.com/chaogu94/p/12529692.html

Guess you like

Origin www.cnblogs.com/fuqiang-terry/p/12529716.html