JVM家庭成員盤點及各自職能介紹

語言: CN / TW / HK

#

JVM為什麼能跨平臺訪問

java被稱為一處編譯,到處執行,那麼它實現的機制到底是什麼呢?關鍵就在jvm遮蔽了跨平臺的多種處理。看圖:

#

JVM組織構成

類裝載子系統

負責將class檔案載入到執行時資料區域。

執行時資料區域

我們程式碼裡主要處理邏輯的地方

  • 首先堆是我們所有執行緒共享的區域。

  • 存放我們new出來的物件,例如:new Student(); 或者陣列物件。如:new int[]{};(注:有時候我們new出來的物件,例如student,並不一定會分配到堆上,可能經過逃逸分析,分配到棧上。關於逃逸分析,我們後面再講)。

這個和我們資料結構中的棧是一樣的。規則:先進後出。

棧包括如下幾部分:

  • 區域性變數

  • 運算元棧

  • 動態連結

  • 方法出口

我們通過如下程式碼例項,依次來講解各個含義。

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方法進行講解

1)定義屬性one

2)定義屬性two

3)運算

本地方法棧

呼叫底層系統的方法,也是有變數以及運算的,這個也需要棧空間。

方法區(元空間)

方法區,jdk1.8後叫元空間。主要存放常量、靜態變數以及類元資訊的。是所有執行緒共享區域。

程式計數器

因為我們是多執行緒環境,所以jvm執行本執行緒到什麼位置都是由位元組碼執行引擎負責記錄的,方便由其它執行緒切換到本執行緒執行時,順序執行上次停頓的位置。

位元組碼執行引擎

負責一行一行執行指令碼的,同時也負責垃圾回收。

#

重點剖析堆

堆的組成結構:新生代(伊甸園區,存活區),老年代。

  • 新生代:初次建立的小物件或未達到分代年齡閾值的物件均位於新生代

  • Eden: 伊甸園區,新建立的小物件一般先放入伊甸園區(注:大物件,與小物件是可以設定其大小的,超過這個值,直接會分配到老年代)

  • s1,s2:倖存區,假如記憶體不足時進行垃圾回收,會將Eden區存活的物件,放入倖存區

  • 老年代:建立的大物件或達到分代年齡閾值的物件以及觸發老年代動態年齡擔保機制的物件

堆中物件建立流程

分配記憶體

當類載入完成後,就可以確定物件大小,此時就需要給物件“安置家”了,當然了java中存在併發情況,好比一群人看到了一片好地,先到先得。那麼怎麼保證每個人都有“地”呢?

劃分記憶體的方法

  • 指標碰撞:此方式對於記憶體規整的情況,猶如佇列,依次向後追加,可以理解成做核酸排隊的人,有胖的有瘦的(物件有大有小),但是都順序指向一邊。

  • 空閒列表:此方式對於記憶體不規整的情況,可以理解成停車場。有些車位有車,有些沒有,如果你開了一個大貨車,需要佔用2個停車位,就需要找停車位空著多的地方。

解決併發問題的方法

  • CAS:併發程式設計中最常用的方式之一,如果失敗就重試。

  • 本地執行緒分配緩衝:(Thread Local Allocation Buffer,TLAB)前面的方式是知道物件大小,此種方式是根據執行的執行緒,在堆中預先分配出一塊空間供給該執行緒使用。通過-XX:+/-UseTLAB引數來控制是否啟用TLAB,同時可搭配引數-XX:TLABSize指定TLAB大小【jdk7已經預設開啟;jdk增加了是否啟用的引數,大小一般為Eden的1%】,具體引數可檢視地址:

    https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html

初始化

記憶體分配之後虛擬機器就會將分配到的空間都初始化為預設值(不包括物件頭),如果使用TLAB,這一工作過程也可以提前到TLAB分配時進行。這一步操作保證了物件的例項欄位再java程式碼中可以不賦予初始值就能直接使用,程式能訪問到這些欄位的預設值。

設定物件頭

物件頭好比是一個身份證,表明了該物件是那個類的例項,怎樣找到類的元資料資訊,物件的雜湊碼,物件的分代年齡等。

