Java Memory management principles and memory area detailed

Source: Internet
Author: User

I. Overview

The Java Virtual machine performs a Java program by dividing the memory it manages into several different data regions that have their own purpose and time for creation and destruction. The memory managed by the Java Virtual machine will include the following runtime data regions, as shown in:

The following is a description of each area.

Second, run-time Data Area program counter

The program counter, which can be seen as the line number indicator of the byte code executed by the current thread. In the virtual machine conceptual model, the bytecode interpreter works by changing the value of the program counter to select the next byte-code instruction that needs to be executed, such as branching, looping, jumping, exception handling, thread recovery, and other basic functions that rely on this counter to complete.

In multi-threading, in order for a thread to switch back to the correct execution location, each thread needs to have a separate program counter that is independent of each other and stored independently, so the memory is thread-private.

When the thread is executing a Java method, this counter records the address of the bytecode instruction of the virtual machine being executed, and when the native method is executed, the counter value is empty.

This memory area is the only area that does not specify any outofmemoryerror conditions.

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 the Java method executes: Each method creates a stack frame to store the local variable table, the operand stack, the dynamic list, the method exit information, and so on. Each method from the call until the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack of the process.

The local variable table holds the various basic data types known to the compiler (Boolean, Byte, char, short, int, float, long, double), object reference, and ReturnAddress type (which points to the address of a bytecode directive).

If you cannot request enough memory for the extension, the OutOfMemoryError exception is thrown.

Local method Stack

The local method stack is similar to the virtual machine, except that the virtual machine stack is the Java method service that the virtual machine executes, and the local method stack is the native method service used by the virtual machine. Some virtual machines combine the local method stack and the virtual machine stack directly.

Throws Stackoverflowerror and OutOfMemoryError exceptions.

Java heap

The Java heap is a piece of memory that is shared by all threads and is created when the virtual machine is started, and the only purpose of this memory region is to hold the object instance.

The Java heap is the primary area of garbage collector management. Because the collector now basically uses the generational recovery algorithm, so the Java heap can be subdivided into: the new generation and the old age. From the memory allocation point of view, the thread-shared Java heap may divide multiple thread-private allocation buffers (Tlab).

The Java heap can be in a physically discontinuous memory space as long as it is logically contiguous. In implementation, both fixed-size and extensible can be implemented.

A OutOfMemoryError exception is thrown when there is no memory in the heap to complete the instance assignment, and the heap cannot complete the extension.

Method area

A method area is a region 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.

Relatively speaking, garbage collection behavior in this area is relatively small, but not the data into the method area of permanent existence, the region's memory recovery target is mainly for the recovery of the constant pool and the type of unloading,

The OutOfMemoryError exception is thrown when the method area does not meet the memory allocation needs.

To run a constant-rate pool:

is part of the method area, which is used to hold various literal and symbolic references generated during the compilation period.

Direct Memory

Direct memory is not part of the data area of the virtual runtime, and a channel-to-buffer Io method is introduced in the NIO class, which can allocate out-of-heap memory directly using the native function library. It then operates as a reference to this memory through a Directbytebuffer object stored in the Java heap.

The allocation of direct memory is not limited by the size of the Java heap, but is limited by the size of the native memory, and all may throw outofmemoryerror exceptions.

Iii. creation, layout, and access of objects

Creation of objects

Creating an object usually requires the new keyword, and when the virtual opportunity is to a new instruction, first check that the parameter of the directive is positioned in the constant pool for the symbolic reference of a class, and check that the class represented by the symbol reference has been loaded, parsed, and initialized. If you do so, the appropriate class loading process is performed.

After the class load check passes, the virtual machine allocates memory for the new object. The task of allocating space to an object is equivalent to dividing a certain size of memory from the Java heap. There are two ways of allocating: A pointer collision, which assumes that the memory in the Java heap is absolutely regular, that the used and idle memory is on one side, that a pointer is placed in the middle of the point indicator, and that the allocation of memory is to move that pointer over the free space to a distance equal to the object's size. Another kind called idle list: If the memory in the Java heap is not structured, the virtual machine needs to maintain a list of which memory blocks are available, find a large enough space in the list to be allocated to the object instance, and update the records on the list. Which allocation method is determined by whether the Java heap is structured or not, and whether the Java heap is structured is determined by whether or not the garbage collector used has a compression function. Another problem that needs to be considered is the thread safety at the time of object creation, and there are two solutions: one is to synchronize the actions of the allocated memory space, and the other is that the memory allocation action is carried out in different spaces by thread, that is, each thread allocates a small chunk of memory (Tlab) in the Java heap. Which thread allocates memory is allocated on the tlab of which thread, and only when Tlab runs out and assigns a new Tlab requires a synchronous lock.

After the memory allocation is complete, the virtual machine needs to initialize the allocated memory space to a value of 0. This step ensures that an instance field of an object can be used directly in Java code without assigning an initial value.

The virtual machines then make the necessary settings for the object, such as which instance of the class it is, how to find the metadata information for the class, and so on, which is stored in the object header of the object.

Once the above work is done, a new object has been created from the perspective of the virtual machine. But from the Java program's point of view, you also need to execute the Init method to initialize the object according to the programmer 's will, so that a truly usable object is fully generated.

Memory layout of the object

In a hotspot virtual machine, the layout of objects stored in memory can be divided into three parts: object header, instance data, and alignment padding.

The object header consists of two parts: the first part is used to store the runtime data of the object itself, such as hash code, GC generational age, locks held by threads, and so on. Officially known as "Mark Word". The second part is a pointer to the type, which refers to the object's class metadata, which the virtual machine uses to determine which class the object is an instance of.

Instance data is a valid information that is actually stored by the object, as well as the various types of field content defined in the program code.

The alignment padding does not necessarily exist, only the placeholder function. , the hotpot VM requires that the object start address must be an integer multiple of 8 bytes, the object header portion is exactly a multiple of 8 bytes, so when the instance data part is not aligned, it needs to be aligned by aligning the padding.

Access positioning of objects

The Java program uses the reference data on the stack to manipulate the concrete objects on the heap. The main access methods are the use of a handle and a direct pointer two:

Handle: The Java heap will draw a chunk of memory as the handle pool, and the reference stores the handle address of the object, and the handle contains the individual address information for the object instance data and the type data. :

Direct pointer: The layout of the Java heap object considers how to place information about the access type data, which is the object address stored in the reference. :

Two ways each have advantages, the most advantage of using a handle is that the reference is stored in a stable handle address, when the object is moved only changes the address of the instance in the handle, the reference does not need to be modified, the advantage of using direct pointer access is faster, it saves the time overhead of pointer positioning.


Java Memory management principles and memory area detailed

Related Article

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.