search
HomeJavajavaTutorialJVM memory management------Overview of memory management of JAVA language

Introduction

Memory management has always been the pride and pride of the JAVA language. It allows JAVA programmers to basically completely ignore the details related to memory management and only focus on business logic. However, no good thing is perfect in the world. While it brings convenience, it also introduces many crazy memory overflow and leak problems.
The terrible thing is not just that. Some programmers who develop in other languages ​​​​put a label on JAVA programmers who "do not understand memory", which is really unacceptable. After all, there are no malloc and delete, no destructors, and no pointers in JAVA. How can programmers who are new to JAVA come into contact with the memory part? What's more, many JAVA programmers are still friends who quit their majors and became monks halfway.
However, although the fact is difficult to accept, there are indeed many JAVA programmers who know nothing about memory. Although mastering the relevant knowledge of memory may not bring about earth-shaking changes and benefits to daily development, but It will still subtly improve your technical level. After understanding memory management, I believe you will have a deep understanding of this.

Memory Division

Talking about the word memory, it is a data storage area that only exists when the program is running. For the division of this area, each virtual machine has its own division method. , but they must comply with the basic specifications of the JAVA virtual machine to implement.
In the virtual machine specification, the memory is divided into six parts, namely PC registers, JAVA virtual machine stack, JAVA heap, method area, runtime constant pool and local method stack.

JAVA Virtual Machine Specification and JAVA Virtual Machine

Here we also need to explain the difference between the JAVA virtual machine specification and the JAVA virtual machine. As the name suggests, the JAVA virtual machine specification is an implementation of the JAVA virtual machine. The specification requirements are formulated by Oracle, and the JAVA virtual machine we usually refer to generally refers to the implementation of a specific JAVA virtual machine specification. For example, the most commonly used JAVA virtual machine hotspot, in fact, there are many implementations of JAVA virtual machine. Even if you have an in-depth understanding of the JAVA virtual machine specification and are interested in it, you can write your own JAVA virtual machine. Of course, the difficulty is not difficult to imagine.

Structural diagram

The following figure is a JVM structure diagram quoted from Baidu Library. Since the runtime constant pool is an area allocated by the method area, there is no runtime constant in this figure. Measurement pool.

JVM memory management------Overview of memory management of JAVA language

Detailed explanation of the memory area

For the above picture, the memory refers to the part of the runtime data area in the rectangular box. Here is a brief introduction to the functions of each part:
1. PC register (thread Unique): The full name is the program counter register, which records the address of the JAVA method currently running by each thread. If a local method is currently executed, the program counter will be an empty address. Its function is to support multi-threading and a series of operations such as thread blocking, recovery, and suspension. Imagine intuitively, how to recover if you do not remember the current running position of each thread. Based on this, each thread has a PC register, which means that the PC register is unique to the thread.
2. JAVA virtual machine stack (unique to threads): The JAVA virtual machine stack is created when a thread is created and is used to store stack frames. The JAVA virtual machine stack is also unique to threads.


Stack frame: Simply put, it can be interpreted as a storage area for temporary data when a method is running. Specifically, it includes data and part of the process results. At the same time, It is also responsible for handling method return values, dynamic linking, and exception dispatching. The stack frame is created when the method is created and destroyed when the method ends. If the method throws an exception, it is also considered the end of the method. However, in each stack frame, there is its own local variable table and operand stack, as well as a reference to the runtime constant pool of the current class.
Local variable table: It is a list of method local variables, which is written into the class file during compilation. For a simple understanding, it can be understood as an object array, which corresponds to each local variable according to the index 0 to length-1. In particular, if it is the local variable table of the instance method, the 0th local variable will be a A reference to the current instance, which is the this keyword, and the rest of the local variables start at index 1.
Operand stack: It is a last-in-first-out (LIFO) stack, and its length is also written into the class file during compilation and is fixed. Its function is to provide space for bytecode instruction operation variable calculation. For example, simply for the sentence int a=9, you need to push 9 into the operand stack first, and then assign 9 to the variable a.

3. JAVA heap (global sharing): This part is the most important part of JAVA memory. The reason why it is the most important part is not because of its importance, but because of the most important part as a developer. part that should be paid attention to. It is created when the JAVA virtual machine is started, stores all object instances and array objects, and has a built-in "automatic memory management system", which is what we often call the garbage collector (GC). The memory release in the JAVA heap is not controlled by the developer and is completely handled by the JAVA virtual machine. The JAVA virtual machine specification does not have clear regulations on how to implement a garbage collector in a JAVA virtual machine. Because of this, the JAVA virtual machine we usually use provides many kinds of garbage collectors, which use different algorithms and implementation methods. Meet various performance needs.
4. Method area (global sharing): The method area is also a component of the heap. It mainly stores the runtime constant pool, field information, method information, the bytecode content of constructors and ordinary functions, and some special method. The biggest difference between it and the JAVA heap is that the information stored is different from the JAVA heap. The biggest difference is that this part of the JAVA virtual machine specification does not mandate the implementation of an automatic memory management system (GC).
5. Local method stack (unique to threads): The local method stack is a traditional stack that is used to support the execution of native methods. If the JAVA virtual machine uses other languages ​​to implement the instruction set interpreter, the native method stack will also be used. If neither of the previous two occurs, that is, if the JAVA virtual machine does not rely on the native method stack, and the JAVA virtual machine does not support native methods, then the native method stack is not needed. If necessary, the local method stack is also created with the startup of each thread.
The above five memory areas, except for the PC register, the other four generally require the JAVA virtual machine implementation to provide customers with size adjustment parameters, which are our commonly used Xms, Xmx, etc.

Memory Management

Memory management is divided into memory allocation and memory release. Take a look at the five memory areas above. In fact, it can be roughly divided into two parts, one is globally shared and the other is thread independent. have.
This part of the memory unique to the thread is created when the thread is started, and when the thread is destroyed, the memory is released. This part of memory does not need to be managed by the garbage collector, but is actively managed by the JAVA virtual machine. Whenever a thread is created, the JAVA virtual machine will allocate the corresponding PC register and JAVA virtual machine stack to it. If necessary If so, there will also be a local method stack. Correspondingly, when a thread is destroyed, the JAVA virtual machine will also release all the memory occupied by this thread.
Compared with the part of memory unique to threads, this part of globally shared memory is more difficult to process, but this is only for the implementation of virtual machines, because this part of memory is required to implement the automatic memory management system (GC) .
Memory allocation for this part of globally shared memory (hereinafter referred to as the heap) is mainly triggered by the programmer's explicit use of the new keyword. As for where and how to allocate this part of new memory, it is decided by the JAVA virtual machine. . The release of this part of memory is managed by the automatic memory management system (hereinafter referred to as GC).
Normally, heap memory allocation depends on the GC strategy and implementation. When allocating, you must consider how to reclaim this part of memory. Because of this, for the explanation of memory allocation, we must first understand how memory is recycled in order to better understand how memory is allocated.

Conclusion

The above is the content of JVM memory management------JAVA language memory management overview. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software