当前位置:网站首页>About object obj = new object()
About object obj = new object()
2022-07-22 01:05:00 【lanleihhh】
Catalog
- Object head
- Object creation process
- How objects are stored in memory ( Objects are different from array storage )
- When using an object as a lock , What happens to the layout of objects in memory ?
- How to locate an object ?
- new How to allocate the objects of ?
- Object obj = new Object() How many bytes are in memory ?
- Class Object in heap or method area ?
Object head
Object creation process
jvm encounter new Instructions , Check whether the symbolic reference of a class can be located in the constant pool , Check whether the class represented by this symbolic reference has gone through the process of class loading , Perform class loading without .
Class loading completed You can determine the memory size required by the object , The distribution method is Collision pointer and Free list Two ways
After the memory allocation is complete ,jvm Initialize all memory spaces except the object header to zero , Ensure that the fields of the object can be used directly without initialization .( To use TLAB It's the same thing )
Set the information in the object header
perform
<init>()
— Call the constructor to initializePoint the object to the reference variable (
If it is new Object() There is no such step
)
about Object obj = new Object()
- new-> Apply for a space , Assign variables to default values ( Semi initialization )
- Call constructor , Assign values to variables ( initialization )
- Establish relational relationships , Assign the object address to the reference variable obj
Use here jclasslib
Plug in view bytecode , After installation View in , choice Show Bytecode With Jclasslib
How objects are stored in memory ( Objects are different from array storage )
- Object head :
- Mark Word: Store the runtime data of the object itself
- Class Pointer: Use this pointer to determine which instance of the class the object is
- The instance data : Store the attribute information of this class and its parent class
- Alignment filling : Due to virtual machine requirements , Object size must be 8 Integer multiples of bytes , The bytes occupied by the object header have been designed as 8 Byte integer times , So if the bytes occupied by the current object are not 8 Byte integer times , You need to align and fill , Add n Bytes , Turn it into 8 Integer multiples of bytes , To meet the design specifications
stay idea View the storage information of an object in memory , rely on jol
package :java object layout
Pay attention to the dependent version and the used JDK The version corresponds to
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.8</version>
</dependency>
Test objects without variables
public class Test2 {
private static class User{
}
public static void main(String[] args) throws InterruptedException {
User user = new User();
// Resolving instances Turn to printable
System.out.println(ClassLayout.parseInstance(user).toPrintable());
}
}
result :
Test objects with variables
public class Test2 {
private static class User{
String name;
int age;
}
public static void main(String[] args) throws InterruptedException {
User user = new User();
// Resolving instances Turn to printable
System.out.println(ClassLayout.parseInstance(user).toPrintable());
}
}
result :
here String Type of age Variables of 4 byte
Variables of reference type enable compression by default , After compression is 4 byte
View the storage of the array
public class Test2 {
private static class User{
String name;
int age;
}
public static void main(String[] args) {
User[] users = new User[10];
System.out.println(ClassLayout.parseInstance(users).toPrintable());
}
}
new A length of 10 Array of , The reference of an element accounts for 4 byte , All element references account for 40 byte
Mark Word=8 byte ,Class Pointer=4 byte ,Array Length=4 byte
8+4+4+40=56 byte
When using an object as a lock , What happens to the layout of objects in memory ?
public static void main(String[] args) {
User user = new User();
// As before the lock : Print object information
System.out.println(ClassLayout.parseInstance(user).toPrintable());
// When an object is used as a lock , Print object information
synchronized(user){
System.out.println(ClassLayout.parseInstance(user).toPrintable());
}
// After releasing the lock : Print object information
System.out.println(ClassLayout.parseInstance(user).toPrintable());
}
result :
In the use of synchronized When locking an object , Of this object mark word Part will make a record
How to locate an object ?
Through... On the stack reference To manipulate objects on the heap
Reference variables on the stack , By JVM To realize the way of accessing objects
- Handle access : It is possible to partition a piece of memory as a handle pool ,reference Is the handle address of the object , The handle contains the instance data of the object 、 Address information of type data
When the object moves , Only change the instance data pointer in the handle ,ref It doesn't have to change itself
- Direct pointer access : The memory layout of objects in the heap should consider how to place the information of type data ,reference Stored in is the object address , You can access
Saves a pointer overhead , Faster
new How to allocate the objects of ?
- If the object cannot escape , And it belongs to hot data , Will be in On the stack Memory
Objects allocated on the stack can be free from garbage collection , Directly pop up - If it takes up a lot of space , Directly in Old age distribution
- Create objects simultaneously under multithreading , To ensure thread safety : stay Eden Small space is opened up for each thread in TLAB, Account for only a 1%; Each thread is created independently , Mutual interference ,TLAB After use , Reuse
CAS+ Failure to retry
To ensure the atomicity of the update operation - stay Eden Distribute
Object obj = new Object() How many bytes are in memory ?
- Mark Word :8 byte
- Class Pointer:4 byte
- Instance Data: The total number of bytes occupied by each attribute
Basic types :byte/boolean=1 byte 、short/char=2 byte 、int/float=4 byte 、double/long=8 byte
Reference type :4 byte - Padding: The bytes occupied by the current object are not 8 An integer multiple of a byte , Just align , Make up enough bytes
Bytes occupied by the object =Mark Word + Class Pointer + Instance Data + Padding
Class Object in heap or method area ?
- Class The object is in the pile
- Stored in the method area is the class Metadata ( That is, the class loader starts from class Type information extracted from the file 、 Methods information 、 Field information, etc ).
边栏推荐
- 剑指offer_知识迁移能力
- Strategy Center -- the brain for enterprises to build digital intelligence operation platform
- MySQL and Mongo database warehousing in scratch pipeline
- 镜头标定板秩事
- [AR Foundation] AR Foundation基础
- Situational data application, the internal driving force of enterprise business innovation
- Openshift security (17) - integrate compliance scanning of openshift compliance operator into rhacs
- In the digital era, the "digital intelligence" transformation of enterprise operation and management
- [ntp/chrony] how to use Chrony to configure NTP services
- if-else:if判断语句和执行语句之间相差1个时钟周期吗?并行执行吗?有优先级吗?
猜你喜欢
"Can't buy" digital transformation, each family's "LEGO" is different
关于oracle表的时间插入语法
[ar Foundation] ar Foundation Foundation Foundation Foundation
Take the interface test in 2 hours - dark horse programmer, master the preparation of interface use cases, interface information analysis, interface information analysis through Oh packet capturing, a
解读符合新时代主流的创客教育模式
[ar foundation] ar foundation foundation foundation
Typescript (II)
LeetCode刷题:二叉树层序遍历相关题目
Solve the problem that Visio closed pattern cannot be filled
Oom analysis tool mat
随机推荐
mysql dense_ Rank(), rank() function
CentOS install redis
Verilog: bitwise, logical
itk中值滤波
Hcip day 2 operation 7.17
整数二分、浮点二分以及STL中的二分函数(lower_bound(),upper_bound())
Brush questions: Niuke - quick introduction
if-else:if判断语句和执行语句之间相差1个时钟周期吗?并行执行吗?有优先级吗?
Script loads data into JSON file
国际会计准则IAS 和 IFRS
Niu Ke brushes the question - Sword finger offer
How bad can a programmer be?
通过例子学C标准库<ctype.h>
Mobile app capture tool mobile selenium - appium, mitmdump mobile capture
[AR Foundation] AR Foundation基礎
Match the middle character re of any two characters
Detailed explanation of Naomi code
It turns out that it is so important in the construction of data center!
Directly output JSON file from scratch
If else: is there a clock cycle difference between the judgment statement and the execution statement? Parallel execution? Is there a priority?