"In-depth understanding of the Java Virtual Machine" --Java memory area

Runtime data area: 

       Java virtual machine memory division during the execution of the Java program in which it will manage for a number of different data areas. As shown below:


Regional features:

1. The program counter (Program Counter Register)

  • Is a smaller memory space, it can be seen 当前线程所执行的字节码的行号指示器. When the bytecode interpreter is working by changing the value of the counter byte code to select a next instruction to be executed, a branch, looping, branching, exception handling, and other basic functions thread recovery relies on the counter to complete.
  • Since the multi-threaded Java Virtual Machine is in turn switched mode execution time and processor allocation by threads to achieve, at any time a determined, a processor (for multi-core processors is a kernel) will only execute a thread of instructions. therefore,为了线程切换后能恢复到正确的执行位置,每条线程都需要有一个独立的程序计数器,各条线程之间计数器互不影响,独立存储,我们称这类内存区域为“线程私有”的内存。
  • If the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed; if the method is being performed Native, this counter value is null (Undefined). This memory area is a 唯一一个在Java虚拟机规范中没有规定任何OutOfMemoryError情况的区域.

2.Java virtual machine stack (Java Virtual Machine Stacks)

  • VM stack are described Java方法执行的内存模型: each method creates a stack frame (Stack Frame) while performing the local variable table for storing information, the operand stack, dynamic link, the method exports. Each method until the completion of the execution procedure is called, a corresponding stack frame to push the stack in a virtual machine process stack.
  • 局部变量表Various types of data stored basic (boolean, byte, char, short, int, float, long, double) compile knowable, object reference (reference type, it is not equivalent to the object itself, may be a pointer to the starting address of the object reference pointers, it may be a point representing an object handle associated with this or other target position) and returnAddress type (address pointing to a byte code instruction).
  • The basic data types long and double of length 64-bit data occupies two local variable space (Slot), the remaining data occupies only one type. Desired local variable table memory space 编译期间完成分配, when entering a method, this method requires much space for local variables allocated in the frame is completely determined,在方法运行期间不会改变局部变量表的大小。

3. The native method stacks (Native Method Stack)

And virtual machine stacks the role is very similar, but the difference between them is the virtual machine execution stack for the Java Virtual Machine method (ie bytecode) services, native method stacks, the virtual machine to use the Native The method of service.

4.Java stack (Java Heap)

  • For most applications, Java heap memory is the biggest piece of the Java virtual machine management. Java heap is shared by all threads in a memory area is created when the virtual machine starts. This memory area 唯一目的就是存放对象实例, almost all of the object instances to allocate memory here.
  • Java heap garbage collection manages the main area, so often also called "GC heap" (Garbage Collected Heap), if we look from the perspective of memory recovery, due to the current collector basically adopted generational collection algorithm, so Java heap can also be broken down into: the old and the new generation's; there is little detail in the Eden space, From Survivor space, to Survivor space. Anyway division, has nothing to do with the storage of content, regardless of which area, are still stored object instances, further divided the purpose is to better recovery of memory, or faster memory allocation.
  • Under the Java Virtual Machine specification, Java heap may be in discontinuous physical memory space, as long as logically contiguous to, just like our disk space. When implementing either can be implemented as a fixed size, can be expanded, but the current mainstream virtual machines are in accordance with the scalability to achieve (by -Xms initialize the stack, -Xmx maximum heap space), if the heap memory allocation is not complete examples, and can not heap upon expansion, it will throw an OutOfMemoryError.

The method area (Method Area)

  • Like the Java heap, each thread is a shared memory area, which is used to store class information has been loaded in the virtual machine, constants, static variables, the time compiler to compile the code and other data . Although Java Virtual Machine Specification as described in the method area is a logical part of the heap, but it has an alias called Non-Heap (non-stack), the Java heap object should be distinguished.
  • 运行时常量池(Runtime Constant Pool) is part of the zone method, for 存放编译期生成的各种字面量和符号引用,这部分内容将在类加载后进入方法区的运行时常量池中存放. Another important feature of the runtime constant pool file relative to Class constant pool is equipped 动态性, Java language does not require constant necessarily only compile to produce, that is not preset the contents of Class file constant pool to enter the zone method often run the amount of the pool, during the run may be the new constants into the pool, take advantage of this feature is the developer intern is more than the String class () method.

6. Direct Memory (Direct Memory)

Not part of the virtual machine runtime data area, nor is it Java virtual machine memory area defined in the specification, but this memory is also used frequently, and may also lead to OutOfMemoryError exception. Clearly, the direct memory allocation native Java heap size is not limited, however, since it is memory, then certainly still be limited by the machine's total memory size and processor addressable space. When the server administrator configure the virtual machine parameters, based on general parameters -Xmx actual memory information, but often ignores the direct memory, so that the sum of each memory region is greater than the physical memory limitations (including the operating system level and physical limitations) causing an OutOfMemoryError occurs when the dynamic expansion.


Guess you like

Origin juejin.im/post/5d54f2976fb9a06b2c3288fc