In-depth understanding of the Java Virtual Machine (a) - Java memory area

Understand Java memory area

Java virtual machine during operation of the Java program, will total memory area is divided into a plurality of memory data, comprising: a program counter, stack virtual machine, native method stacks, heap, the method area. Each memory data area features, characteristics vary.

1. Program Counter

1.1 program counter understand

The program counter is a small memory footprint region, belonging to the thread private, it can be seen as the row number designator bytecode execution.

Role of the program counter 1.2

● bytecode interpreter work is to get the next bytecode instruction to be executed by changing the value of the program counter.

● program counter thread private belongs, so that each thread has its own counter, when a thread context switch can be realized by a thread resumes execution counter value is saved.

1.3 Program Counter features

● thread private and small memory footprint.

● Java memory area is only an OutOfMemoryError of a region does not appear.

● Java thread execution method stored bytecode instruction address counter; Native method is executed, the counter value is null.

2. VM stack

2.1 understand the role of a virtual machine and stack

VM stack belong thread private, life cycle and thread consistency. Virtual machine stack Java memory model described method performed: Each method generates a Java virtual machine stack the stack frame when executed, the local variable table for storing information, the operand stack, dynamic link, the method exports. From a Java method calls to finished, it means a stack frame onto the stack in the virtual machine stack and the stack.

2.2 virtual machine stack features

● a desired local variable table memory space allocation at compile been completed, Java method is executed, the local variable table space required in the stack frame is determined, it will not change at.

● the virtual machine stack two errors may occur:

  • 1.StackOverFlowError: When the depth is greater than the maximum depth of virtual machines allowed stack frame, and the virtual machine when the stack is not dynamic expansion, it will throw this error.
  • 2.OutOfMemoryError: When the virtual machine to allow dynamic expansion, while the virtual machine is not enough memory allocated to stack stack frame, throw this exception.

3. The native method stacks

3.1 native method stacks of understanding

The method described is a local stack memory model native method, its stack with the virtual machine role is very similar.

3.2 features native method stacks

● virtual machine stack as native method will create a stack frame in native method stacks at the time of execution.

● HotSpot virtual machine to virtual machine native method stacks and stacks combined.

● native method stacks can also throw StackOverFlowError, OutOfMemoryError exception.

4. Java heap

4.1 heap of understanding and action

The sole purpose is to store the Java heap object instance, the vast majority of object instances are here to allocate memory.

4.2 heap of features

● Java heap memory area is the largest memory footprint of an area.

● heap is the main area of ​​garbage collection, it is also referred to as "GC heap."

● due to the need of memory recovery, Java heap further subdivided into the new generation and the old era, the Cenozoic can be divided into: Eden, From Survivor, To Survivor.

● heap is shared by the threads that all threads can access heap of data.

● stack may not be in contiguous physical memory, as long as can be logically continuous.

● fixed heap size can also be extended, but lack of heap memory allocation, and the heap can not be extended again, will throw an OutOfMemoryError.

The method area

5.1 understand the role and methods zone

● loading area for storing method through classes, constants, variables static, time compiler compiled code.

● Method region is described as a logical part of the stack, and because the garbage collection process zone harsh conditions (i.e., the memory area can not be easily recycled method), it is "permanent generation" argument.

Features 5.2 Method zone

● method area is shared by the threads.

● less frequent garbage collection behavior in the method area.

5.3 runtime constant pool of understanding

Runtime constant pool area is part of the method, class file type public static final constants at compile time is stored in the method area. The method of addition may be dynamically extended area, such as the String.intern () method may speak into a string constant pool at runtime.

6. Direct Memory

Direct Memory understanding and action

● Direct Memory does not belong to the composition of Java memory region, but also can be used by Java programs.

● NIO introduced memory and cache based on channel I / O mode, it can be directly allocated by a memory area outside the Java native method, and the object operated by DirectByteBuffer Java heap, the efficiency can be improved in some scenarios.

● Direct Memory shortage will throw an OutOfMemoryError.

Guess you like

Origin juejin.im/post/5d1874c8f265da1b94215f65