Deep understanding of Java memory areas of Java virtual machines

Source: Internet
Author: User
Tags array length garbage collection

run-time data region

Deep understanding of Java memory areas of Java virtual machines
Image.png

Program counter

The line number indicator of the byte code executed by the current thread

Java Virtual machine stack (memory model for Java methods)

Each method creates a stack frame at the same time to store information such as local variable tables, operand stacks, dynamic links, method exits, and so on.

The local variable table can hold the base data type, the reference type, the ReturnAddress type (the address that points to a byte code directive)

The long and doube types of data reuse 2 local variable table spaces, while the remaining data types only occupy 1.

Local method Stack

Similar to the Java Virtual machine stack, except that the Java virtual machine stack Executes the Java method, and the local method stack serves the native method

Java heap

All object instances and arrays are allocated on the heap.

Main areas of garbage collector management

New generation, old age, Eden Space, survivor space ...

Method area

Used to store data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine.

Version of the class
Field
Interface
Method
Run a constant-rate pool

Part of the method area. class file In addition to the class version, fields, methods, interfaces, and other descriptive information, there is also a constant pool, used to hold the various literal and symbolic references generated during compilation, this part of the class will be loaded into the method area of the run-time constant pool.

For example:

String s1 = "abc";//放入常量池String s2 = "abc";//放入常量池(可以理解常量池为 HashSet)//常量池s3 引用堆内存中的 new String("abc");String s3 = new String("abc");s1 == s2 //trues1 == s3 //false深入理解Java虚拟机 之 Java 内存区域image.png

Direct Memory

Object

Creation of objects

1, when the virtual opportunity to the memory creation instruction, came to the method area, find the method area there is no symbolic reference (class information exists in a primitive form, string)

2, check that the symbol reference has been loaded, parsed and initialized, if not the class loading process, otherwise directly ready to allocate memory for the new object

3, the allocation of memory into the pointer collision and the free list of two ways, allocating memory also to ensure concurrency security, there are two ways.

Pointer collision: As long as the free space in heap memory is very regular, use and unused space is all continuous, just move the pointer to

Idle list: Virtual machines maintain a list of what is allocated and where they are idle for the presence of space scattered in the heap memory

CAS commands the way to control the operation is synchronous

Local thread allocation buffer Tlab (thread local Allocation buffer): Allocates a small piece of independent memory for each thread in the heap, so there is no concurrency problem, and the Java level corresponds to the implementation of the ThreadLocal class

4, after allocating memory to initialize the object header, this new information includes: the object corresponding to the class metadata, the object's GC generation, the object hash code

5. Finally, after the creation of a new object, it is necessary to execute the command in the constructor to complete the initialization of the Java plane, which is the method in the JVM. To this a new object is produced, ready to be used.

The memory model of the object

The header of the object header

There are two types of information in the object header: The flag information (Mark Word) and the Meta information Pointer (Kclass Pointer)

Identity information is used to hold the state of some intrinsic properties of an object, which are created from the object, rather than by the user of Java:

Hash code: Unique identifier of the object
Generational age of objects: related to garbage collection
Locks held by Threads
The state of the lock
Biased to thread ID, bias timestamp
Array Length: If the object is an array, there will be array length information
The meta-information pointer is a pointer to the class meta information in the method area.

Information about the instance

The information for the instance holds information that is really valid for Java users, that is, the fields defined in the class, including those that inherit from the parent class.

Snap To fill

Whether or not the memory segment is populated depends on the length of the previous two parts, in order to ensure that the object memory model is 8 bytes long, which is the requirement of automatic memory management of the virtual machine.

Working with objects

After the object is created, a local variable table is maintained in the virtual machine stack that stores the values of the underlying type and underlying type, and the reference type and the value of the reference type.

Where the value of the reference type is the object address in the heap. There are two ways to refer to addresses in a heap:

Handle: Maintains a pool of handles in the heap, with the object address contained in the handle, and when the object changes, simply change the handle, without changing the reference to the local variable table in the stack
Direct pointer: The object's address is stored directly in the stack, and the benefit is that the access speed becomes faster
Deep understanding of Java memory areas of Java virtual machines

Deep understanding of Java memory areas of Java virtual machines

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.