目录

JVM为什么能跨平台访问

JVM组织构成

类装载子系统

运行时数据区域

本地方法栈

方法区

程序计数器

字节码执行引擎

重点剖析-堆

堆中对象创建流程

分配内存

划分内存的方法

解决并发问题的方法

初始化

设置对象头

 示例展示对象头信息

为什么要指针压缩 

对象创建后到底进入堆的那个那个区域


JVM为什么能跨平台访问

java被称为一处编译,到处运行,那么它实现的机制到底是什么呢?关键就在jvm屏蔽了跨平台的多种处理。看图:

java向量计算 jvm向量化_jar

JVM组织构成

java向量计算 jvm向量化_apache_02

类装载子系统

负责将class文件加载到运行时数据区域

运行时数据区域

我们代码里主要处理逻辑的地方

  • 首先堆是我们所有线程共享的区域
  • 存放我们new出来的对象,例如:new Student(); 或者数组对象。如:new int[]{};(注:有时候我们new出来的对象,例如student,并不一定会分配到堆上,可能经过逃逸分析,分配到栈上。关于逃逸分析,我们后面再讲)

这个和我们数据结构中的栈是一样的。规则:先进后出。

栈包括如下几部分

  • 局部变量
  • 操作数栈
  • 动态链接
  • 方法出口

java向量计算 jvm向量化_java向量计算_03

我们通过如下代码实例,依次来讲解各个含义。

package com.jvm;

public class JvmRunner {
   public static final int month  = 5;

   public int show(){
       int one = 10;
       int two = 11;
       int result = one+two ;
       return result;
   }
    public static void main(String[] args) {
        JvmRunner jvmRunner  = new JvmRunner();
        jvmRunner.show();
    }
}

我们通过反编译该class文件,来看看具体如何操作的。鉴于反编译占用太大空间,我直接画图描述。

  1. 栈中为main线程分配空间默认1M,为main方法以及show方法分配栈帧空间(每个方法,都有独立空间)

  2. 我们重点通过show方法进行讲解
    一:定义属性one

    二:定义属性two

    三:运算

本地方法栈

调用底层系统的方法,也是有变量以及运算的,这个也需要栈空间

方法区

方法区,jdk1.8后叫元空间。主要存放常量、静态变量以及类元信息的。是所有线程共享区域

程序计数器

因为我们是多线程环境,所以jvm执行本线程到什么位置都是由字节码执行引擎负责记录的,方便由其它线程切换到本线程执行时,顺序执行上次停顿的位置。


字节码执行引擎

负责一行一行执行脚本的,同时也负责垃圾回收。


重点剖析-堆

堆的组成结构:新生代(伊甸园区,存活区),老年代

java向量计算 jvm向量化_开发语言_04

 

  •  新生代:初次创建的小对象或未达到分代年龄阈值的对象均位于新生代
  • Eden: 伊甸园区,新创建的小对象一般先放入伊甸园区(注:大对象,与小对象是可以设置其大小的,超过这个值,直接会分配到老年代)
  • s1,s2:幸存区,假如内存不足时进行垃圾回收,回将Eden区存活的对象,放入幸存区
  • 老年代:创建的大对象或达到分代年龄阈值的对象以及触发老年代动态年龄担保机制的对象


堆中对象创建流程

  

java向量计算 jvm向量化_java向量计算_05


分配内存

当类加载完成后,就可以确定对象大小,此时就需要给对象“安置家”了,当然了java中存在并发情况,好比一群人看到了一片好地,先到先得。那么怎么保证每个人都有“地”呢?

划分内存的方法

  • 指针碰撞:此方式对于内存规整的情况,犹如队列,依次向后追加,可以理解成做核酸排队的人,有胖的有瘦的(对象有大有小),但是都顺序指向一边
  • 空闲列表:此方式对于内存不规整的情况,可以理解成停车场。有些车位有车,有些没有,如果你开了一个大货车,需要占用2个停车位,就需要找停车位空着多的地方

解决并发问题的方法

初始化

内存分配之后虚拟机就会将分配到的空间都初始化为默认值(不包括对象头),如果使用TLAB,这一工作过程也可以提前到TLAB分配时进行。这一步操作保证了对象的实例字段再java代码中可以不赋予初始值就能直接使用,程序能访问到这些字段的默认值。


设置对象头

对象头好比是一个身份证,表明了该对象是那个类的实例,怎样找到类的元数据信息,对象的哈希码,对象的分代年龄等。

