Java memory Partitioning, memory allocation principle (in-depth understanding of JVM one)

Source: Internet
Author: User

The AVA virtual machine divides the managed memory into several different data regions during Java execution. These zones have their own uses, as well as the creation and destruction of time, and some regions exist as virtual machine processes start, while others are created and destroyed depending on the start and end of the thread.

The Java Virtual machine includes the following run-time data regions:



Program Counter

A program counter is a small area that acts as a line number indicator of the bytecode that is being executed by the current thread. In the virtual machine model, the bytecode indicator specifies the next instruction to be executed by changing the value of the program counter. The basic functions of branching, looping, etc. are dependent on the program counter to complete.

Because the multithreading of a Java Virtual machine is done by taking turns switching and allocating processor execution time, a single processor executes only one thread of instruction at a time. In order to recover the correct execution location after a thread recovery, each thread needs a separate program counter to ensure that the threads do not affect each other. So the program counter is "thread-private" memory.

If the virtual machine is executing a Java method, the counter specifies the address of the bytecode directive, and if a local method is being executed, the counter specifies that the undefined is empty. The program counter area is the only area in the Java virtual machine that does not have a OutOfMemory exception defined.


Java Virtual machine stack

The same as the program counters are 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 for storing local variable tables, operations stacks, dynamic links, method exits and other information. The process by which each method is called corresponds to a stack frame in the virtual machine stack from the stack to the stack.

Commonly referred to as virtual machine runtime is divided into stacks and heaps, where the stack refers to the VM stack or virtual machine stack in the local variable table part.

The local variable table holds the various basic data types, object references, and returnaddress types known to the compiler (addresses that point to a bytecode directive). The memory space required by the local variable table is fully determined when the compiler completes the allocation, and when entering a method this method needs to allocate much memory space in the frame, which does not change the size of the local variable table during the run. (A long and double of length 64 takes up two local variable space, and the other data types occupy one)

There may be two types of exceptions for the Java Virtual machine stack: 1. The stack depth requested by the thread is greater than the stack depth allowed by the virtual machine, and Stackoverflowerror will be thrown. 2. The virtual machine stack space can be dynamically extended, throwing a outofmemory exception when the dynamic extension is unable to request enough space.


Local Method Stack

The local method stack and the virtual machine stack are basically similar, except that the Java virtual machine stack executes Java code (bytecode), and the local method stack executes the service of the local method. Stackoverflowerror and OutOfMemory exceptions are also thrown in the local method stack.

Heap

a heap is the largest piece of memory managed by a Java virtual machine. A heap is an area shared by all threads that is created when a virtual machine is started. The only purpose of the heap is to hold object instances, where almost all of the object instances are allocated, but with the development of the JIT compiler and the maturation of escape techniques, stack allocation and scalar substitution techniques make this situation subtly changing, and the allocation is becoming less absolute.

Attached: In the Java programming language and environment, the instant compiler (JIT Compiler,just-in-time compiler) is a program that translates Java bytecode (including programs that need to be interpreted) into instructions that can be sent directly to the processor. When you have written a Java program, the source language's statements will be compiled into bytecode by the Java compiler, rather than compiled into a directive code corresponding to a particular processor hardware platform (for example, Intel's Pentium microprocessor or IBM's system/390 processor). Bytecode is platform-independent code that can be sent to any platform and can be run on that platform.

The Java heap is the primary area of garbage collector management, so it is also known as the GC heap. Since the garbage collectors are basically using generational collection algorithms, the Java heap can be subdivided into: Cenozoic and laosheng generations. In detail can be divided into Eden (Eden area) space, from Survivor space, to survivor space (Survivor area). If viewed from a memory allocation perspective, a thread-shared Java heap can divide multiple thread-private allocation buffers. However, regardless of the partition, it is not related to the content, no matter what area, is used to store object instances. The purpose of the subdivision is to better reclaim memory or to allocate memory more quickly.

Java heap can be a physically discontinuous space, as long as logically continuous, mainstream virtual machines are implemented in an extensible manner. If there is no memory in the current pair to complete the creation of the object instance, and the memory extension cannot occur, the OutOfMemory exception is thrown.


Method Area

The method area is also a thread-shared area used to store data such as class information, constants, static variables, and immediate compiler (JIT) compiled code that have been loaded by the virtual machine. the Java Virtual machine describes the method area as a logical partition of the heap, but the method area has an alias Non-heap (not a heap) that is used to differentiate it from the Java heap.

The Java Virtual Machine specification also has a very loose limit on this area, allowing for fixed size and extensibility, as well as non-garbage collection, in addition to space that can be physically discontinuous. In contrast, garbage collection behavior is less frequent in this area (so the definition of constants and static variables is more important). The memory collection of the method area will still occur, but the memory collection in this area is primarily for the collection of constant pools and the unloading of types.

In general, the method area of memory recovery is very difficult to satisfy. The OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.

run a constant-rate pool

running a constant pool is part of the method area, in addition to the classes ' versions, fields, methods, interfaces, and so on, there is also a message that the const pool is used to store the various literal and symbolic references generated by the compiler, and this information is stored in the run-time pool of the method area after the class is loaded . Java virtual machines have strict rules for every part of a class, including Chang, and each byte is used to store which data must have a specification, so that it can be recognized, loaded, and executed by the virtual machine. In general, in addition to saving the symbolic references described in the class file, the translated direct references are 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 that it is dynamic, and the Java Virtual machine does not require constants to be generated only at compile time, that is, content that is not pre-placed into the class file constant pool to enter the run-times pool of the method area. New constants can also be placed in a constant pool during run time.

Chang is part of the method area, so it is limited by memory and throws a OutOfMemoryError exception when it cannot request enough memory.

Object Access

Object access is ubiquitous in the Java language, and even the simplest accesses involve the association between the Java stack, the Java heap, and the three most important memory areas of the method area. As in the following code:

Object obj = new Object ();

Assuming this code appears in the method body, the semantics of the "Object obj" section will be reflected in the local variable table of the Java stack, as a reference type of data exists. and "New Object ();" The semantics of the section will be reflected in the Java heap, forming a piece of structured memory that stores all instance data values of type Object (Instance), and the length of this memory is not fixed depending on the type and the distribution of objects implemented by the virtual machine. Also, in the Java heap, you must include address information that can find memory data for this object, which are stored in the method area.

because the reference type specifies a reference to the object in the Java virtual machine, it does not specify the way in which the reference is to be located, and the specific location of the objects in the Java heap, so the virtual machine implements the object access differently. There are two main ways to access it: handle access and direct pointers.

1. If you use handle access, a chunk of memory will be partitioned into the Java heap as the handle pool, where the address of the object is stored, and the handle contains the specific address information of the object instance data and the type data reference.

2. If accessed by a direct pointer, the layout of the Java heap object must consider how to place information about the access type data, and the address of the object is stored directly in the reference.

There are advantages to both ways, and the biggest benefit of the Bureau and access method is that the reference is stored in a stable handle address, when the object is moved, only the instance data pointer in the handle is changed, and the reference itself does not need to be modified. The greatest advantage of pointer access is the speed, which saves the overhead of a pointer-to-location, and because object access is very frequent in Java, once such overhead is a very significant cost.

Specific access methods are specified by the virtual machine, and the virtual machine Sun hotspot uses a direct pointer approach, but it is common to see how the various languages and frameworks use handle access from the overall scope of software development

Java memory Partitioning, memory allocation principle (in-depth understanding of JVM one)

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.