There are a lot of articles about the JVM memory structure, I believe that many of my friends have a certain understanding of this part of the knowledge.

So, please try to answer these questions:

What is the memory structure managed by the JVM?


2. What are the differences between different virtual machines in implementing runtime memory?


3. Which areas of the runtime data area are shared by threads? Which ones are exclusive?


4. Are there any areas available other than JVM runtime memory?


What is the difference between a heap and a stack?


6. Are arrays in Java stored on a heap or stack?


7. How many ways can objects be created in Java?


What is the process of object creation in Java?


9. Do objects in Java have to allocate memory on the heap?


How to obtain heap and stack dump files?

If you can answer all 10 of the above questions correctly, then you really know a lot about the JVM’s memory structure and memory allocation. If there are some things you don’t know, this article will help you to answer them.

Q1: What is the memory structure managed by the JVM?

During the execution of Java programs, the Java VIRTUAL machine divides the managed memory into different data areas. The Java Virtual Machine Specification specifies that the memory managed by the JVM should include the following runtime areas:

The Java virtual machine run-time data area mainly consists of PC registers (program counters), Java virtual machine stack, local method stack, Java heap, method area, and runtime constant pool.

Each region has its own different role, the role of each region is not believed in this article.

However, it should be noted that the above area division is only logical area. The specification has relatively loose restrictions on some areas. Therefore, different VIRTUAL machine vendors have different implementations, even different versions of the same VIRTUAL machine.


Q2: How do different virtual machines implement runtime memory differently?

As mentioned earlier, the Java Virtual Machine Specification defines the area of memory required by the JVM runtime, which varies from virtual machine to virtual machine implementation. Of these areas, the specification manages the method area the most loosely. The specification describes this area as follows:

The method area is created at virtual machine startup, and although the method area is a logical part of the heap, simple virtual machine implementations can choose not to implement garbage collection and compression in this area. This version of the specification also does not limit the implementation method area memory location and code compilation management policy. The capacity of the method area can be fixed, or it can expand dynamically with the needs of the program execution and shrink when no more space is needed. The method area in the real memory station can be discontinuous.

This regulation can be said to give virtual machine manufacturers great freedom.

The virtual machine specification does not specify the location of the method area implementation, and in the most famous HotSopt virtual machine implementation (prior to Java 8), the method area is only logically independent and does not physically exist independently of the heap, but in the permanent generation. Therefore, the method area can also be garbage collected at this point.

As it turns out, there are plenty of short-lived objects in the JVM, as well as some with long lifetimes. In order to use different collection strategies for them, the Generation collection algorithm is used, so the HotSpot VIRTUAL machine classifies the heap into new generation, old generation and permanent generation according to the age of the object.

In Java 8, the HotSpot virtual machine removes the persistent generation and uses local memory to store class metadata information called Metaspace.



Q3: Which areas of the runtime data area are shared by threads? Which ones are exclusive?

The PC registers, virtual machine stack, and local method stack are thread exclusive in the JVM runtime memory area.

The Java heap and method area are shared by threads. However, it is worth noting that the Java heap actually allocates a separate TLAB space for each thread, which is shared by the thread when allocated and by the thread when used. (TLAB introduction)


Q4: Are there any areas available other than JVM runtime memory?

In addition to the run-time data area we described earlier, there is another part of memory that is frequently used. It is not part of the run-time data area or the memory area defined in the Java Virtual Machine specification, but it is direct memory.

The direct memory allocation is not limited by the Java heap size, but it is still affected by the total memory of the server.

NIO, introduced in JDK 1.4, introduces a Channel – and buffer-based I/O approach, which can use Native functions to allocate out-of-heap memory directly and then operate as an application of that memory through a DirectByteBuffer object stored in the Java heap.


Q5: What is the difference between a heap and a stack?

The heap and stack (virtual machine stack) are two completely different memory areas, one is thread exclusive, the other is thread shared, the biggest difference between the two is the contents of the storage is different:

The heap mainly houses object instances. The stack (local variable table) mainly stores references to various basic data types and objects.


Q6: Are arrays in Java stored on a heap or on a stack?

In Java, arrays are also objects, so how objects are stored in memory also applies to arrays;

So, instances of arrays are stored in the heap, and references to arrays are stored on the stack.


Q7: How many ways are there to create objects in Java?

There are five ways to create an object in Java.

The easiest way to do this is to use the new keyword.

User user = new User();Copy the code

In addition, you can use reflection to create objects:

User user = User.class.newInstance();Copy the code

Or use newInstance of the Constructor class:

Constructor< User> constructor = User.class.getConstructor(); User user = constructor.newInstance();Copy the code

In addition, clone method and deserialization method can also be used. These two methods are not commonly used and the code is complicated, so they will not be shown here. If you are interested, you can learn about them by yourself.


Q8: What is the process of object creation in Java?

For the creation of a normal Java object, the process is as follows:

1. The virtual machine goes to the new directive and to the constant pool to locate the symbolic reference to the class. Check whether the class represented by the symbol reference has been loaded, parsed, and initialized. 3. The VM allocates memory for objects. 4. The VM initializes all allocated memory space to zero. 5. The VM performs necessary Settings on the object. 6, execute method, member variable initialization.


Q9: Do objects in Java have to allocate memory on the heap?

As mentioned earlier, the Java heap mainly holds object instances, but as JIT compilation time advances and escape analysis techniques mature, on-stack allocation and scalar replacement optimization techniques can lead to subtle changes in how all objects allocated to the heap become less “absolute.”

In fact, the JIT does a lot of optimizations to the code during compilation. Some of these optimizations are designed to reduce heap allocation stress, and an important technique is called escape analysis.

If the JIT does escape analysis and finds that some objects do not have an escape method, it is possible that heap allocation will be optimized to stack allocation. (For more on escape analysis and stack allocation, see escape analysis in Java. Objects are not always allocated on the heap.)


Q10: How to obtain heap and stack dump files?

Java Dump is a runtime snapshot of a Java VM. Save the running status and information of the Java virtual machine to a file.

Heap dumps can be obtained using the jmap command on the server, and call stack dumps for threads can be obtained using the jstack command.

The above is the whole content of this article, for many of the knowledge points, the author did not expand one by one, interested friends can learn about the Java virtual machine related knowledge, recommended three books: Understanding the Java Virtual Machine, HotSpot In Action, and The Java Virtual Machine Specification (8th edition) are also referenced in this article. I hope it helps.