java向量计算 jvm向量化_jar_06

 示例展示对象头信息

1.代码中引入依赖jar包

<dependency>
            <groupId>org.openjdk.jol</groupId>
            <artifactId>jol-core</artifactId>
            <version>0.16</version>
        </dependency>

2.创建对象以及测试类

package com.jvm;

/**
 * 描述 告白
 *
 * @author 阿牛
 * @version 1.0
 * @date 2022/05/20 15:13:48
 */
public class Confess {

    int id;
    String name;
    byte islove;
    Object info;
}

package com.jvm;

import org.openjdk.jol.info.ClassLayout;

/**
 * 描述 计算对象大小
 *
 * @author 阿牛
 * @version 1.0
 * @date 2022/05/20 14:54:31
 */
public class ObjectMarkWord {

    public static void main(String[] args) {

        ClassLayout layout = ClassLayout.parseInstance(new Object());
        System.out.println("Object对象输出...");
        System.out.println(layout.toPrintable());
        System.out.println("---------------\n");
        ClassLayout intLayout = ClassLayout.parseInstance(new int[]{});
        System.out.println("int[]对象输出...");
        System.out.println(intLayout.toPrintable());
        System.out.println("---------------\n");
        ClassLayout confessLayout = ClassLayout.parseInstance(new Confess());
        System.out.println("Confess对象输出...");
        System.out.println(confessLayout.toPrintable());
    }

}