示例展示物件頭資訊

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/06/05 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/06/05 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分析

  • int陣列分析

  • 自定義物件分析

  • 關閉指標壓縮分析

-XX:-UseCompressedOops -XX:-UseCompressedClassPointers
-XX:-UseCompressedOops;關閉所有壓縮指標
-XX:-UseCompressedClassPointers;僅關閉壓縮物件頭裡的型別指標Klass Pointer(注:物件Object會繼續壓縮的)

為什麼要指標壓縮

  • 如果不壓縮,那麼記憶體佔用量會加大,同時較大指標在主記憶體和快取中移動資料會佔用較大頻寬,同時為GC也帶來很大壓力。

  • 在jvm中,32位機器最大支援4G記憶體(2的32次方),可以通過對物件的壓縮編碼、解碼方式進行優化,使得jvm只用32位地址就可以支援更大的記憶體配置(小於等於32G)。

  • 為了減少64位平臺下記憶體的消耗,啟用指標壓縮功能。

  • 堆記憶體小於4G不需要啟動指標壓縮。

  • 堆記憶體大於32G時,壓縮指標會失效,會強制使用64位來對物件進行定址,就會出現1的問題,所以堆記憶體不要大於32G。

物件建立後到底進入哪個區域

先說結果:

1、Eden(伊甸園區)

2、老年代

3、棧(該情況適用於開啟了逃逸分析以及標量替換,且滿足該條件的物件)

針對這個問題,我們引入如下幾個概念:

  • 大物件:比如字串,陣列。jvm裡面有一個引數可以定義多大才算大物件。

    -XX:PretenureSizeThreshold(單位位元組);超過這個引數直接進入老年代。【注:該引數只在Serial和ParNew兩個收集器下有效】。

  • 分代年齡:一個新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.)。

  • 物件動態年齡判斷:該機制一般再minor gc之後觸發;也就是說Eden區經過垃圾回收會有一批物件存活,這批存活的物件大小之和如果超過了存放物件的那個S區的50%(-XX:TargetSurvivorRatio可以指定),那麼S區中分代年齡大於等於這批將要移動過來物件集合中最大分代年齡的物件,統一移動到老年代。

  • 老年代空間分配機制(1.6 Update 24- 已經廢棄): 年輕代每次minor gc之前都會計算下老年代剩餘可用空間,如果這個可用空間小於年輕代裡現有的所有物件大小之和(包括垃圾物件),此時會檢視一個引數

    “-XX:-HandlePromotionFailure”(jdk1.8預設就有)的引數是否設定了。如果有這個引數,就會看看老年代的可用記憶體大小,是否大於之前每一次minor gc 後進入老年代的物件平均大小。如果上一步結果是小於或者之前說的引數沒有設定,那麼就會觸發一次Full gc,對老年代和年輕代一起回收一次垃圾,如果回收完還是沒有足夠空間存放新的物件就會發生OOM。當然,如果minor gc之後剩餘存活的需要挪動到老年代的物件大小還是大於老年代可用空間,那麼也會觸發Full gc,full gc完之後如果還是沒有空間放minor gc之後的存活物件,也會放生OOM。

  • 注:從目前oracle官方文件中檢視,該參HandlePromotionFailure已經不存在了。

  • 老年代空間分配機制(1.6+)

    JDK6 Update 24之後,HandlePromotionFailure 這個引數不會再影響到虛擬機器的空間分配擔保策略,JDK6 Update 24之後的規則變為只要老年代的連續空間大於新生代物件總大小或者歷次晉升的平均大小就會進行Minor GC,否則將進行Full GC

掃描下方二維碼新增 「好未來技術」 微信官方賬號

進入好未來技術官方交流群與作者實時互動~

(若掃碼無效,可通過微訊號 TAL-111111 直接新增)

- 也許你還想看 -

給非前端夥伴的前端知識學習引導

關於冪等設計

Python併發程式設計入門

基於雙模檢測的通話錄音質檢解決方案

我知道你“在看”喲!~