Java Virtual Machine Memory Area breakdown (1)

Source: Internet
Author: User

I. Overview
For developers of C and C + + programs, in memory management, programmers have absolute access to memory, but also primarily to the correct use and cleanup of memory, which requires a high level of programmer.

For Java programmers, with the help of the automatic memory management mechanism of the virtual machine, it is no longer necessary to write the paired Delete/free code for each new operation, and it is not easy to have memory leaks and memory overflow problems, and it looks like the memory managed by the virtual machine is fine. However, it is precisely because Java programmers put the power of memory control to the Java Virtual machine, in the event of memory leaks and overflow problems, if you do not understand how the virtual machine uses memory, then the troubleshooting will be an extremely difficult task.

Second, Java Runtime data regions
We generally consider the JVM to be a heap and stack component in development, but the actual Java virtual machine divides the memory it manages into several different data regions during the execution of the Java program. These zones have their own purpose, as well as the creation and destruction of time, and some regions exist as virtual machine processes start, and some are built and destroyed depending on the start and end of the user thread. Such as:

Below is a picture taken off the net, for reference only:

The following sections explain the features of each area in detail:
(1) Program counter

If you have learned the principle of computer composition should be very clear, the program counter is the same as the identity card, because the JVM also has its own CPU, in the execution of multi-thread programs, the time slice rotation, according to the program counter to schedule the execution of the thread.
Program Counter Register is a small memory space that acts as a line number indicator of the bytecode that is being executed by the current thread. In the virtual machine concept model (only the conceptual model, the various virtual machines may be implemented in some more efficient way), the bytecode interpreter works by changing the value of this counter to select the next need to execute the bytecode instruction, branch, loop, jump, exception handling, Basic functions such as thread recovery need to rely on this counter to complete.
Because the multithreading of a Java virtual machine is implemented in a way that threads rotate and allocate processor execution time, at any given moment, a processor (a kernel for a multicore processor) executes only the instructions in one thread. Therefore, in order to recover the thread after switching to the correct execution location, each thread needs to have a separate program counter, the counters between the threads do not affect each other, isolated storage, we call this type of memory area is "thread-private" memory.

If the thread is executing a Java method, this counter records the address of the executing virtual machine bytecode instruction, or null (Undefined) If the Natvie method is being executed. This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions.

(2) Java virtual machine stack

Like the program counter, the Java Virtual machine stack (Java Stacks) is also thread-private, with the same life cycle as the thread.
The virtual machine stack describes the memory model that is executed by the Java method: Each method is executed with a stack frame (stack frame①) for storing local variable tables, operation stacks, dynamic links, method exits, and so on. Each method is called until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the stack.

Java memory is often differentiated into heap memory (heap) and stack memory (stack), which is coarser, and the division of Java memory areas is actually far more complex. The popularity of this partitioning method only shows that most programmers are most concerned about the memory area that is most closely related to object memory allocation. The "heap" referred to in this section is described later, and the "stack" refers to the current virtual machine stack, or the local variable table portion of the virtual machine stack.

The Local variables table holds the various basic data types (Boolean, Byte, char, short, int, float, long, double), object references (reference types, which are not equivalent to the object itself and are implemented according to different virtual machines). It may be a reference pointer to the start address of the object, or it may point to a handle representing the object or other location associated with the object, and the ReturnAddress type (the address of a bytecode directive).

The 64-bit length of long and double data takes up 2 local variable space (slots), and the remaining data types occupy only 1. The amount of memory space required for a local variable table is allocated during compilation , and when entering a method, the method needs to allocate much of the local variable space in the frame is fully deterministic and does not change the size of the local variable table while the method is running.