/*

/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=53584:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/lib/tools.jar:/Users/niuyanpeng/goodfuture/selfproject/SpringBootTest/target/classes:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/boot/spring-boot-starter/2.3.2.RELEASE/spring-boot-starter-2.3.2.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/boot/spring-boot/2.3.2.RELEASE/spring-boot-2.3.2.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/spring-context/5.2.8.RELEASE/spring-context-5.2.8.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/spring-aop/5.2.8.RELEASE/spring-aop-5.2.8.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/spring-expression/5.2.8.RELEASE/spring-expression-5.2.8.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/boot/spring-boot-autoconfigure/2.3.2.RELEASE/spring-boot-autoconfigure-2.3.2.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/boot/spring-boot-starter-logging/2.3.2.RELEASE/spring-boot-starter-logging-2.3.2.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/apache/logging/log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/spring-core/5.2.8.RELEASE/spring-core-5.2.8.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/spring-jcl/5.2.8.RELEASE/spring-jcl-5.2.8.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/yaml/snakeyaml/1.26/snakeyaml-1.26.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/cloud/spring-cloud-starter-alibaba-nacos-discovery/2.2.5.RELEASE/spring-cloud-starter-alibaba-nacos-discovery-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/cloud/spring-cloud-alibaba-commons/2.2.5.RELEASE/spring-cloud-alibaba-commons-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/nacos/nacos-client/1.4.1/nacos-client-1.4.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/nacos/nacos-common/1.4.1/nacos-common-1.4.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/commons-io/commons-io/2.2/commons-io-2.2.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/apache/httpcomponents/httpasyncclient/4.1.4/httpasyncclient-4.1.4.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/apache/httpcomponents/httpcore/4.4.13/httpcore-4.4.13.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/apache/httpcomponents/httpcore-nio/4.4.13/httpcore-nio-4.4.13.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/nacos/nacos-api/1.4.1/nacos-api-1.4.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/google/guava/guava/29.0-jre/guava-29.0-jre.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/google/guava/failureaccess/1.0.1/failureaccess-1.0.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/checkerframework/checker-qual/2.11.1/checker-qual-2.11.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/google/errorprone/error_prone_annotations/2.3.4/error_prone_annotations-2.3.4.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/google/j2objc/j2objc-annotations/1.3/j2objc-annotations-1.3.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/commons-codec/commons-codec/1.14/commons-codec-1.14.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/fasterxml/jackson/core/jackson-core/2.11.1/jackson-core-2.11.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/fasterxml/jackson/core/jackson-databind/2.11.1/jackson-databind-2.11.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.1/jackson-annotations-2.11.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/prometheus/simpleclient/0.5.0/simpleclient-0.5.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/spring/spring-context-support/1.0.10/spring-context-support-1.0.10.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/cloud/spring-cloud-commons/2.2.5.RELEASE/spring-cloud-commons-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/security/spring-security-crypto/5.3.3.RELEASE/spring-security-crypto-5.3.3.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/cloud/spring-cloud-context/2.2.5.RELEASE/spring-cloud-context-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/cloud/spring-cloud-starter-netflix-ribbon/2.2.5.RELEASE/spring-cloud-starter-netflix-ribbon-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/cloud/spring-cloud-netflix-ribbon/2.2.5.RELEASE/spring-cloud-netflix-ribbon-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/cloud/spring-cloud-netflix-archaius/2.2.5.RELEASE/spring-cloud-netflix-archaius-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/cloud/spring-cloud-starter-netflix-archaius/2.2.5.RELEASE/spring-cloud-starter-netflix-archaius-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/commons-configuration/commons-configuration/1.8/commons-configuration-1.8.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/netflix/ribbon/ribbon/2.3.0/ribbon-2.3.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/netflix/ribbon/ribbon-transport/2.3.0/ribbon-transport-2.3.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/reactivex/rxnetty-contexts/0.4.9/rxnetty-contexts-0.4.9.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/reactivex/rxnetty-servo/0.4.9/rxnetty-servo-0.4.9.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/javax/inject/javax.inject/1/javax.inject-1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/reactivex/rxnetty/0.4.9/rxnetty-0.4.9.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/netflix/ribbon/ribbon-core/2.3.0/ribbon-core-2.3.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/netflix/ribbon/ribbon-httpclient/2.3.0/ribbon-httpclient-2.3.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/apache/httpcomponents/httpclient/4.5.12/httpclient-4.5.12.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/sun/jersey/jersey-client/1.19.1/jersey-client-1.19.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/sun/jersey/jersey-core/1.19.1/jersey-core-1.19.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/sun/jersey/contribs/jersey-apache-client4/1.19.1/jersey-apache-client4-1.19.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/netflix/servo/servo-core/0.12.21/servo-core-0.12.21.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/netflix/netflix-commons/netflix-commons-util/0.3.0/netflix-commons-util-0.3.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/netflix/ribbon/ribbon-loadbalancer/2.3.0/ribbon-loadbalancer-2.3.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/netflix/netflix-commons/netflix-statistics/0.1.1/netflix-statistics-0.1.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/reactivex/rxjava/1.3.8/rxjava-1.3.8.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/cloud/spring-cloud-starter-openfeign/2.2.5.RELEASE/spring-cloud-starter-openfeign-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/cloud/spring-cloud-starter/2.2.5.RELEASE/spring-cloud-starter-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/security/spring-security-rsa/1.0.9.RELEASE/spring-security-rsa-1.0.9.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/bouncycastle/bcpkix-jdk15on/1.64/bcpkix-jdk15on-1.64.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/bouncycastle/bcprov-jdk15on/1.64/bcprov-jdk15on-1.64.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/cloud/spring-cloud-openfeign-core/2.2.5.RELEASE/spring-cloud-openfeign-core-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/boot/spring-boot-starter-aop/2.3.2.RELEASE/spring-boot-starter-aop-2.3.2.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/github/openfeign/form/feign-form-spring/3.8.0/feign-form-spring-3.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/github/openfeign/form/feign-form/3.8.0/feign-form-3.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/commons-fileupload/commons-fileupload/1.4/commons-fileupload-1.4.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/spring-web/5.2.8.RELEASE/spring-web-5.2.8.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/spring-beans/5.2.8.RELEASE/spring-beans-5.2.8.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/github/openfeign/feign-core/10.10.1/feign-core-10.10.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/github/openfeign/feign-slf4j/10.10.1/feign-slf4j-10.10.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/github/openfeign/feign-hystrix/10.10.1/feign-hystrix-10.10.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/netflix/archaius/archaius-core/0.7.6/archaius-core-0.7.6.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/netflix/hystrix/hystrix-core/1.5.18/hystrix-core-1.5.18.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/cloud/spring-cloud-starter-alibaba-nacos-config/2.2.5.RELEASE/spring-cloud-starter-alibaba-nacos-config-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/cloud/spring-cloud-starter-alibaba-sentinel/2.2.5.RELEASE/spring-cloud-starter-alibaba-sentinel-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-transport-simple-http/1.8.0/sentinel-transport-simple-http-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-transport-common/1.8.0/sentinel-transport-common-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-datasource-extension/1.8.0/sentinel-datasource-extension-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/fastjson/1.2.71/fastjson-1.2.71.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-annotation-aspectj/1.8.0/sentinel-annotation-aspectj-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-core/1.8.0/sentinel-core-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/aspectj/aspectjrt/1.9.6/aspectjrt-1.9.6.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/aspectj/aspectjweaver/1.9.6/aspectjweaver-1.9.6.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/cloud/spring-cloud-circuitbreaker-sentinel/2.2.5.RELEASE/spring-cloud-circuitbreaker-sentinel-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-reactor-adapter/1.8.0/sentinel-reactor-adapter-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-spring-webflux-adapter/1.8.0/sentinel-spring-webflux-adapter-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-spring-webmvc-adapter/1.8.0/sentinel-spring-webmvc-adapter-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-parameter-flow-control/1.8.0/sentinel-parameter-flow-control-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/googlecode/concurrentlinkedhashmap/concurrentlinkedhashmap-lru/1.4.2/concurrentlinkedhashmap-lru-1.4.2.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-cluster-server-default/1.8.0/sentinel-cluster-server-default-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-cluster-common-default/1.8.0/sentinel-cluster-common-default-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/netty/netty-handler/4.1.51.Final/netty-handler-4.1.51.Final.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/netty/netty-common/4.1.51.Final/netty-common-4.1.51.Final.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/netty/netty-resolver/4.1.51.Final/netty-resolver-4.1.51.Final.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/netty/netty-buffer/4.1.51.Final/netty-buffer-4.1.51.Final.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/netty/netty-transport/4.1.51.Final/netty-transport-4.1.51.Final.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/netty/netty-codec/4.1.51.Final/netty-codec-4.1.51.Final.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/csp/sentinel-cluster-client-default/1.8.0/sentinel-cluster-client-default-1.8.0.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/alibaba/cloud/spring-cloud-alibaba-sentinel-datasource/2.2.5.RELEASE/spring-cloud-alibaba-sentinel-datasource-2.2.5.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/boot/spring-boot-starter-actuator/2.3.2.RELEASE/spring-boot-starter-actuator-2.3.2.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/boot/spring-boot-actuator-autoconfigure/2.3.2.RELEASE/spring-boot-actuator-autoconfigure-2.3.2.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/springframework/boot/spring-boot-actuator/2.3.2.RELEASE/spring-boot-actuator-2.3.2.RELEASE.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.11.1/jackson-datatype-jsr310-2.11.1.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/io/micrometer/micrometer-core/1.5.3/micrometer-core-1.5.3.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/hdrhistogram/HdrHistogram/2.1.12/HdrHistogram-2.1.12.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/latencyutils/LatencyUtils/2.0.3/LatencyUtils-2.0.3.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/projectlombok/lombok/1.18.20/lombok-1.18.20.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/apache/commons/commons-lang3/3.7/commons-lang3-3.7.jar:/Users/niuyanpeng/goodfuture/soft/apache-maven-3.8.1/repository/org/openjdk/jol/jol-core/0.16/jol-core-0.16.jar com.jvm.ObjectMarkWord
# WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
Object对象输出...
java.lang.Object object internals:
OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (object header: mark)     0x0000000000000001 (non-biasable; age: 0)
  8   4        (object header: class)    0xf80001e5
 12   4        (object alignment gap)    
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

---------------

int[]对象输出...
[I object internals:
OFF  SZ   TYPE DESCRIPTION               VALUE
  0   8        (object header: mark)     0x0000000000000001 (non-biasable; age: 0)
  8   4        (object header: class)    0xf800016d
 12   4        (array length)            0
 12   4        (alignment/padding gap)   
 16   0    int [I.<elements>             N/A
Instance size: 16 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes total

---------------

Confess对象输出...
com.jvm.Confess object internals:
OFF  SZ               TYPE DESCRIPTION               VALUE
  0   8                    (object header: mark)     0x0000000000000001 (non-biasable; age: 0)
  8   4                    (object header: class)    0xf800f2ea
 12   4                int Confess.id                0
 16   1               byte Confess.islove            0
 17   3                    (alignment/padding gap)   
 20   4   java.lang.String Confess.name              null
 24   4   java.lang.Object Confess.info              null
 28   4                    (object alignment gap)    
Instance size: 32 bytes
Space losses: 3 bytes internal + 4 bytes external = 7 bytes total


Process finished with exit code 0

 */

