当前位置:网站首页>jvm-04. Object's memory layout

jvm-04. Object's memory layout

2022-06-23 05:56:00 CaptainCats

Memory layout of objects

Object creation

Object o = new Object();

There is a variable in memory o Point to new Out object ,
The blue area represents the object .
 Insert picture description here

Semi initialization problem

adopt new There are three steps to creating objects :
The first step is to apply for memory allocation , This step assigns default values to member variables ,
The second step is to call the constructor , This step assigns the initial value to the member variable ,
Step 3: establish the association between pointer and object .

The first step is to solve c++ The problem of variable legacy values ,
Is the value left by the last program accessing this space , To be safe, set the default value first .

The layout of objects

Common object

It is divided into 4 Parts of :

markword

8 Bytes

class pointer

4 Or 8 Bytes , Pointing to corresponding class

instance data

Object's internal data , For example, member variables

padding

Concatenate the length of the number of bytes so that it can be 8 to be divisible by
 Insert picture description here
We can use JOL Class library to demonstrate

<!-- https://mvnrepository.com/artifact/org.openjdk.jol/jol-core -->
<dependency>
    <groupId>org.openjdk.jol</groupId>
    <artifactId>jol-core</artifactId>
    <version>0.16</version>
    <scope>provided</scope>
</dependency>

ObjectLayout

package com.duohoob.jvm.bytecode;

import org.openjdk.jol.info.ClassLayout;

@SuppressWarnings("unused")
public class ObjectLayout {
    

	private static class T {
    
		int a;
		int b;
		String str = new String("abc");
	}
	
	public static void main(String[] args) {
    
		T t = new T();
		System.out.println(ClassLayout.parseInstance(t).toPrintable());
	}
	
}

function

com.duohoob.jvm.bytecode.ObjectLayout$T object internals:
OFF  SZ               TYPE DESCRIPTION               VALUE
  0   8                    (object header: mark)     0x0000000000000001 (non-biasable; age: 0)
  8   4                    (object header: class)    0xf800c043
 12   4                int T.a                       0
 16   4                int T.b                       0
 20   4   java.lang.String T.str                     (object)
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

String Quoting share 4 Bytes , That is to say 32 position ,
Why not 8 Bytes 64 Bit is because the compressed pointer is used ,
How many people do we often talk about , Represents in memory 2 Of n The next address ,
32 The maximum number of bit computers can only use 4G Of memory .

Array

Multiple array lengths
 Insert picture description here

What does the object header contain

The object header contains markword、class pointer Information ,
markword contain hashCode Information 、synchronized Lock information 、GC Recycling marks .
GC The mark contains three color mark information and generation age .

Object positioning

Direct Pointers

In the heap are instance objects , In the method area is the type class.
 Insert picture description here

Indirect pointer

advantage : Garbage collection doesn't have to be changed frequently t.
shortcoming : slow , You need to locate the pointer first , Then navigate to the object , Two visits .
 Insert picture description here

The allocation of objects

Local variables in functions ,
new You will first try to allocate space within your own stack frame ,
for example for Pass through... In a cycle new Create objects ,
Method execution completed , Stack frame auto Popup , Free up space .

Escape analysis

Analyze the created objects inside the function ,
Is there any other stack frame called besides the current stack frame ,
Whether it has escaped the current stack frame control range ,
If yes, it cannot be allocated on the stack , Can only be allocated on the heap .

原网站

版权声明
本文为[CaptainCats]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230419070885.html