Java memory region details

Source: Internet
Author: User

During Java program execution, the Java Virtual Machine divides the memory areas it manages into several different data areas. These regions have their own purposes. Some regions exist with the startup of virtual machine processes, and some regions are created and destroyed Based on the startup and termination of user threads. According to Java Virtual Machine Specification Version 2, the memory managed by the Java Virtual Machine will include the following runtime data areas.


Program counters

A program counter is a small memory space. Its function can be seen as a row number indicator of the bytecode executed by the current thread, when the bytecode interpreter is working, it selects the next bytecode instruction, branch, loop, jump, and exception handling to be executed by changing the counter value, this counter is required for basic functions such as thread recovery.

Each thread has an independent program counter to ensure that the virtual machine can be restored to the correct execution position after thread switching (using the thread private memory, each thread does not affect each other ).

If the thread is currently executing JAVA code, the address of the VM bytecode instruction is being executed when the counter records; if it is the Native method, the counter value is blank. This memory region is the only region in which the Java Virtual Machine specification does not specify any OutOfMemoryError conditions.


Java Virtual Machine Stack

Like program counters, they are also thread-proprietary and have the same lifecycle as threads. The Virtual Machine stack describes the Memory Model of Java method execution. When each method is executed, a stack frame is created to store local variable tables, Operation stacks, and dynamic links, method exit and other information. Each method is called until the execution is completed, which corresponds to the process from the inbound stack to the outbound stack of a stack frame in the Virtual Machine stack.

The local variable table stores various basic data types (boolean, byte, char, short, int, float, long, and double) known during the compilation period, and object reference ), returnAddress (pointing to the address of a bytecode instruction ).

Data of the long and double types occupies two local variable spaces (slots), and only one of the other data types occupies one space. The local variable table needs to be allocated during the programming period. when you enter a method, the size of the local variable space allocated in the frame required by this method is completely determined, the table size of local variables is not changed during method running.


Local method Stack

The local method stack is very similar to the Virtual Machine stack. The difference is that the virtual machine stack executes the Java code (bytecode) service for the virtual machine, while the local method stack is the Native method service used by the virtual machine.


Java heap

Java heap is a memory area shared by all threads. It is created when the VM is started. The only purpose of this memory area is to store object instances. Almost all object instances and arrays are

Allocate memory here. (But with the development of JIT compiler and the gradual maturity of escape analysis technology, stack allocation, scalar replacement, and optimization technology will lead to some subtle changes, all objects are allocated on the stack, which is not so absolute)

Java heap is the main area of garbage collection management. Java heap can be divided into the new generation and the old generation. in a detailed manner, there are: Eden space, From two vor spaces, To two vor spaces. From the perspective of memory Allocation, the Java heap shared by threads may be divided into private Allocation buffers (Thread Local Allocation Buffer, TLAB) for multiple threads ).


Method Area (permanent generation)

The method area, like the Java heap, is a memory area shared by threads. It is used to store the class information, constants, static variables, and Code Compiled by the real-time compiler.


Runtime constant pool

The runtime constant pool is part of the method area. in addition to the description information such as the version information, fields, methods, and interfaces of the Class, the Class file also contains the Constant Pool Table ), used to store various literal and symbolic references generated during compilation.


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.