当前位置:网站首页>Object allocation, layout, and access in the JVM heap

Object allocation, layout, and access in the JVM heap

2020-12-08 14:01:38 osc_ 3aqbiyys

Click on the above In blue , choice “ Official account of star sign ”

High quality articles , First time delivery

  author  |   Whispering without words

source |  urlify.cn/a6v22m

1、 Object creation

Java It is an object-oriented language ,Java Objects are created all the time during the program running . From the language level , Creating an object is just a new It's just keywords , In the virtual machine , object ( Only ordinary Java object , Exclude arrays and Class Object etc. ) What is the process of creating ?

With Hotspot Take a virtual machine , When the virtual machine encounters a bytecode instruction , First, it checks whether the parameter of this instruction can locate a symbol reference in the constant pool , And check whether the class represented by this symbol reference has been loaded 、 Parse and initialize , without , Then the corresponding class loading process will be executed first

After the class load check passes , Virtual machine will allocate memory for new objects . The size of memory required by an object can be completely determined during class loading , The task of allocating space for an object is actually equivalent to taking a certain size area of memory from Java Divide it up in the heap , And assign to objects . There are two main ways to divide memory :

  • Pointer collision method

    That is, the assumption Java The memory in the heap is absolutely regular , The memory that has been used is put aside , Free memory on the other side , There is a pointer in the middle as an indicator of the dividing point , So when you need to allocate memory , Just move the pointer to the free space a distance equal to the size of the object

  • Free list method

    This is the case Java The memory in the heap is not regular , Used memory and free memory are interleaved with each other . At this point, the virtual machine must maintain a list , Record which memory blocks are available , When allocating, find a large enough space from the list to divide it into object instances , And update the list record

thus it can be seen , Select which allocation method is determined by Java Whether the pile is regular or not , and Java Whether the heap is regular or not depends on whether the garbage collector used has the ability to compress the space

Besides how to divide the available space , There's another question to consider : Object creation is a very frequent behavior in virtual machines , Even if it's just changing the position of the pointer , It's not safe in concurrent situations , It's possible that someone is being given A Allocate memory , The pointer hasn't been modified yet , object B At the same time, the original pointer is used to allocate memory . There are two ways to solve this problem : One is to synchronize the action slices that allocate memory space , In fact, virtual machine adopts CAS With the failure retrial mechanism to ensure the atomicity of the update operation ; The other is to divide memory allocation into different spaces according to threads , That is, each thread Java A small chunk of memory is pre-allocated in the heap , Called local thread allocation buffer (Thread Local Allocation Buffer), abbreviation TLAB, Which thread will allocate memory , It is allocated in the local buffer of the thread , Only the local buffer runs out , Synchronous locking is required only when a new buffer is allocated . Whether the virtual machine uses TLAB, Can pass -XX:+/UseTLAB Parameters to set

After the memory allocation is complete , The virtual machine must first allocate the memory space to ( But it doesn't include the object head ) All initialized to zero , If used TLAB Words , This work can also be advanced in TLAB By the way . This operation ensures that the instance field of the object is in Java Code can be used directly without initial value , Enables the program to access the zero values corresponding to the data types of these fields

Next ,Java The virtual machine also needs an object header on the object (Object Header) Set it up , The object header holds information such as which instance of the object the object is 、 How to find the metadata information of a class 、 Object's hash code 、 Object's GC Generational age, etc

When the above work is finished , From the perspective of virtual machine , A new object has been born , And from Java From a procedural point of view , Object creation just started , Object's constructor , namely Class Of documents <init> Method has not been executed , All fields are zero by default .new The command will be executed later <init> Method , Initialize the object , Such a really usable object can be considered as fully constructed

2、 Memory layout of objects

stay HotSpot In the virtual machine , The storage layout of objects in heap memory can be divided into three parts : Object head (Header)、 The instance data (Instance Data) And align fill (Padding)

1. Object head

Hotspot The object header of virtual machine object includes two kinds of information , The first is used to store the runtime data of the object itself , Such as hash code 、GC Generational age 、 Lock status flag 、 Thread holds lock 、 To the thread ID、 Directional timestamps, etc . The length of this part of data is 32 Bit and 64 In bit virtual machine, they are 32 Bit bit sum 64 A bit , Officially called “Mark Word”

But actually , Objects need to store a lot of data to be run , Already exceeded 32、64 position Bitmap The maximum structure can record , therefore Mark Word Designed as a data structure with dynamic definition , In order to reuse their own storage space as much as possible , For example, in 32 position JVM It's stored in this way :

Another part of the object header is the type pointer , That is, the pointer of the object to its type metadata ,Java The virtual machine uses this pointer to determine which class the object belongs to . However , Not all virtual machines keep type pointers on object headers , let me put it another way , The metadata information of an object does not have to go through the object itself . Besides , If the object is an array , Then there must also be a piece of data in the object header to record the length of the array

2. The instance data

The instance data part is the real effective information stored by the object , That is, we define various types of field content in the code , Whether inherited from the parent class , Fields defined in subclasses must be recorded

3. Alignment filling

Alignment filling is not inevitable , It's just a placeholder , because Hotspot The automatic memory management mechanism of virtual machine requires that the size of the object must be 8 Integer multiples of bytes . therefore , If the data part of the object instance is not aligned , It needs to be filled by aligning the padding

3、 Object access location

Java The program will go through the stack reference Data to manipulate specific objects on the heap . stay Java In the virtual machine specification , Just the rules reference A type is a reference to an object , There is no definition of how to implement . Therefore, the object access mode is implemented by the virtual machine independently , There are two main access methods: handle and direct pointer :

  • Handle access

    Java A chunk of memory is divided in the heap as a pool of handles ,reference Is the handle address of the object , The handle contains the specific address information of object instance data and type data

Direct Pointers

  • reference The object address is stored in , So if you just access the object itself , It can avoid multiple overhead . in addition , If you use direct pointer access ,Java The memory layout of objects in the heap must consider how to place the relevant information for accessing type data

The two ways are different , The advantage of using handles is that reference A stable handle address is stored in , If the object is moved ( Such as garbage collection ), Then only the instance data pointer in the handle will be changed , and reference There is no need to modify itself . And the advantage of using a direct pointer is that it's faster , Save time . stay HotSpot Virtual machine mainly uses direct pointer to access objects


If you think the article is good , At the end of the article ???? It's back , Remember to give it to me 「 give the thumbs-up 」 and 「 Looking at 」 Oh ~

版权声明
本文为[osc_ 3aqbiyys]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208140037301a.html