This is the 11th day of my participation in the More text Challenge. For more details, see more text Challenge

Anyone with more than three years of development experience will have one sentence on their resume: they know/are familiar with the JVM (memory structure) and have some understanding of garbage collection mechanisms.

But most people don’t know what they’re talking about, or aren’t prepared enough, or just make things up, at least by memorizing concepts, even if the interviewer doesn’t.

The following articles will focus on the JVM, the JMM, garbage collection algorithms, garbage collectors, and tuning.

The first step in the long March.

Today we’re going to look at the MEMORY structure of the JVM, albeit conceptually; However, by virtue of theoretical knowledge, combined with daily development work, accumulated over a long period of time, the usual programming has a wide impact.

Here I use JDK8 as an example to describe the memory structure of the JVM. If you want to learn more about it, you can actually buy a book to read, “Understanding the Java Virtual Machine in Depth”.

Draw a picture, look at it first!

In general, there are two types: thread-private and thread-shared. Take a look below.

Thread private

Program Counter Register

An indicator of the bytecode line number to be executed by a thread, changing the value of the counter to select the next bytecode instruction to be executed, counting only Java methods.

Since each thread has its own line number, and since program counters take up so little memory, it makes sense to use separate counters for each thread. This is less stressful, and is much faster because each thread has a private counter.

This is why program technologists are thread private.

Vm Stack (Feature: last in, first out)

The memory model for Java method execution, consisting of stack frames.

The stack frame is the basic data structure during the operation of the method. Each stack frame mainly contains local variables, operand stack, dynamic connection, return address, and so on.

The virtual machine stack stores the stack frames of the methods executed in the thread. The stack frames are destroyed only after the methods are executed.

Here is another important and frequently asked point: How to cause StackOverflowError, OutOfMemoryError, and why?

That’s a question that we’ll talk about in a future article, or you can do your own research.

Local method stack

The function of the local method stack is similar to that of the virtual machine stack. The virtual machine stack provides services for Java methods, while the local method stack serves Native methods.

Threads share

MetaSpace

Used to store loaded class information, constants, static variables, and so on.

Prior to JDK1.8, this data was stored in the method area, known as PermGen.

Unlike the permanent generation that consumes the Memory of the Java VM, the meta space directly uses the local memory storage.

There are many benefits of changing into a meta-space, such as:

  1. When a string constant pool is stored in a permanent generation, it is prone to memory overflow and performance problems.

  2. The size of the class information is difficult to determine because the permanent generation uses Java virtual machine memory, so it is difficult to specify the permanent generation memory.

  3. As mentioned in the book Understanding the Java Virtual Machine, other virtual machines (such as JRockit, IBM J9) have no permanent generation. This means that the Java Virtual Machine (HotSpot) cannot integrate with other virtual machines; This problem is solved when the meta-space is used.

Heap (Heap)

The Java heap is an area of memory shared by all threads and is the largest chunk of memory managed in the Java virtual Machine.

This is where almost all object instances are stored, and the garbage collection mechanism that we’ll talk about later is also for the heap, because the heap is the primary area that the garbage collector manages.

From the perspective of the garbage collector, the heap can also be divided into the new generation (Eden, From Survivor, To Survivor) and the old generation.

I’ll talk more about this later in the garbage collection article, but you can also check out the books.