您的位置:首页 > 新闻 > 会展 > 百度教育智能小程序_广西壮族自治区省长_百度关键词排名代发_大数据推广公司

百度教育智能小程序_广西壮族自治区省长_百度关键词排名代发_大数据推广公司

2025/3/11 15:19:00 来源:https://blog.csdn.net/nanxiaotao/article/details/145978680  浏览:    关键词:百度教育智能小程序_广西壮族自治区省长_百度关键词排名代发_大数据推广公司
百度教育智能小程序_广西壮族自治区省长_百度关键词排名代发_大数据推广公司

前言

对于大部分的语言,新对象的实例化基本分为三个阶段:分配、系统初始化、用户初始化。

分配

在分配阶段主要需要确定进程空间中是堆区、还是栈区来获取实例数据。

系统初始化

系统初始化指的是特定编程语言的初始化,在C编程语言中,new分配的对象不需要进行初始化,而在Java编程语言中,所有的对象都需要进行系统初始化,具体包含将JVM分配到的内存空间初始化为零值,设置对象头(包含对象的哈希码、GC分代年龄、对象的类元数据信息等)等。

用户初始化

主要指运行与当前对象有关联的任何实例初始化程序和构造函数。

示例

源码

public class Case{public Object newInstance(){return new Object();}
}

编译

public class Case {<ClassVersion=55><SourceFile=Case.java>public Case() { // <init> //()VL0 {aload 0 // reference to selfinvokespecial java/lang/Object.<init>()Vreturn}}public java.lang.Object newInstance() { //()Ljava/lang/Object;L0 {new java/lang/Objectdupinvokespecial java/lang/Object.<init>()Vareturn}}
}

实验

源码

package org.sample;import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(value = 3)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class UserInit {@Benchmarkpublic Object init() {return new Init(42);}@Benchmarkpublic Object initLeaky() {return new InitLeaky(42);}static class Init {private int x;public Init(int x) {this.x = x;}}static class InitLeaky {private int x;public InitLeaky(int x) {doSomething();this.x = x;}@CompilerControl(CompilerControl.Mode.DONT_INLINE)void doSomething() {// intentionally left blank}}
}

编译

public class org/sample/UserInit {<ClassVersion=50><SourceFile=UserInit.java>public UserInit() { // <init> //()V<localVar:index=0 , name=this , desc=Lorg/sample/UserInit;, sig=null, start=L0, end=L1>L0 {aload 0 // reference to selfinvokespecial java/lang/Object.<init>()Vreturn}L1 {}}public java.lang.Object init() { //()Ljava/lang/Object;<localVar:index=0 , name=this , desc=Lorg/sample/UserInit;, sig=null, start=L0, end=L1><visAnno:desc = Lorg/openjdk/jmh/annotations/Benchmark; , values = []>L0 {new org/sample/UserInit$Initdupbipush 42invokespecial org/sample/UserInit$Init.<init>(I)Vareturn}L1 {}}public java.lang.Object initLeaky() { //()Ljava/lang/Object;<localVar:index=0 , name=this , desc=Lorg/sample/UserInit;, sig=null, start=L0, end=L1><visAnno:desc = Lorg/openjdk/jmh/annotations/Benchmark; , values = []>L0 {new org/sample/UserInit$InitLeakydupbipush 42invokespecial org/sample/UserInit$InitLeaky.<init>(I)Vareturn}L1 {}}
// The following inner classes couldn't be decompiled: org/sample/UserInit$InitLeaky org/sample/UserInit$Init 
}

Init()部分汇编

                                                  ; ------- allocation ----------
0x00007efdc466d4cc: mov    0x60(%r15),%rax          ; TLAB allocation below
0x00007efdc466d4d0: mov    %rax,%r10
0x00007efdc466d4d3: add    $0x10,%r10
0x00007efdc466d4d7: cmp    0x70(%r15),%r10
0x00007efdc466d4db: jae    0x00007efdc466d50a
0x00007efdc466d4dd: mov    %r10,0x60(%r15)
0x00007efdc466d4e1: prefetchnta 0xc0(%r10); ------- /allocation ---------; ------- system init ---------
0x00007efdc466d4e9: movq   $0x1,(%rax)              ; put mark word header
0x00007efdc466d4f0: movl   $0xf8021bc4,0x8(%rax)    ; put class word header; ...... system/user init .....
0x00007efdc466d4f7: movl   $0x2a,0xc(%rax)          ; x = 42.; -------- /user init ---------

在Init()方法中,没有严格的区分系统初始化和用户初始化的顺序。

InitLeaky()部分汇编

                                                  ; ------- allocation ----------
0x00007fc69571bf4c: mov    0x60(%r15),%rax
0x00007fc69571bf50: mov    %rax,%r10
0x00007fc69571bf53: add    $0x10,%r10
0x00007fc69571bf57: cmp    0x70(%r15),%r10
0x00007fc69571bf5b: jae    0x00007fc69571bf9e
0x00007fc69571bf5d: mov    %r10,0x60(%r15)
0x00007fc69571bf61: prefetchnta 0xc0(%r10); ------- /allocation ---------; ------- system init ---------
0x00007fc69571bf69: movq   $0x1,(%rax)              ; put mark word header
0x00007fc69571bf70: movl   $0xf8021bc4,0x8(%rax)    ; put class word header
0x00007fc69571bf77: mov    %r12d,0xc(%rax)          ; x = 0 (%r12 happens to hold 0); ------- /system init --------; -------- user init ----------
0x00007fc69571bf7b: mov    %rax,%rbp
0x00007fc69571bf7e: mov    %rbp,%rsi
0x00007fc69571bf81: xchg   %ax,%ax
0x00007fc69571bf83: callq  0x00007fc68e269be0       ; call doSomething()
0x00007fc69571bf88: movl   $0x2a,0xc(%rbp)          ; x = 42; ------ /user init ------

在InitLeaky()方法中,由于优化器无法确定是否需要x的值,它们必须假设最坏的情况,首先执行系统初始化,然后再完成用户初始化。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com