Java Memory structure (run-time data region)

Source: Internet
Author: User

The Java Virtual machine specification for Java Virtual machine memory is actually the Java Virtual Runtime data area, its schema is as follows:

Where the method area and the heap are data areas that are shared by all threads.

The Java Virtual machine stack, the local method stack, and the program counter are thread-isolated data areas.

(1). Program Counter:

is a small memory space, which can be seen as the line number indicator of the byte code executed by the current thread, while the bytecode parser works by changing the value of the program counter to select the next byte-code instruction to execute. The basic functions such as branching, looping, jumping, exception handling, and thread recovery are all dependent on the program counter to complete.

The multithreading of a Java Virtual machine is implemented by rotating threads and allocating processor execution time slices, and at any one time a processor executes only one thread instruction, so to ensure that the thread switches back to the correct execution location, each thread needs a separate program counter. So the program counter is thread-private memory.

The program counter is the only memory area in the Java virtual machine that does not specify any memory overflow outofmemoryerror.

(2). Java Virtual machine stack:

The Java Virtual machine stack 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 to create a stack frame to hold the local variable table, the operand stack, the dynamic connection, and the method exit. Each method is called until the completion process, which corresponds to a stack frame in the virtual machine from the stack to the stack process.

The local variable table of the Java Virtual machine stack holds 8 Java basic type Data, object references (not the object instance itself) known by the compiler, and method return address ReturnAddress.

The local variable table space units of the Java Virtual machine stack are slots (slots), where 64-bit-length double and long types occupy two slots, and the rest of the data types occupy only one slot. The 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 the size of the local variable table is not changed while the method is running.

The Java Virtual machine stack has two exceptions: if the thread requests a stack depth greater than the maximum allowed for the virtual machine, the Stackoverflowerror exception is thrown, and if the virtual machine stack can be dynamically extended, An OutOfMemoryError exception is thrown when the extension cannot request enough memory.

(3). Local Method Stack:

The local method stack is very similar to the Java Virtual machine stack, except that the Java Virtual machine stack is the Java method service for the virtual machine, and the local method stack is the operating system local method service that is called for the virtual machine.

The Java Virtual Machine specification does not enforce the implementation and data structures of the local method stack, and the Sun hotspot virtual machine directly merge the Java Virtual machine stack with the local method stack.

Similar to the Java Virtual machine stack, the local method stack throws Stackoverflowerror and OutOfMemoryError exceptions.

(4). Heap:

The heap is the largest chunk of memory that a Java Virtual machine manages, and the Java heap is a chunk of memory that is shared by all threads, created at the start of a Java virtual machine, and the only purpose of heap memory is to hold object instances. Almost all object instances are allocated memory on the heap.

Java heap is the main area of garbage collector management, from the point of view of garbage collection, because the garbage collector basically uses the Generational collection algorithm, the Java heap can be subdivided into the Cenozoic and the old generation.

The Java Virtual Machine specification stipulates that the heap can be in a physically discontinuous memory space, as long as it is logically contiguous. The implementation can be fixed-size or can be dynamically extended. A OutOfMemoryError exception is thrown if there is no memory completion instance allocation in the heap, and the heap size cannot be extended.

(5). Method Area:

The method area, like the heap, is an area of memory shared by each thread that stores data such as class information, constants, static variables, and immediately compiled code, which have been loaded by the virtual machine. Although the Java Virtual Machine specification describes the method area as a logical part of the heap, the method area has a name non-heap (not a heap).

The Sun hotspot virtual machine calls the method zone the permanent generation (Permanent Generation), and the most important part of the method area is the run-time-constant pool. In addition to the class file in the description of the version, field, method, interface, and other descriptive information, there is also a constant pool, for the compilation period generated by a variety of literal variables, symbolic references, direct references, etc., which will be loaded into the method area after the class load in the run constant pool, In addition, new constants can be stored in a constant pool during run time, such as The Intern () method of String.

The OutOfMemoryError exception is also thrown when the method area and the run-time pool are unable to satisfy memory allocations.

(6). Direct Memory:

Direct memory is not part of the data area of the Java Virtual Machine runtime, nor is it defined in the Java VM specification, but is still used in Java development.

The newly introduced NIO (new I/O) in JDK1.4 introduces a channel-and buffer-based I/O approach that can be used to directly allocate out-of-heap memory using the operating system local method library. A Directbytebuffer object stored in the Java heap is then manipulated as a reference to the out-of-heap direct memory, avoiding duplication of data between Java heap memory and local direct memory, which can significantly improve performance.

While direct memory does not directly receive the memory impact of the Java Virtual machine, if the sum of the individual memory regions of the Java Virtual Machine is greater than the physical memory limit, resulting in direct memory shortages, the OutOfMemoryError exception is thrown when dynamically expanding.

program counters, virtual machine stacks, and local method stacks in the Java Virtual Machine Memory Architecture These three regions are created with threads that are destroyed with thread destruction, so memory allocation and recycling for these three regions is deterministic, and the Java garbage collector focuses on the heap memory and method area memory of the Java Virtual machine.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Memory structure (run-time data region)

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.