Don’t confuse the Java memory region with the Java memory model any more. 😏

Small orange products must belong to the boutique. Make it a habit to like first and look later.

Last time we talked about the JVM’s class loading mechanism, mainly covering how to break the parent delegation mechanism. If you don’t understand the parent delegation mechanism, you can move on to the previous article.

There are many articles on the Web about Java regions and the memory model, but many people are still confused after reading them, mainly because they confuse the two and don’t care about the JDK version. So a lot of concepts, it’s very confusing.

So the following sections will focus on Java memory regions, covering the major run-time partitioning of regions and the responsibilities of each region. For articles on the Java memory model, see the MESI protocol,JMM, Common Threading methods, and more.

A list,

We all know that C language, as the big brother in the computer world, is developed for memory. For each object, we need to manually create it, open up memory, and destroy it. If we don’t destroy it, it may never be deleted.

But Java is not like this, it has a set of virtual machine automatic memory management mechanism, we do not need to manually create objects, open up memory, and even destroy, memory overflow and leakage is not easy. I don’t think it looks so good.

But it’s also a headache, and we’re completely confused about what’s inside. I have no idea what to do if something goes wrong.

So we’re going to learn how Java memory actually works and how the parts are divided.

Second, the overall framework

First, our JVM loads classes into memory, so there must be an area of memory in the JVM to hold the classes we write. Java has class objects, ordinary objects, local variables, method information, and so on, so the JVM divides the memory area:

Method area

The method area, like the heap, is an area of memory shared by threads to hold information about the class.

The method area, which only exists before JDK1.8, stores classes loaded from “.class “files, including class names, method information, field information, static variables, constants, and compiled code from the compiler. This is also known as a “permanent generation,” but the two are not equivalent, except that the generational collection theory of the garbage collector extends to the method area, allowing the garbage collector to manage memory as if it were the Java heap, eliminating the need to write code specifically for the method area.

Note: this is only useful for HotSpot virtual machines, for other virtual machines such as IBM J9, there is no concept of persistent generation.

We can set the size of -xx :MaxPermSize to control the upper limit of the method area. But there are pros and cons. Beneficial is it easier for us to put the memory occupied by the control program after start-up, disadvantages are once beyond the limit, can appear the Java. Lang. OutOfMemoryError: PermGen error. (Here is an interview question, line up excellent experience, pay attention to.)

Starting with JDK1.8, the name of this area was changed to Metaspace, which uses local memory directly. Compared to the pre-1.8 version, it requests memory directly, no longer running out of memory. If not, add server configuration. But you can’t scale indefinitely, so you can use -xx :MaxMetaspaceSize to control the maximum memory.

Here’s an example:

public class Test
{
    public static void main(String[] args)
     {
    }
}
Copy the code

We create a new Test class with an empty main method. When the class is loaded, its class-related information will be loaded into the method area, as shown in the following figure.

Iv. Program counter

It is a small chunk of memory that can be viewed as the line number of the bytecode executed by the current thread. Because Java virtual machine multithreading through the threads take turns to switch, take turns to get the resources of the processor to carry out, so you need to have a counter in the thread to record the execution of which line, easy to switch back, so each thread needs an independent program counter, they are independent of each other, do not affect each other.

Vernacular explanation:

This big winter, is lying happily watching drama, suddenly hungry, want to make a bubble noodles to eat, at this time is not to suspend, first bubble noodles? When the instant noodles are ready, come back and continue to watch. So the pause here is equal to the program counter.

Based on the above example, we add two lines of code:

public class Test { public static void main(String[] args) { A a=new A(); a.load(); }}Copy the code

When the JVM loads class information into memory, it actually uses its own bytecode execution engine to execute these bytecode instructions. The program counter is thread private, meaning that each thread has its own program counter that keeps track of which bytecode instructions were executed by the current thread.

Java virtual machine stack

The Java virtual machine stack is a data structure that describes the execution of Java methods. When each method is executed, a Stack Frame is created to store information about local variables, action stacks, action links, method exits, and so on.

The process of each method from being called to the completion of execution is actually a stack frame in the virtual machine stack from the process of loading and unloading.

For example, if the main thread executes the main() method, it creates a stack frame (containing the local variable A) and pushes it onto the main thread’s own Java virtual machine stack, as shown below:

The main thread then proceeds with the load method and the method of class A, which, if it has A local variable, creates another stack frame and pushes it into its own virtual machine stack, as shown below.

Local method stack

The local method stack is similar to the Java virtual machine stack except that the local method stack serves the Native methods used by the VIRTUAL machine, while the Java virtual machine stack serves the Java methods (that is, bytecode) executed by the virtual machine. The local method stack is also thread-private.

Many of the underlying APIS in the JDK, such as IO, NIO, network, etc., if you look at its source code, you will find that many places are called native modification methods, such as CAS. This will be put aside for the moment, and will be explained later.

Like this:

public native int hashCode(); 
Copy the code

When calling a Native method, there will also be a stack corresponding to the thread to store information such as the local variable table used at the bottom of the native method, which is the function of the local method stack.

Seven, the Java heap

It’s the biggest piece of data management. A block of memory content shared by all threads that is automatically created when the virtual machine is started. This area is the main storage object, which is often referred to as the garbage collection area.

Java heap memory, the most important area of JVM memory that houses Java objects, is the thread shared area.

In the following code, new A() creates an instance of an object whose information is stored in the Java heap:

public class Test{ public static void main(String[] args) { A a = new A(); a.load(); }}Copy the code

When the main thread executes the main() method, it creates a stack frame for it to merge into the stack. The local variable ain the stack frame holds the address of the object instance A in the Java heap memory:

Eight, summary


This article mainly describes the JVM runtime memory area, mainly including their respective division, each part of the specific storage, the middle combined with the code detailed description of the process, hope to give you a little help, do not think inside a black box, a face of muddlestate. (Mainly to not be confused in the interview, hahaha)

If you feel that writing is also good, trouble to give a praise 👍, your recognition is the power of my writing!

If you think something is wrong, please feel free to comment.

You can also pay attention to my public number learning Java little sister, we discuss together. All right, bye.