Java Virtual machine memory model

Source: Internet
Author: User

1. Composition of the Java memory area

The memory of the Java Virtual Runtime is mainly composed of five parts:

(1) Method area

The method area is used to store data such as class information, constants, static variables, and so on, that are loaded by the virtual machine. The string constant pool is located in the method area.

(2) heap

The heap is used to store object instances, and the object instances created by new in Java are saved in the heap.

(3) Virtual machine stack

The virtual machine stack is used to implement the method invocation, each time the method call corresponds to a stack frame in the stack, the stack frame contains the local variable table, the operand stack, and the method interface equals the information about the method.

(4) Local method stack

The local method stack is similar to a virtual machine stack, except that it is a call to a local method (a system method implemented by another language). In fact, in the implementation of the Hotspot virtual machine, the virtual machine stack and the local method stack are merged together.

(5) Program counter

The function of the program counter is equivalent to the function of the PC register, if the Java method is currently executing, the address of the current bytecode instruction is indicated, and if the local local method is executed, the value is undefined.

Where the method area and heap are thread-shared memory, the virtual machine stack, the local method stack, and the program counter are thread-independent memory, each thread only holds its own data.

2. Virtual machine Exceptions

The exception thrown by the virtual machine is an object of the error class and is an error that we cannot resolve.

The program counter does not throw an error;

The depth of the method call in the virtual machine stack exceeds the maximum depth specified by the virtual machine, then the Stackoverflowerror is thrown, the virtual machine stack can be dynamically extended, and if there is not enough memory in the extension, the OutOfMemoryError is thrown;

The local method stack, like the virtual machine stack, throws Stackoverflowerror and OutOfMemoryError;

The method area and heap will throw outofmemoryerror when they cannot meet the allocation requirements;

3. Objects in a virtual machine

(1) Creation of objects

When executing a new command, the method area is first checked to see if the corresponding class has been loaded, and if not, the corresponding class information is loaded into the method area first;

The virtual machine allocates a chunk of memory in the heap to store the object instance and initializes the value of the object memory space to 0;

The contents of the object header are set by the virtual machine, and the object-related information is stored in the header;

Executes the constructor method to initialize the object;

(2) Storage of objects

Objects are stored in three parts of the virtual machine: The object header, the instance data, and the alignment padding.

The object header includes two parts, the first part stores the object related run-time information, such as: Hash Code, GC generational age, lock status flag, etc. the second part is a type pointer that indicates the type to which the object belongs.

Instance data stores valid data for an object, followed by two principles when storing data, first following the principle of having type data of the same length together, followed by the principle of storing the attribute of the child class after storing the parent class attribute.

The aligned padding does not store meaningful content and is used only as a placeholder for memory address requirements.

(3) Access to Objects

Access objects are referenced by objects, references are saved in the stack, and instances of objects are accessed by reference and types in two ways:

With a handle, if you use this method, there is a handle pool in the Java heap that holds the address of each object instance and the address of the type to which the object belongs, while the object's reference points to the handle in the handle pool;

With a direct pointer, if you use this method, the address of the type to which the object belongs is stored when the object instance data is stored in the Java heap;

Each of these two approaches has advantages:

The first way when an object instance is moved is to change the address of the object instance in the handle without modifying the object reference;

The advantage of the second approach is that access is faster, saving the overhead of locating an object instance by pointer, which is what the hotspot virtual machine is using.

Java Virtual machine 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.