Java memory model

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 contain the following several execution-time data regions, for example, as seen in:

The following is a description of each region.

Back to top Ii. Data area program counter at execution time

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

In multiple threads, each thread needs to have a separate program counter in order for the thread to switch back to the correct run location. Each thread is independent of each other and stored independently, so this memory is thread-private .

When a thread is running a Java method. This counter records the address of the bytecode instruction in the running virtual machine, and when the native method is running, the counter value is empty.

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

Java Virtual Machine stack

  The Java Virtual machine stack is also thread-private , and its life cycle is the same as the thread.

The virtual machine stack description describes the memory model in which the Java method runs: Each method creates a stack frame at the same time 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).

Assume that you cannot request enough memory for the extension. The OutOfMemoryError exception is thrown.

Local method Stack

  The local method stack is similar to a virtual machine, except that the virtual machine stack is a Java method service run by the virtual machine, 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 an area of memory that is shared by all threads. Created when the virtual machine is started. The only purpose of this memory area is to hold object instances .

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 also 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, just logically contiguous.

In implementation, it is possible to achieve both fixed-size and extensible.

Assume that there are no memory-complete instance allocations in the heap, and that the heap cannot be extended at completion. A OutOfMemoryError exception will be thrown.

Method Area

  A method area is a region of memory shared by each thread. It is used to store data such as class information, constants, static variables, instant compiler compiled code, etc. that have been loaded into the virtual aircraft .

The garbage collection behavior is relatively rare in this area, but it is not the data that is in the method area for a permanent existence. The memory recovery target for this area is primarily for the collection of constant pools and for the unloading of types,

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

To perform 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 when the virtual machine executes. Introducing a channel-to-buffer IO approach in the NIO class, it is possible to use the native library to directly allocate out-of-heap memory and then operate through a Directbytebuffer object stored in the Java heap as a reference to this memory.

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

Back to TopIii. the creation, layout and interview process of objectsCreation of Objects

Creating an object generally requires newkeyword. When a virtual opportunity comes to a new instruction, first check whether the parameter of this instruction is positioned in the constant pool for the symbolic reference of a class, and check that the class represented by the symbol reference is loaded, parsed, and initialized. Assume that the corresponding class loading process is run.

a pointer collision , if the memory in the Java heap is absolutely regular, The used and spare memory is on one side, and a pointer is placed in the middle to serve as the indicator of the dividing point. Allocating memory is to move that pointer over the spare space to a distance equal to the size of the object.

There is also a list called spare : If the memory in the Java heap is not structured. A virtual machine is required to maintain a list. Record which memory block is 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 to use is determined by the regularity of the Java heap, and whether the Java heap is structured is determined by the use of the garbage collector with the compression finishing function.

Another problem that needs to be considered is the thread safety problem at the time of object creation, with two solutions: one is to synchronize the action of allocating memory space. There's another way. Memory allocation actions are performed in different spaces according to the thread. That is, each thread pre-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.

Then the virtual confidential object to make the necessary settings, such as the object is which class instance, how to find the class metadata information, etc., the information is stored in the object's object header.

When the above work is finished, a new object has been created from the virtual machine's point of view. However, from the Java program Point of view, you also need to run the Init method. Initializes the object according to the will of the program ape, so that a truly usable object is fully produced.

Memory layout of the object

In the 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 contains two parts: the first part is used to store the execution time 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 the type pointer. A pointer to the class metadata of the object. The virtual machine uses this pointer to determine which class the object is an instance of.

Instance data is a valid information that an object is actually stored. are also the various types of field content defined in the program code.

The alignment fill does not necessarily exist, only the placeholder. , the hotpot VM requires that the object start address must be an integer multiple of 8 bytes, and 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 targeting for objects

The Java program uses the reference data on the stack to manipulate the detailed objects on the heap. The basic way to access it is to use the handle and the direct pointer two kinds:

  Handle : The Java heap will draw a chunk of memory as a handle pool, and the reference stores the handle address of the object. The handle includes detailed address information for the object instance data and the type data respectively . What you see:

  

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

What you see:

  

Two ways each have advantages, the 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 advantage of referencing without changes and using direct pointers is that it is faster. It saves time overhead for pointer positioning.

Java memory model

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.