search
HomeJavajavaTutorialWhat is Java object storage memory layout method

Java Object Storage Memory Layout

As we all know, Java is an object-oriented language, so what does an object contain in memory? First of all, Most objects are stored on the heap (except escape ).

Then the object storage in the heap is mainly divided into three parts

  • Object header, object instance data, alignment supplement (the array will have one more Array length)

  • Object header:

    mark word: Stores the hashCode and lock information of the object ( Lock upgrade) or information such as generation age or GC flag

    Type pointer: Stores a pointer to the class to which the object belongs (class file in metadata), the JVM passes This determines which class this object belongs to

  • Object instance data:

    The object information generated by new stores the attribute data information of the class, including the parent Class attribute information;

  • Alignment supplement

    Array objects will be filled with multiple alignments

    JVM requires object occupation The space must be a multiple of 8 to facilitate memory allocation (allocated in bytes as the minimum unit), so this part is used to fill up insufficient space to make up the number.

Access positioning of Java objects

  • The mainstream access methods mainly include handleanddirect Pointer

  • Handle:

    A piece of memory is divided into the Java heap as a handle pool, and the object stored in the reference in the stack The handle address, and the handle contains the specific address information of the object instance data and type data

  • direct pointer:

    java heap The memory layout of the object must consider how to prevent access to relevant information of the type data. What is stored in the reference is directly the object address.

  • Direct pointer access to the object does not require an additional indirect access overhead, and the handle is convenient. When the object address changes (garbage collection will move the object address), you only need to change the pointer reference itself in the handle. No need to change.

The creation process of Java objects

  • When the virtual machine encounters the new instruction, it first checks whether the specified class is loaded and verified , Prepare (Allocate memory space for all static variables in the class, and set an initial value for them ), Parse and initialize.

  • After the class check, the virtual machine allocates memory for new objects

    How to ensure the safety of heap memory allocated in concurrent situations

    The virtual machine uses CAS to configure failure recovery Try to ensure atomicity

    Leave memory allocation to threads, allocate space when creating threads, and leave the task of allocating memory to threads. Open through TLAB (Thread local Allocation Buffer)

  • After allocating the memory, set the object header, such as which class instance, hashcode, class metadata information pointer (method area)

  • Execute the init method (kernel method), initialize member variables, execute the instantiation code block, call the construction method of the class, and assign the first address of the object in the heap to the reference variable.

Is the memory allocated by Java objects thread-safe?

  • CAS adds failure retries to ensure the atomicity of updates.

  • Divide the memory allocation into different spaces according to threads, that is, each thread pre-allocates a small block of memory in the Java heap, which is called the local thread allocation buffer TLAB. Which thread wants to allocate memory? Allocate in the corresponding TLAB, and synchronize after the TLAB is used up.

Java class instantiation sequence

  • Static members and static initialization blocks of the parent class are executed in the order they appear in the code

  • Subclass static members and static initialization blocks are executed in the order they appear in the code.

  • Parent class instance members and instance initialization blocks are executed in the order they appear in the code. Execute

  • parent class construction method

  • subclass instance members and instance initialization blocks in the order they appear in the code. Execute the

  • subclass construction method in sequence

The above is the detailed content of What is Java object storage memory layout method. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment