JVM Memory Management------The Java Language Memory management overview

Source: Internet
Author: User
Tags instance method

Introduction

Memory management has always been the proud and proud capital of the Java language, which allows Java programmers to completely ignore the details associated with memory management and focus only on business logic. But there is no perfect thing in the world, and it brings convenience, and it introduces a lot of maddening memory overflows and leaks.

Not only that, but some programmers who have developed in other languages have put a "no memory" hat on the Java programmer, which is a bit hard to accept. After all, there is no malloc and delete in Java, no destructors, no pointers, how can the first-touch Java handlers get in touch with the memory part, simply to make unlimited things in a limited space that's what jmm should do, We try to write code when we write code, according to the idea of garbage collection. I think that's the purpose.

Although the truth is difficult to accept, but there are a lot of Java programmers to memory this part of the body is not known, although the knowledge of memory, may not give the development of the usual changes and benefits, but it will still subtly improve your technical level, this after understanding memory management, I believe you will have a deep understanding.

Memory Partitioning

When it comes to memory, it is a data storage area that is only available when the program is running, and each virtual machine has its own partitioning method for dividing the area, but they all have to follow the basic specifications of the Java Virtual machine.

In the virtual machine specification, the memory is divided into six parts, namely the PC Register, Java Virtual machine stack, Java heap, method area, run constant pool, and local method stack .

Java Virtual machine specification and Java Virtual machine

There is also a need to explain the differences between the Java Virtual Machine specification and the Java Virtual machine, as the name implies, theJava Virtual Machine specification is a Java Virtual machine implementation of the specification requirements, is made by Oracle, The Java virtual machine that we usually refer to is the implementation of a specific Java Virtual machine specification . For example, we most often use the Java Virtual machine hotspot, in fact, Java Virtual machine has a lot of implementations, even if you have a deep understanding of the Java Virtual Machine specification and interested in it, you can write a Java virtual machine, of course, the difficulty is not difficult to imagine. But I won't. I don't have to write.

Structure diagram

Memory Area Detailed

For the above picture, memory refers to the rectangular box in the run-time data section, the following is a brief description of the role of the various parts:

1.PC Register (thread exclusive): The full name is the program Count register, which records the address of the Java method that each thread is currently running, and if the local method is currently executing, the program counter will be an empty address. Its role is to support multi-threading, thread blocking, recovery, hang and a series of operations, intuitive to imagine, if you do not remember where each thread is currently running, and how to recover it. According to this, each thread has a PC register, which means that the PC register is thread-unique.

2.Java Virtual machine stack (thread exclusive): the Java Virtual machine stackis created at the same time that the thread was created, used to store stack frames, and the Java Virtual machine stack is thread -specific.

stack frame : Simple point, can be interpreted as a method to run the temporary data storage area, specifically, it includes data and part of the process results, at the same time, it has to handle the return value of the method, dynamic link and the task of abnormal dispatch . The stack frame is created as the method is created, and is destroyed as the method ends, and if the method throws an exception, the method ends. In each stack frame, however, it has its own local variable table and the operand stack, as well as a reference to the current class's run-time-constant pool.

local variable table : It is a list of methods local variables that are written to the class file at compile time. simply understood, it can be understood as an array of objects, which correspond to each local variable according to index 0 to length-1 , in particular, if it is the local variable table of the instance method, the No. 0 local variable will be a reference to the current instance, the This keyword, 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 fixed when it is written to the class file at compile time . Its function is to provide the byte code instruction operation variable calculation space, such as simple, for the int a=9 This sentence, you need to first press 9 into the operand stack, and then assign 9 to a variable.

3.Java Heap (global share): This part is the most important part of Java memory, the most important part of it, not because of its importance, but rather as part of the developer's most concern. It is created with the launch of a Java virtual machine, storing all object instances and array objects, and built-in "Automatic memory Management System", which is what we often call garbage collectors (GC). The release of memory in the Java heap is not under the control of the developer and is fully managed by the Java Virtual machine. As for how Java virtual machines implement garbage collectors, the Java Virtual Machine specification does not have a clear stipulation, and because of this, we usually use the Java Virtual Machine provides many kinds of garbage collectors, they use different algorithms and implementation methods, has met the various performance requirements.

4. method Area (global sharing): The method area is also an integral part of the heap, it mainly stores the running constant pool, the field information, the method information, the construction method and the ordinary function bytecode content as well as some special methods . It differs from the Java heap except that the information stored is not the same as the Java heap, the biggest difference is that this part of the Java Virtual Machine specification does not enforce an automatic memory management system (GC).

5. local method Stack (thread exclusive): The local method stack is a traditional stack that is used to support the execution of the native method. The local method stack is also used when the Java virtual machine is used to implement the instruction set interpreter in other languages . If none of the preceding two occur, that is, if the Java virtual machine does not rely on the local method stack and the Java Virtual Machine does not support the native method, then the local method stack is not required. If necessary, the local method stack is also created with each thread's startup.

The above five memory areas, in addition to the PC register, the remaining four generally, the Java virtual machine is required to provide the client to adjust the size of the parameters, that is, we commonly used XMS, xmx and so on.

Memory management

Memory management is divided into memory allocation and memory release, look at the above five memory areas, in fact, can be broadly divided into two parts, part of the global share, part of the thread is unique .

This part of the memory that is unique to the thread is created as the thread starts, and the memory is freed when the thread is destroyed. This part of the memory, does not require garbage collector management, but the Java Virtual machine to actively manage, whenever a thread is created, the Java Virtual machine will be assigned to the corresponding PC register and Java Virtual machine stack, if necessary, there will be a local method stack. Correspondingly, when a thread is destroyed, the Java Virtual machine will also release the memory that the thread occupies .

This portion of the memory that is shared globally is more difficult to handle than the portion of memory that is unique to the thread, but this is only for the implementation of the virtual machine, because this part of the memory is to implement the automatic Memory management system (GC).

This part of the memory of the global share (the heap), the memory allocation is mainly by the programmer to display the use of the new keyword to trigger, as to the new out of this part of the memory allocation, how to allocate, it is the Java virtual machine to decide. The release of this part of the memory is managed by an automated memory management system (the GC) .

In general, heap memory allocation is dependent on the policy and implementation of the GC, and when allocated, it is necessary to consider how to reclaim this portion of memory. And that's why, for this part of memory allocation, we have to understand how memory is being recycled before we can better understand how memory is allocated.

Conclusion

This concludes the overview of memory management in the Java language, and the following chapters will focus on how the GC works and how it is implemented, so please be sure to look forward to it.

JVM Memory Management------The Java Language Memory management overview

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.