Java Memory Area

Source: Internet
Author: User

Reprint please indicate source: http://www.cnblogs.com/lighten/p/5971424.html

1. Preface

Java because of the automatic memory management mechanism, so developers generally do not need to worry about memory leaks and other issues. However, this does not mean that memory problems do not occur, because the lack of attention to memory-related issues, making it difficult to find this problem, so it is necessary to understand how the virtual machine uses memory, but also to deepen their understanding of the program. All the contents of the JVM series are read "in-depth understanding of Java Virtual Machine" and "Java Specification", refining summary, due to the publication of the book earlier, at that time JDK8 has not come out, this version is only based on JDK7, I am still shallow, In the learning stage, currently JDK9 in the test. During this period, the JVM technology is also developing, there may be some content and the current technology, of course, the change is unlikely to be very large, but when reading to pay attention to this problem, if there is any place in the current version of the improvement, please advise, thank you.

2. Memory Area Division 2.1 JDK7

Java Virtual machines define the run-time data areas that are used by programs during run time, some of which are created when the virtual machine is started, destroyed on exit, and other data regions that correspond to threads, and the start and end of threads cause their corresponding data regions to be created and destroyed.

Red refers to a thread-independent or thread-private memory, and blue is the area of thread-sharing.

   1)PC Register (program counter) : The Java Virtual machine supports multi-threading, and each virtual machine thread has its own PC (program Counter) register. At any moment, the thread of a Java virtual machine executes only one method of code, and the method being executed is called the current method. If this method is not native, then the PC register will save the address of the executing bytecode instruction, if the method is native, the value of the PC register is undefined. The capacity of the PC register should at least be able to hold a returnaddress type of data or a value of a platform-dependent local pointer. This is the only area in the Java Virtual Machine specification that does not specify any outofmemoryerror conditions.

2)Java Virtual machine stack:each thread in Java has its own private Java Virtual machine stack, which is created at the same time as the thread to store the stack frame (Frames). Its role is very similar to the stack in traditional speech (c language, etc.), where local variables and some process results are stored. It stores the local variable table, the operand stack, the dynamic link, the method exit and so on information. Each method from the call to the completion of the process, corresponding to a stack frame in the virtual machine into the stack to the process of the stack. The Java Virtual machine stack may have the following exceptions: 1. If a thread requests an allocated stack capacity that exceeds the maximum allowable capacity of a Java virtual machine, a Stackoverflowerror exception is thrown. 2. If the Java Virtual machine stack can be dynamically extended, and the extended action has been tried, but not enough memory is currently requested to complete the extension, or when a new thread is created, there is not enough memory to create the corresponding virtual machine stack. The Java Virtual machine will throw a OutOfMemoryError exception.

3) Local method stack:Java Virtual machine implementations may use traditional stacks (often referred to as ' C Stacks ') to support the execution of Native methods (which are written in languages other than Java), which is the local method stack (Native methods Stack). The local method stack is also used when the virtual machine implements the instruction set interpreter using a different language. If the Java virtual Machine does not support the native method, and it does not rely on the traditional stack, it can not support the local method stack, if the local method stack is supported, the stack is usually created by thread when threads are allocated. The local method stack may have the following exception: If a thread requests an allocated stack capacity that exceeds the maximum allowable capacity of the local method stack, the Java virtual machine throws a Stackoverflowerror exception. If the local method stack can be dynamically extended, and the extended action has been tried, but the current cannot request enough memory to complete the extension, or if there is not enough memory to create the corresponding local method stack when creating a new thread, the Java virtual machine will throw a OutOfMemoryError exception.

4)Java heap: in a Java virtual machine, a heap is a region of run-time memory that can be shared by each thread, and an area that allocates memory for all class instances and arrays of objects. The heap is created when the virtual machine is started, and it stores various objects managed by the automatic Memory management system (Automatic Storage Management system, which is often called "garbage Collector (garbage collector)"). These managed objects are not required and cannot be explicitly destroyed. When implemented, it can be either fixed size or extensible (via-XMX and-xms control). If the actual required heap exceeds the maximum capacity that the automatic memory management system can provide, the Java virtual machine will throw a OutOfMemoryError exception.

5) Method area: in a virtual machine, the method area is a run-time memory area that can be shared by each thread. The method area is very similar to the code store in the traditional language (Storage area of Compiled code) or the body segment of the operating system process (text Segment), which stores the structure information for each class, such as a run-time-constant pool (runtime Constant Pool), field and method data, constructors, and bytecode contents of normal methods, also include some special methods used in class, instance, and interface initialization. The method area is created when the virtual machine is started, although the method area is a logical part of the heap, but a simple virtual machine implementation can choose not to implement garbage collection in this area. This version of the Java Virtual Machine specification also does not qualify the management strategy for implementing the memory location of the method area and compiling the code. If the memory space of the method area does not meet the memory allocation request, the Java virtual machine throws a OutOfMemoryError exception.

6) Run a constant pool: Runtime The Constant pool is a run-time representation of a const pool for each class or interface, and it includes several different constants: A method or a field reference that is known from the compilation period to the value literal that must be parsed after it has been run. The run-time pool plays a role similar to the symbol table in traditional languages, but it stores data in a wider range than the usual symbolic table. each run-time pool is allocated in the method area of the Java Virtual Machine , and the corresponding running constant pool is created after the classes and interfaces are loaded into the virtual machine. When a class or interface is created, the Java virtual machine throws a OutOfMemoryError exception if the amount of memory required to construct the run-time pool exceeds the maximum value that can be provided by the method area.

7) Direct Memory: Direct memory is not part of the data area when the virtual machine is running, nor is it a memory area defined in the Java VM specification. However, this part of memory is also used frequently. It can also cause outofmemoryerror anomalies to occur. The new Input/output class was added to the JDK1.4, introducing a channel-and buffer-based I/O approach that can be used to directly allocate out-of-heap memory using the native library. It is then manipulated by a Directbytebuffer object stored in the Java heap as a reference to this block of memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and native heap. The total memory in each region (including direct memory) is less than the physical memory.

2.2 JDK8

Looking at the Java Virtual Machine specification of JDK8, it seems that there is not much change.

3 concluding remarks

The first five areas are roughly the JVM's memory division, we need to focus on the heap and Java Virtual machine stack, one holds the object instance, one is the execution data of the method, the next chapter will talk about the structure of the stack frame, more convenient to understand how the method is called.

Java Memory Area

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.