Java memory area

Source: Internet
Author: User

The memory managed by JVM includes several runtime data regions:

1ProgramProgram counter register, independent storage. This type of memory area is called "thread private" memory. It is the only region in which the Java Virtual Machine specification does not specify any outofmemoryerror conditions.

2. Virtual Machine stack (Vm stack): it is also private to the thread and has the same lifecycle as that of the thread. When each method is executed, a stack frame is created to store the local variable table, Operation stack, dynamic link, and method exit. The process of calling each method until execution is complete corresponds to the process from the inbound stack to the outbound stack of a stack frame in the VM stack.
The local variable table stores the basic types known during the compilation period, including object references and returnaddress types. The memory space required by the local variable table is allocated during compilation. The size of the local variable to be allocated in the frame in a method is completely determined, the local variable table size is not changed during method running.
There are two exceptions in this region: 1. the stack depth requested by the thread is greater than the depth allowed by the VM, and stackoverflowerror is thrown. 2. When the extended memory cannot be applied, outofmemoryerror is thrown.

3 native method Stack: similar to VM stack, it is used to call services for native and can also throw stackoverflowerror and outofmemoryerror

Heap: the largest memory block managed by JVM. It is a memory area shared by all threads. It is created at VM startup and stores object instances for a unique purpose. Almost all object instances and arrays must be allocated on heap. Heap is the main area for GC management. It can be physically discontinuous memory space, as long as it is logically consecutive. If heap does not have memory for instance allocation and cannot be expanded, an outofmemeoryerror is thrown.

Method Area (non-heap): Same as heap, each thread shares the memory area, used to store information about classes loaded by VMS, constants, static variables, and compiled real-time compilers.Code. GC behavior rarely occurs in this region. The goal of GC is mainly for constant pools and uninstallation of types. In general, the collection result is hard to satisfy. When the method cannot meet the memory allocation, an outofmemoryerror is thrown.
The runtime constant pool is part of the method area. In addition to the version, field, method, and interface of the class, the class file also contains the constant pool table, which is used to store the literal volume and symbol reference generated during compilation, this will be stored in the runtime constant pool after the class is loaded.

Direct Memory is not part of the VM runtime data zone, nor is it the memory region defined in the JVM specification. However, it is also frequently used and can cause outofmemoryerror.

Object Access:
Instant and simplest access also involves the relationship between stack, heap, and method zone.
Object OBJ = new object (); "Object OBJ" will be reflected in the local variable table of the stack. It will appear as a reference type, and "New obejct ()" will be reflected in heap, form a structured memory that stores the data values of all instances of the object type. In addition, heap must contain the address information that can be found for this object type data (such as the object type, parent class, implemented interface, method, etc. In the legal area.
Different VMS implement different object access methods. There are two mainstream Methods: handle access and direct pointer. P31.
The biggest advantage of handle access is that the reference stores a stable handle address. when an object is moved, you only need to change the instance data pointer in the handle, but the reference does not need to be changed.

The biggest advantage of direct pointer access is that the speed is faster, saving the time overhead of pointer positioning, but you need to change the reference.

If you reprint it, describe the source! Http://blog.csdn.net/xukunddp

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.