“This is the second day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

Introduction: I have recently read an in-depth understanding of the JVM, so I would like to record some knowledge here.

1. An overview of the

Due to the virtual machine automatic memory management mechanism, Java programs are not prone to memory leakage and overflow problems, but once memory leakage and overflow problems occur, do not know how to use the VIRTUAL machine memory, troubleshooting will be a very headache.

2. Run time data area

At runtime, Java programs divide the memory they manage into data fields. The Java virtual machine contains the following runtime areas:

2.1 Program counter

A program counter is a thread’s private memory. Each thread has an independent program counter, which can be regarded as a line number indicator of the bytecode executed by the current thread and a control flow indicator. Branch, loop, jump, exception handling, thread recovery and other basic functions are realized by this counter.

2.2 Java Virtual Machine Stack

The virtual stack is also thread-private and has the same lifetime as a thread. The virtual machine stack mainly describes the threading model of Java method execution. Each method creates a stack frame in the virtual machine stack at the time of execution, and the process from the beginning of execution to the end of execution of each method corresponds to the process of the stack frame in the virtual machine stack.

2.3 Local method stack

The local method stack is similar to the virtual machine stack, except that the virtual machine stack serves Java methods (bytecodes) while the local method stack serves Native methods (Native methods).

2.4 Java heap

The Java heap is a model shared by all threads, created when the virtual machine is started, and its sole purpose is to hold object instances. The Java heap can be in a physically discontinuous region, but logically it should be treated as continuous.

2.5 method area

The method area is also an area of memory shared by threads that stores loaded type information, constants, static variables, just-in-time compiled code cache, and so on. Method area garbage collection is mainly for constant pool collection and type offloading.

2.6 Runtime constant pool

The runtime constant pool is the part of the method area that holds various literal and symbolic references generated at compile time and is stored in the runtime constant pool after the class is loaded. In addition, the runtime constant pool is dynamic, and some constants can be put into the runtime constant pool at run time.

conclusion

This tutorial provides an overview of Java’s runtime data areas, where program counters and the local method stack, as well as the Java virtual machine stack, are owned independently by each thread, while method areas and the heap are memory areas shared by all threads.