3.对象头分析

  • Object分析
  • java向量计算 jvm向量化_apache_07


  • int数组分析
  • java向量计算 jvm向量化_java向量计算_08


  • 自定义对象分析
  • java向量计算 jvm向量化_开发语言_09


  • 关闭指针压缩分析
-XX:-UseCompressedOops -XX:-UseCompressedClassPointers

 -XX:-UseCompressedOops;关闭所有压缩指针
-XX:-UseCompressedClassPointers;仅关闭压缩对象头里的类型指针Klass Pointer(注:对象Object会继续压缩的)


为什么要指针压缩 

  1. 如果不压缩,那么内存占用量会加大,同时较大指针在主内存和缓存中移动数据会占用较大带宽,同时为GC也带来很大压力
  2.  在jvm中,32位机器最大支持4G内存(2的32次方),可以通过对对象的压缩编码、解码方式进行优化,使得jvm只用32位地址就可以支持更大的内存配置(小于等于32G)
  3. 为了减少64位平台下内存的消耗,启用指针压缩功能
  4. 堆内存小于4G不需要启动指针压缩
  5. 堆内存大于32G时,压缩指针会失效,会强制使用64位来对对象进行寻址,就会出现1的问题,所以堆内存不要大于32G


对象创建后到底进入堆的那个那个区域

