[Learning] 2.Java virtual machine JVM run-time data area

Source: Public Number: ape Valley

Here, we first aside, I believe we often asked during an interview on Java memory model , I would often ask this question in the interview others. However, I will make often embarrassing, I also voice hardly ever, the interviewer will "recite" period (Java Virtual Machine is a heap, the method area, a virtual machine stack, it it it ...), but also to estimate heart Fortunately, before proudly wanted Geti of online search is too, well prepared. Whenever this time, I do not have the heart to break, because "recite" a really smooth too!

This is no wonder the interview, first of all knowledge aspects of the Java Virtual Machine, a program for senior ape, work in contact with something positive Java virtual machine much. Secondly, we have followed this good going on, online search a Java memory model, the first page is a large degree of your mother push introduce Java runtime data area, played a certain misleading, capital of embarrassment.

Benpian will give you a small partner to detail make up the Java run-time data area, Java memory model is the focus inside a virtual machine, the back will be introduced individually drawn one.

1. Introduction runtime data area

The required program memory space to run, some are not able to determine at compile time, have to be created in the system based on the actual operating conditions dynamically at runtime. Java virtual machine memory division during the execution of the Java program in which it will manage for a number of different data areas. These regions have their own purposes, and the time of creation and destruction, some areas along with the virtual machine process exists, and some regions are dependent on start and end users to create and destroy threads while.

JVM run-time data area .png

As shown, the heap and method area is shared by all threads in public areas, heap and method area occupied by the JVM memory space is responsible for the management of the memory allocation within the region by HotSpot memory management module maintenance, the working memory is released by the garbage collector automatically. VM stack, native method stacks, the program counter is a private area of ​​threads, each thread is associated with unique stack and program counter, and only to execute the program using their own share of stack space program and a calculator.

2. heap (Heap)

For most applications, Java heap (Java Heap) is the largest piece of memory in the Java virtual machine management . The heap is available to each thread shared run-time memory area, is created when the virtual machine starts. The sole purpose of this memory area is stored object instance, almost all object instances are here to allocate memory. This is described in the Java Virtual Machine Specification is: all object instances and array objects must be allocated on the heap. But with the JIT compiler development with escape analysis technology matures, distribution on the stack, scalar replacement optimization techniques will result in some subtle changes, all objects are allocated on the heap gradually becomes less "absolutely" the .

Java heap capacity can be fixed or dynamically expand as the demand for program execution and automatically shrink when not needed too much space. Java stack may be in a discontinuous physical memory space, as long as can be logically contiguous. If there is no complete examples in the heap memory allocation, and the stack can no longer expand, it will throw an OutOfMemoryError.

Java heap is the main area managed by the garbage collector, and therefore often also called "GC heap" (Garbage Collected Heap) . From the perspective of memory recovery point of view, due to the current collector is basically using generational collection algorithm , Java Virtual Machine heap is divided into the old and the new generation's. Wherein, the new generation area has been divided into Eden, and two Survivor areas the same size (From Survivor, To Survivor). By default, Java virtual machine taken is a dynamic allocation strategy (JVM parameter -XX: + UsePSAdaptiveSurvivorSizePolicy), according to the generation target rate, and the use of Survivor area, and dynamically adjusting the proportion of Eden region Survivor region. May be adjusted by parameters (SurvivorRatio) this ratio, SurvivorRatio this parameter is the ratio of the capacity of the new generation area and Survivor Eden region, the default is 8, representative of Eden: Survivor = 8: 1.

JVM heap generational .png

Is it possible there are two objects share some memory of the accident?

When the new call instruction, the division will be a memory to store an object in Eden. Since the heap space is shared by the threads, thus directly where space is designated side needs to be synchronized it is. Otherwise, it will be possible for some accident two shared memory objects appear. The solution is, Java heap may draw multiple threads to allocate a buffer of private TLAB (the Thread Local Allocation Buffer, the corresponding virtual machine parameters -XX: + UseTLAB, enabled by default).

Specifically, each thread can apply to the Java virtual machine for a contiguous memory, such as 2048 bytes, as a thread private TLAB. This operation requires locking, threads need to maintain two pointers (in fact probably more, but two important it), pointing to a starting position in TLAB free memory, and a point at the end of the TLAB. The next new instruction pointer adder can directly (bump the pointer), it was known to achieve a pointer collisions, i.e., the spare memory location pointer points plus the number of bytes requested. If the value of the pointer after the addition of free memory is still less than or equal to the pointer points to the end, it represents the allocation succeeds. Otherwise, TLAB did not have enough space to meet this new operation. This time, the current thread will need to re-apply for a new TLAB.