In the Java virtual machine specification, there are two exceptions to this area: if the thread requests a stack depth greater than the virtual machine allows, the STACKOVERFLOWERROR exception will be thrown, and if the virtual machine stack can be dynamically extended (most of the current Java virtual machines can be dynamically extended, However, a fixed-length virtual machine stack is also allowed in the Java virtual machine specification, which throws a OutOfMemoryError exception when the extension fails to request enough memory.

(3) Local method stack

The local methods Stack (Native method Stacks) is very similar to the virtual machine stack, except that the virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine, while the local method stack serves the Native method used by the virtual machine. The language, usage, and data structure of the methods used in the local method stack in the virtual machine specification are not mandatory, so the virtual machine can implement it freely. Even a virtual machine, such as a Sun HotSpot virtual machine, directly puts the local method
Stack and virtual machine stack. As with virtual machine stacks, the local method stack area throws Stackoverflowerror and OutOfMemoryError exceptions.

(4) Java heap

For most applications, the Java heap (Java heap) is the largest piece of memory managed by a Java virtual machine. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started . The only purpose of this area of memory is to hold object instances where almost all of the object instances are allocated memory. This is described in the Java virtual machine specification as: All object instances and arrays are allocated on the heap, but with the development of the JIT compiler and the gradual maturity of the escape analysis technology, stack allocation, scalar replacement optimization technology will lead to some subtle changes occur, All objects are allocated on the heap and gradually become less "absolute".

The Java heap is the main area of garbage collector management, so many times it is also called "GC Heap" ("Garbage collected heap", fortunately the country did not translate into "garbage heap"). If from the perspective of memory recycling, because the collector is now basically used in the generation of the collection algorithm, so the Java heap can be subdivided into: the new generation and the old age, and more detailed there is Eden space, from Survivor space, to Survivor space. If viewed from a memory allocation perspective, the thread-shared Java heap may divide multiple thread-private allocation buffers (thread Local Allocation buffer, Tlab). However, regardless of the partitioning, it is not the content of the storage, no matter what area, stored is still an object instance, the purpose of further partitioning is to better reclaim memory, or to allocate memory more quickly. In this chapter, we only discuss the role of memory areas, and the allocation of each of these areas in the Java heap
and recycling details will be the subject of the next chapter.

According to the Java Virtual Machine specification, the Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous, just like our disk space. When implemented, it can be either fixed or extensible, although the current mainstream virtual machines are implemented in a scalable way (via-XMX and-xms control). A OutOfMemoryError exception will be thrown if there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.

(5) Method area

The method area, like the Java heap, is an area of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine. Although the Java Virtual Machine specification describes the method area as a logical part of the heap, it has an alias called Non-heap (Not a heap), which should be distinguished from the Java heap.

The Java Virtual Machine specification has a very loose limit on this area, and you can choose not to implement garbage collection, except that you do not need contiguous memory and can choose a fixed size or extensible, as with the Java heap . The garbage collection behavior is relatively rare in this area, but it is not the data that enters the method area as "permanent" as the name of the permanent generation. The memory recovery target of this area is mainly for the recovery of constant pool and unloading of type, in general, the recovery "score" in this area is more difficult to be satisfied, especially the type of unloading, the condition is very harsh, but the recovery of this part of area is really necessary.

According to the Java virtual machine specification, a OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.

(6) running a constant-rate pool

the runtime Constant pool is part of the method area . In addition to information such as the version of the class, fields, methods, interfaces, and so on, there is a constant pool (Constant pool Table) that holds the various literal and symbolic references generated during the compilation period. This section is stored in the run-time pool of the method area after the class is loaded .

Java virtual machines have strict specifications for each part of a Class file (which naturally includes Chang), and each byte is used to store which data must conform to the requirements of the specification, so that it is recognized, loaded, and executed by the virtual machine. However, for running a constant pool, the Java virtual machine specification does not require any details, and the virtual machines implemented by different providers can implement this memory area as they want. However, in general, in addition to saving the symbol references described in the Class file, the translated
The access reference is also stored in the run-time-constant pool.

Another important feature of running a constant pool relative to a Class file's const pool is dynamic, and the Java language does not require constants to be generated only at compile time, that is, the contents of the constant pool in the class file are not pre-placed to enter the method area to run the const pool, and new constants may be put into the pool during run time. , this feature is the Intern () method of the String class that is used by developers more.

Since the run-time-constant pool is part of the method area, it is naturally constrained by the memory of the method area, which throws a OutOfMemoryError exception when the constant pool is no longer able to request memory.

(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, but this portion of memory is also used frequently and can cause outofmemoryerror anomalies to occur.

The new Input/output class was added to JDK 1.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, and then through a stored Java The Directbytebuffer object inside the heap operates as a reference to this memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and Native heap.

Obviously, the allocation of native direct memory is not limited by the size of the Java heap, but, since it is memory, it will certainly be subject to the size of the native total memory (including RAM and SWAP or paging files) and the processor addressing space. When the server administrator configures the virtual machine parameters, the parameters such as-xmx are usually set according to the actual memory, but the direct memory is often ignored, so that the sum of each memory area is greater than the physical memory limit (including physical and operating system-level limitations), resulting in dynamic scaling
An OutOfMemoryError exception occurred.

Java Virtual Machine Memory Area breakdown (1)

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.