Java runtime data area

1. Program counter

1. What is a program counter

Line number indicator of the bytecode executed by the current thread

2. Features

Thread private: each thread has an independent program counter, which does not affect each other and is stored independently, which is convenient for thread switching

3. Working mechanism

The owning thread executes the Java method: record the address of the virtual machine bytecode instruction

The owning thread executes the native method: empty, there is no exception such as memory overflow, such as outofMemoryError

2. Java virtual machine stack

1. What is the Java virtual machine stack

Memory Model of Java Method Execution

2. Features

Thread private: each thread has a Java virtual machine stack, the thread ends, the stack frame also ends

3. Working mechanism

When a Java method is executed, a stack frame is created, which stores messages such as local variable tables, operation stacks, dynamic connections, and method exits. The method starts to execute the stack frame into the stack, and the method ends the stack frame out of the stack.

4. Supplement

1. Local variable table: basic data type, object reference type, returnAddress type

Basic data types: Boolean(1), byte(1), char(1), short(1), int(1), float(1), long(2), double(2)---local variables occupied space

Object reference type: reference type, a reference pointer to the starting address of the object\representing the handle of the object\representing the location related to the object

returnAddress: Points to the location of a bytecode instruction

2. The local variable table completes the allocation of local variable space at compile time. will not change during runtime

3. Two exceptions:

stackOverflowError: The stack depth requested by the thread is greater than the depth allowed by the virtual machine

OutOfMemoryError: The depth requested by the thread exceeds the depth of the virtual machine's dynamic expansion

3. Local method stack

1. What is the local method stack

Memory model of Java native method execution

2. Features

Same as Java virtual machine stack

3. Operating mechanism

The same as the Java virtual machine stack ( note that the native method stack here will still throw stackOverflowError and OutOfMemoryError exceptions. Only the program counter will not throw OutOfMemoryError exceptions )

4. Java heap

1. What is the Java heap

It is the largest memory area where object instances are stored in the Java virtual machine. It is also the main area managed by the garbage collector.

2. Features

  • Shared by all threads
  • Physical discontinuity, logical continuity

3. Operating mechanism

Created when the Java virtual machine starts, allocating memory for all object instances. When the heap memory cannot complete the instance allocation, an OutofMemory exception will be thrown. The thread finishes reclaiming memory.

5. Method area

1. What is the method area

Store class information, constants, static variables, and code data compiled by the just-in-time compiler that have been loaded by the virtual machine.

2. Features

  • Shared by all threads
  • a logical part of the heap
  • No need for contiguous memory space
  • Choose not to implement garbage collection

3. Operating mechanism

Mainly for the recycling of the constant pool and the unloading of types, when the memory allocation requirements cannot be met, an outofmemory exception is thrown

6. Runtime constant pool

1. What is the runtime constant pool

Stores various literals and symbolic references generated at compile time. is part of the method area

2. Features

  • There is no specification for storage, different providers can customize
  • Dynamic: new constant pools can also be added during runtime

3. Operating mechanism

The literal and symbolic references generated when the class is loaded are stored in the runtime constant pool of the method area. If the memory limit is exceeded, an outofmemory exception is thrown

7. Direct memory

1. What is direct memory

Off-heap memory allocated directly by native functions

2. Features

  • Does not belong to the virtual machine runtime data area
  • A memory area that does not belong to the Java virtual machine
  • Not limited by Java heap size
  • Limited by total native memory

3. Operating mechanism

Through the channel-buffer I/O method, the directbytebuffer of the Java heap is used as a direct memory reference. When the total memory of the machine is greater than the physical memory of the machine, an outofmemory exception will be thrown


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325820737&siteId=291194637