This is the first article I participated in beginners’ introduction

As is known to all, the Java virtual machine in the process of executing the Java program it managed by the memory is divided into a number of different data areas, these areas have their USES, and the time of creation and destruction, some areas with the start of the virtual machine process, some area is dependent on user thread start and technology to set up and destroyed.

It is mainly divided into: method area, heap, stack (virtual machine stack, local method stack), program counter

2. Program counter

Because Java virtual machine multithreading is implemented by switching threads and allocating processor execution time, at any given moment, only one processor will execute instructions in one thread. Each thread has its own program counter so that it can be switched back to its original execution position.

This memory region is the only one that does not specify any memory overflow (OutOfMemoryError) cases.

3. Java Virtual machine stack

When each method is executed, the Java VIRTUAL machine synchronously creates a stack frame to store information such as local variable table, operand stack, dynamic linkage, method exit, etc. (The process of each method from invocation to completion of execution corresponds to the process of a stack frame in the virtual machine stack.)

The memory space required by the local variable table is allocated at compile time, and when entering a method, how much local variable space the method needs to allocate in the stack frame is completely determined

Memory overflow:

  1. A StackOverflowEroor exception is thrown if the stack depth requested by the thread is greater than the depth allowed by the virtual machine
  2. If the Java virtual machine stack can be expanded, OutOfMemoryError will be raised if sufficient memory cannot be allocated during stack expansion.

4. Local method stack

5, the Java heap

The Java heap is an area of memory that is shared by all threads and is created at virtual machine startup. The sole purpose of this memory area is to hold object instances

Object instance: the object that comes out of new, the object reference, and the address where the instance is stored

The Java heap is an area of memory managed by the garbage collector, and the “new generation,” “old age,” “permanent strip,” “Eden space,” “From Survivor space,” and “To Survivor” partitions in the Java heap are just common features or design styles of some garbage collectors. Rather than the inherent memory layout of a Java virtual machine implementation.

Most current virtual machines are implemented as extensible (with arguments -xmx and -xms). If the Java object instance allocation is not complete and the heap can no longer be expanded, the Java virtual machine will throw OutOfMemoryError

6. Method area

Features:

  • (1) is created at JVM startup, and its actual physical memory space is as discontinuous as the Java heap.
  • (2) The size of the method area, like the heap space, can be fixed or expanded.
  • (3) The size of the method area determines how many classes the system can hold. If the system defines too many classes and causes the method area to overflow, the VIRTUAL machine will also throw an overflow error
  • Before (Java. Lang. Java8 OutOfMemoryError: PermGen space) or after java8 (Java. Lang. OutOfMemoryError: Metaspace)
  • (4) Shutting down the JVM frees memory in this area

HotSpot method area evolution: before Jdk7, method area implementation was persistent generation, Jdk8 started method area implementation using meta-space instead of persistent generation. The meta-space is not in the virtual machine’s memory, but uses local memory

6.1. Runtime constant pool

Runtime constant pool creation timing: The corresponding runtime constant pool is created after classes and interfaces are loaded into the VIRTUAL machine.

The runtime constant pool is dynamic, such as adding to the runtime constant pool using the String intern method.

When creating a run-time constant pool for a class or interface, the JVM throws OutOfMemoryError exceptions for stack, heap, and method area interactions if the amount of memory required to construct the run-time constant pool exceeds the maximum that the method area can provide