3. The method of region (Method Area)

The method is the same as the thread stack area and shared, created when the virtual machine is powered on, the method area may be viewed as a logical part of the heap, but it has an alias called Non-Heap (non-heap), and the aim should be to Java heap differentiate.

The method is similar to area code memory region after the conventional compiler, which stores configuration information of each class , such as:

  • Constant pool
  • area
  • Methods Data
  • Bytecode methods and constructors of
  • Class, instance, method used when the special initialization of the interface

NOTE : "in-depth understanding of the Java Virtual Machine" in the category information will be summarized for storing virtual machine has been loaded, constants, static variables, the time compiler to compile the code and other data method area.

Java Virtual Machine Specification limitation on the method very relaxed area, in addition to and do not require continuous as the Java heap memory can be selected and fixed size or may be expanded, but also can choose not to implement garbage collection. Garbage collection target this area is mainly for recycling and unloading of constant pool types.

4. The program counter (Program Counter Register)

Java virtual machine can support multiple execution threads, each thread has its own Java virtual machine program counter. At any time, a Java virtual machine thread will execute the code of a method, this method is being called the current thread execution method of this thread (current methon). If this method is not native, address bytecode instructions that saved the program counter is a Java Virtual Machine being executed. If the method is native method, then the program counter value is null (undefined). Program counter storage capacity should be at least the value of a data type returnAddress or a pointer associated with the local platform.

The program counter is a smaller memory space, it can be seen as an indicator byte-code line number of the current thread of execution. This memory area is the only one not provide for any situation in the region OutOfMemoryError Java Virtual Machine Specification.

The stack virtual machine (VM Stack)

Each Java virtual machine thread has its own private Java virtual machine stack, its life cycle and the same thread. Is a virtual machine stack Java memory model described method performed: Each method creates a stack frame (stack frame) while performing information storage for local variables table, operand stack, dynamic links, and so on for export . Each method until the completion of the execution procedure is called, a corresponding stack frame with a push to the stack in a virtual machine process stack.

Java virtual machine stacks the following exceptions may occur:

  • If the thread stack allocation request exceeds the capacity of the largest Java Virtual Machine stack permitted capacity, Java virtual machine will throw a StackOverflowError exception.
  • If the Java Virtual Machine stack can be dynamically expanded, and can not apply it tries to extend the time to have enough memory, or there is not enough memory area when creating a new thread to create a corresponding virtual machine stack, it will throw a Java Virtual Machine OutOfMemoryError abnormal

6. A native method stacks (Native Method Stack)

Native method stacks and stack virtual machine's role is very similar, but the difference between them is the virtual machine to virtual machine execution stack Java method (ie bytecode) service, and the local stack method was to use a virtual machine to the native method service.

Java Virtual Machine Specification allows native method stacks implemented as a fixed size, or calculated dynamically expand and contract in accordance with. If a fixed-size native method stacks, then every thread of native method stacks capacity can be independently selected when creating the stack.

Like the virtual machine stack, the stack area native method can also throw StackOverflowError and an OutOfMemoryError.

7. Extended knowledge

7.1 distribution and escape analysis on the stack

The basic idea is allocated in the stack so: Analysis of the local variable scope is limited to the internal method, the JVM stack frame allocated directly in the object space, avoid allocating the heap. This analysis is called escape analysis (also called evolution analysis) , the assignment of the stack frame is called allocated on the stack .

The aim is to reduce the number of collecting the new generation, and indirectly improve JVM performance. A virtual machine is allowed to escape analysis stack switch configuration From Sun Java 6u23, HotSpot escape analysis enabled by default.

7.2 stack frame

Stack frames are used to support virtual machine data structures and methods for performing the method calls, which is a virtual machine runtime data area of 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 stack frame corresponds to a process to push the stack from the stack in a virtual machine inside.

When the compiler code, stack frame requires much local variable table, how deep the operand stack have been completely determined, and written into the Code attribute in the method table. So a stack frame how much memory needs to be allocated, will not receive the impact of the program run variable data, but only depending on the particular virtual machine implementation.

Chain method calls a thread can be long, many methods are simultaneously being executed. For the execution engine, in active threads, only the top of the stack stack frame is valid, it called the current stack frame (Current Stack Frame), and methods associated with the stack frame called the current method (Current Method) . All bytecode instruction execution engine is operated only for the current stack frame. Conceptual structure stack frame is as follows:
Conceptual structure of the stack frame .png

8. FIG brain runtime data area

Run-time data area .png

HD, no code, a complete mind map can be private letter or a message to inform Oh! ! !

Guess you like

Origin www.cnblogs.com/alterem/p/11567111.html