先说结果:1.Eden(伊甸园区) 2.老年代  3.栈(该情况适用于开启了逃逸分析以及标量替换,且满足该条件的对象)

针对这个问题,我们引入如下几个概念

  1. 大对象:比如字符串,数组。jvm里面有一个参数可以定义多大才算大对象。
    -XX:PretenureSizeThreshold(单位字节);超过这个参数直接进入老年代。【注:该参数只在Serial和ParNew两个收集器下有效
  2. 分代年龄:一个新new出的对象,大多数在Eden区,经过垃圾回收,会放入幸存区S,此时分代年龄+1,如果下次垃圾回收又存活,从S区域进入另一个S区域,则分代年龄+1;这个参数的含义就是告诉我们,当最大分代年龄阈值时,认为该对象生命力比较顽强会长期存活,会放入老年代。(我们应该还记得对象头中有一个4位来标识分代年龄的,这也就表明了,分代年龄最大为15)。可通过参数-XX:MaxTenuringThreshold来设置;如果我们觉得我们大多数对象一旦创建很难销毁,可以调低一点这个参数。(The largest value is 15. The default value is 15 for the parallel (throughput) collector, and 6 for the CMS collector.)
  3. 对象动态年龄判断:该机制一般再minor gc之后触发;也就是说Eden区经过垃圾回收会有一批对象存活,这批存活的对象大小之和如果超过了存放对象的那个S区的50%(-XX:TargetSurvivorRatio可以指定),那么S区中分代年龄大于等于这批将要移动过来对象集合中最大分代年龄的对象,统一移动到老年代。
  4. java向量计算 jvm向量化_开发语言_10

  5.  
  6. 老年代空间分配机制(1.6 Update 24- 已经废弃):年轻代每次minor gc之前都会计算下老年代剩余可用空间,如果这个可用空间小于年轻代里现有的所有对象大小之和(包括垃圾对象),此时会查看一个参数
    “-XX:-HandlePromotionFailure”(jdk1.8默认就有)的参数是否设置了。如果有这个参数,就会看看老年代的可用内存大小,是否大于之前每一次minor gc 后进入老年代的对象平均大小。如果上一步结果是小于或者之前说的参数没有设置,那么就会触发一次Full gc,对老年代和年轻代一起回收一次垃圾,如果回收完还是没有足够空间存放新的对象就会发生OOM。当然,如果minor gc之后剩余存活的需要挪动到老年代的对象大小还是大于老年代可用空间,那么也会触发Full gc,full gc完之后如果还是没有空间放minor gc之后的存活对象,也会放生OOM
  7. java向量计算 jvm向量化_开发语言_11


  8. 注:从目前oracle官方文档中查看,该参数HandlePromotionFailure已经不存在了
  9. java向量计算 jvm向量化_jar_12

  10.  
  11. 老年代空间分配机制(1.6+)
    JDK6 Update 24之后,HandlePromotionFailure 这个参数不会再影响到虚拟机的空间分配担保策略,JDK6 Update 24之后的规则变为只要老年代的连续空间大于新生代对象总大小或者历次晋升的平均大小就会进行Minor GC,否则将进行Full GC

 

java向量计算 jvm向量化_java向量计算_13