Deep understanding of Java Virtual Machine notes---memory areas

Source: Internet
Author: User

The Java Virtual machine divides the memory it manages into several different data regions during the execution of a Java program. These areas have respective uses, as well as creation and destruction times, 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. According to the Java Virtual Machine Specification (2nd edition), the memory areas managed by the Java Virtual machine include the following runtime data regions,


1. Procedure counter (program Counter Register)
A program counter is a small amount of memory space that acts as a line number indicator of the bytecode that is being executed by the current thread. The bytecode interpreter works by changing the value of this counter to select the next byte-code instruction to execute, and the basic functions such as branching, looping, jumping, exception handling, thread recovery, and so on, need to rely on this counter to complete.
Because the multithreading of a Java Virtual machine is implemented in a way that threads switch CPU time slices in turn, at any one time, a processor (a kernel for a multicore processor) will only line up the instructions in one thread. Therefore, in order to return to the correct execution position after each switch, each thread needs a separate program counter, which is private to the thread.
If the current thread is executing a Java method, this counter is the address of the virtual machine bytecode under execution, and if a native method is executed, the value of this counter is null (UNDEFINEDD), the calculator must be able to accommodate the return address of the method or a local pointer to the specific platform. This area is the only area in the Java virtual machine that does not stipulate any outofmemoryerror.

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, and each time the method is executed, a stack Frame is created in the virtual machine stack to store information such as the operand stack, the dynamic link, the local variable table, and so on. Each method is called until its execution completes and the object has a stack frame in the virtual machine stack in the stack and out of the process.
The virtual machine specification illustrates that the Java Virtual machine stack can be implemented as a fixed size, or can be implemented to scale and contract dynamically based on calculations. If it is implemented as a fixed size, it needs to be created independently. The Java Virtual machine implementation may be given to programmers and users to control the initial size of the Java Virtual Machine stack, of course, if it is the implementation of dynamic expansion and contraction, you can also control the maximum and minimum size of the virtual machine stack.
The virtual machine specification provides two exceptions in this area: if the thread request stack depth exceeds the allowed depth of the virtual machine, the virtual machine will throw a stackoverflowerror error, and if the virtual machine stack can be dynamically extended (most of the current virtual machines can be dynamically extended, Except that the Java Virtual Machine specification allows a fixed-length virtual machine stack, the Java virtual machine throws a OutOfMemoryError error when the extension fails to request enough memory or when a new thread is created that does not have enough memory to create an initial size virtual machine stack.

3. Local Method Stack
The local methods Stack (Native method Stacks) is very similar to the virtual machine stack, but the difference is that the virtual machine stack executes the Java method, and this
The local law stack performs the native method. The language used in the virtual machine specification for methods in the local method stack is not mandatory for use and data structures
, the specific virtual machine can be implemented freely. Even virtual machines, such as Sun HotSpot, directly merge the local method stack with the virtual machine stack. As with the virtual machine stack, the local method stack also throws Stackoverflowerror and OutOfMemoryError errors.

4. Java Heap

for most applications, the Java heap (Java heap) is a Java virtual The largest piece of the interior that the machine manages. The Java heap is an inner area 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 object instances are allocated for internal storage. The description in the virtual machine specification is that all instances of the class and array objects are allocated in the heap.

The Java heap is the main area of the garbage collector's role, so it is often called the GC heap (garbage collected heap). If from the perspective of memory recycling, because the current collector is basically a generational collection algorithm, so the Java heap can also be subdivided into: the new generation and the old age; Again, the Cenozoic can be divided into 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 partition, is not related to the content, no matter what area, storage is still an object instance, further dividing its purpose is to better reclaim memory or allocate memory more quickly.
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. That can be implemented into a fixed size, can also be implemented into dynamic expansion and contraction, but the current mainstream virtual machine can be dynamically expanded and contracted (through-XMX and-xms control). A outofmemory error will be thrown if there is not enough 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. The method area is created when the virtual machine is started.
For a hotspot virtual machine, the method area is also referred to as the "permanent generation (Permanent Generation)", which is essentially not equivalent, simply because the hotspot virtual machine extends the GC collection to the method area, or to the method area for permanent implementation. For other virtual machines (such as Bea JRockit, IMB J9) There is no concept of a permanent generation, that is, hotspot now has to abandon the permanent generation and "move" to native memory to achieve the planning of the method area (in JDK8 has removed the permanent generation, In JDK7, some data that was originally stored in the method area is moved to the Java heap.
Like the Java heap, the method area does not require contiguous memory and can choose a fixed size with extensible shrinkage, and optionally does not implement garbage collection because the garbage collection effect of the method area is unsatisfactory. The OutOfMemory exception is thrown when the method area does not meet the intrinsic allocation requirements.

6. When running a constant amount
The runtime contant pool is part of the method area. In addition to the class's version, field, method, interface, and other descriptive information, there is also a constant table (Constant pool table) that stores the various literal and symbolic references generated during the compilation period, which will be stored in the run-time pool of the method area after the class is loaded.
One of the key features of running a constant pool relative to a class file const pool is that it is dynamic, and the Java language does not require constants to be generated only at compile time, that is, the content in the constant pool that is not pre-placed in the class method can be entered into the run constant pool of the method area. You can also put new constants into a constant pool during a program run, such as The Intern () method of the String class.
Running a constant is part of the method area and is naturally constrained by the size of the method area memory, which throws a OutOfMemory exception when the const pool is no longer able to apply to 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 part of memory is also used frequently and can cause outofmemoryerror errors to occur. Garbage collection, although the virtual machine will be the direct memory recycling, but not like the new generation and the old age, the discovery of insufficient space to notify the collector to garbage collection, it can only wait until the old age is full after FULLGC, and then "by the way" to clean up the direct memory of discarded objects.
New NiO has been introduced into JDK1.4, which introduces an I/O method based on channel and buffer, which can allocate out-of-heap memory directly using the native function library. It is then manipulated by a Directbytebuffer object stored in the Java heap 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.

Deep understanding of Java Virtual Machine notes---memory areas

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.