• 周四. 10 月 3rd, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

JAVA memory area

King Wang

1 月 3, 2022

Personal blog address :https://alexaccele.github.io/

Java Memory area

Java Virtual machine is executing Java In the process of the program, the memory it manages will be divided into several different data areas .

  1. Program counter ( Thread isolation )
  2. Java Virtual machine stack ( Thread isolation )
  3. Native Method Stack
  4. Java Pile up ( Thread sharing )
  5. Method area ( Thread sharing )
  6. Runtime constant pool ( Part of the method area )

Program counter

Program counter (Program Counter Register) It’s a small amount of memory , Is the line number indicator of the bytecode being executed by the current thread . The next bytecode instruction to be executed is selected by the value of the counter , Branch 、 loop 、 Jump 、 exception handling 、 Basic functions such as thread recovery rely on this counter .

At any given moment , A processor ( It is a kernel for a multicore processor ) Will only execute an instruction in a thread . In order to recover to the correct execution position after thread switching , Each thread needs to have a separate program counter , Threads are private .

Is the only one in Java Nothing is specified in the virtual machine specification OutOfMemoryError Area of situation .

Java Virtual machine stack

Java The virtual machine stack has the same lifetime as threads .
Java A stack frame is created when each method is executed in (Stack Frame) be used for Store local variable table The stack of operands Dynamic links Methods the export Etc . Every method from call to execution , Corresponding to a stack frame in the virtual machine stack stack stack to stack out of the process .

Local variable table All kinds of basic data types known at compile time are stored (boolean、byte、char、short、int、float、long、double)、 Object reference (reference type , Not necessarily the object itself , May be a reference pointer to the starting address of the object , It can also be a handle to a representative object or other location related to the object ) and returnAddress type ( Points to the address of a bytecode instruction ).

64 A length of long and double Type of data is occupied 2 The local variable space (Slot), The rest of the data type values occupy 1 individual .

The memory space required for local variables is allocated during compilation , The local variometer size does not change during method run .

If the stack depth of the thread request is greater than the depth allowed by the virtual machine , Will throw out StackOverflowError abnormal ;
If the virtual machine can be dynamically extended , If you can’t request enough memory while expanding , It will be thrown out. OutOfMemoryError abnormal .

Native Method Stack

The effect is very similar to the virtual machine stack , The virtual machine stack executes for the virtual machine Java Method ( Bytecode ) service , The local method stack is used by the virtual machine Native Method service .

Same as virtual machine stack , It will also be thrown out StackOverflowError Exceptions and OutOfMemoryError abnormal .

Java Pile up

Java Pile up (Java Heap) yes Java The largest chunk of memory managed by a virtual machine , Is a memory area shared by all threads . The sole purpose of this memory area is to hold object instances , Almost all Object instances are all allocated memory here .

Java The specification of the virtual machine specification ,Java The heap can be in a physically discontinuous memory space , As long as it’s logically continuous .

If there is no memory in the heap to complete the instance allocation , And the heap cannot be extended , It will be thrown out. OutOfMemoryError abnormal .

Method area

Method area is the memory area shared by each thread , Used to store class information loaded by virtual machine , Constant , Static variables , Real time compiler compiled code and other data .

Sometimes the method area is also called “ Forever ”(Permanent Generation). But it’s not really permanent . stay JDK1.7 Of HotSpot in , The string constant pool that was originally placed in the permanent generation has been moved out .

The target of this area is to recycle the constant pool and unload the type .

When the method area cannot meet the memory allocation requirements , Will throw out OutOfMemoryError abnormal .

Runtime constant pool

Runtime constant pool (Runtime Constant Pool) Is part of the method area . Various literal quantities and coincident references generated at compile time , The runtime constant pool that will enter the method area after the class is loaded .

When the constant pool can no longer request memory, it will throw OutOfMemoryError abnormal .

Direct memory

Direct memory (Direct Memory) It’s not part of the virtual machine runtime datazone , It’s the local direct memory ( Physical memory ), When the local computer runs out of memory , Causes the dynamic extension to fail and still throws OutOfMemoryError abnormal .

This article references from 《 In depth understanding of Java virtual machine 》 The second edition

发表回复