Java JVM運(yùn)行時數(shù)據(jù)區(qū)(Run-Time Data Areas)
1、官網(wǎng)概括
引用官網(wǎng)說法:
The Java Virtual Machine defines various run-time data areas that are used during execution of a program. Some of these data areas are created on Java Virtual Machine start-up and are destroyed only when the Java Virtual Machine exits. Other data areas are per thread. Per-thread data areas are created when a thread is created and destroyed when the thread exits.
運(yùn)行時數(shù)據(jù)區(qū),是java虛擬機(jī)定義的在程序執(zhí)行期間使用的各種運(yùn)行時的數(shù)據(jù)區(qū)。這些運(yùn)行時數(shù)據(jù)區(qū)分為兩種,一種是在java虛擬機(jī)啟動時創(chuàng)建,僅在java虛擬機(jī)退出時才被銷毀,這種可以理解為線程共享的。另外一種是數(shù)據(jù)區(qū)是針對每個線程的,是在創(chuàng)建線程時創(chuàng)建的,并在線程退出時銷毀這個數(shù)據(jù)區(qū),這種可以理解為線程私有的。
2、圖例和思維導(dǎo)圖
JVM運(yùn)行時數(shù)據(jù)區(qū)圖例:
思維導(dǎo)圖:Java虛擬機(jī)運(yùn)行時數(shù)據(jù)區(qū),虛擬機(jī)棧、本地方法棧、程序計(jì)數(shù)器是線程私有的,方法區(qū)、堆是線程共享的
3、方法區(qū)(Method Area)
what is method area? 下面摘錄官網(wǎng)對方法區(qū)的描述
(1)、方法區(qū)是線程共享的內(nèi)存區(qū)域,在虛擬機(jī)啟動時創(chuàng)建
The Java Virtual Machine has a method area that is shared among all Java Virtual
Machine threads.
The method area is created on virtual machine start-up.
(2)、雖然方法區(qū)是堆的一個邏輯部分,但是其別名為非堆(Non-heap),目的是和堆區(qū)分開
The method area is analogous to the storage area for compiled code of a conventional language or analogous to the “text” segment in an operating system process
(3)、方法區(qū)存儲運(yùn)行時常量池、字段和方法數(shù)據(jù),以及方法和構(gòu)造函數(shù)的代碼,包括在類和實(shí)例初始化和接口初始化中使用的特殊方法
It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods (§2.9) used in class and instance initialization and interface initialization.
ps,注意點(diǎn):如果方法區(qū)中的內(nèi)存不能用于滿足分配請求,Java 虛擬機(jī)將拋出一個OutOfMemoryError.
歸納:JVM方法區(qū)中存儲了每個類的信息(包括類的名稱、方法信息、字段信息),靜態(tài)變量,常量已經(jīng)編譯器編譯后的代碼等。方法區(qū)是線程共享的,習(xí)慣上方法區(qū)也被稱為“永久代”。如果方法區(qū)中的內(nèi)存不能用于滿足分配請求,Java 虛擬機(jī)將拋出內(nèi)存不足異常
4、堆(Heap)
(1)、Java堆是Java虛擬機(jī)所管理內(nèi)存中最大的一塊,堆是運(yùn)行時數(shù)據(jù)區(qū),從中分配所有類實(shí)例和數(shù)組的內(nèi)存
The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.
(2)、堆是在虛擬機(jī)啟動時創(chuàng)建的,是所有 Java 虛擬機(jī)線程之間共享的
The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads.
The heap is created on virtual machine start-up
注意:如果計(jì)算需要的堆多于自動存儲管理系統(tǒng)所能提供的堆,Java 虛擬機(jī)將拋出一個 OutOfMemoryError.
歸納:Java 中的堆是用來 存儲對象本身的以 及數(shù)組,堆是被所有 線程共享的。Java 堆從 GC 的角度還可以細(xì)分為: 新生代( Eden 區(qū) 、 From Survivor 區(qū) 和 To Survivor 區(qū) )和老年代。。如果計(jì)算需要的堆多于自動存儲管理系統(tǒng)所能提供的堆,Java 虛擬機(jī)將拋出一個 OutOfMemoryError.
5、Java虛擬機(jī)棧
Java虛擬機(jī)棧:Java Virtual Machine Stacks
(1)、每一個java虛擬機(jī)線程都有一個java虛擬機(jī)棧,在線程創(chuàng)建時候就創(chuàng)建虛擬機(jī)棧
Each Java Virtual Machine thread has a private Java Virtual Machine stack,
created at the same time as the thread
(2)、java虛擬機(jī)線程中的每一個方法對應(yīng)一個棧幀;調(diào)用一個方法,就向虛擬機(jī)棧中壓入一個棧幀;一個方法調(diào)用完成,就將改棧幀從棧中彈出
A Java Virtual Machine stack stores frames (§2.6)A new frame is created each time a method is invoked. A frame is destroyed when
its method invocation completes.
6、 棧幀(Stack Frame)
棧幀:每個棧幀對應(yīng)一個被調(diào)用的方法,可以理解為一個方法的運(yùn)行空間
每個棧幀中包括局部變量表(Local Variables)、操作數(shù)棧(Operand Stack)、動態(tài)鏈接(Dynamic Linking)、方法返回地址(Return Address)
- 局部變量表:方法中定義的局部變量以及方法的參數(shù)存放在這張表
- 操作數(shù)棧:以壓棧和出棧的方式存儲操作數(shù)的
- 動態(tài)鏈接:每個幀都包含對當(dāng)前方法類型的運(yùn)行時常量池的引用,以支持方法代碼的動態(tài)鏈接,class方法的文件代碼指的是要調(diào)用的方法和要通過符號引用訪問的變量。動態(tài)鏈接將這些符號方法引用轉(zhuǎn)換為具體方法引用,根據(jù)需要加載類以解析尚未定義的符號,并將變量訪問轉(zhuǎn)換為與這些變量的運(yùn)行時位置相關(guān)聯(lián)的存儲結(jié)構(gòu)中的適當(dāng)偏移量(重點(diǎn)理解一下符號方法引用轉(zhuǎn)換為具體方法引用,class文件編譯為字節(jié)碼之后,會有一個符號引用規(guī)范,動態(tài)鏈接就是將符號方法引用轉(zhuǎn)換為具體方法引用)
- 方法返回地址:當(dāng)一個方法開始執(zhí)行后,只有兩種方式可以退出,一種是遇到方法返回的字節(jié)碼指令;一種是遇見異常,并且該異常不在方法內(nèi)處理,則方法調(diào)用會 突然完成。執(zhí)行athrow指令 ( § athrow ) 也會導(dǎo)致顯式拋出異常,如果當(dāng)前方法未捕獲異常,則會導(dǎo)致方法調(diào)用突然完成。突然完成的方法調(diào)用永遠(yuǎn)不會向其調(diào)用者返回值。
注意:
如果線程中的計(jì)算需要比允許的更大的 Java 虛擬機(jī)堆棧,則 Java 虛擬機(jī)將拋出一個StackOverflowError.如果 Java 虛擬機(jī)堆棧可以動態(tài)擴(kuò)展,并且嘗試擴(kuò)展但沒有足夠的內(nèi)存來實(shí)現(xiàn)擴(kuò)展,或者如果沒有足夠的內(nèi)存可以為新線程創(chuàng)建初始 Java 虛擬機(jī)堆棧,則 Java 虛擬機(jī)機(jī)器拋出一個OutOfMemoryError.
7、程序計(jì)數(shù)器(The pc Register)
每個java虛擬機(jī)線程都有自己的程序計(jì)數(shù)器。在任何時候,每個 Java 虛擬機(jī)線程都在執(zhí)行單個方法的代碼,如果該方法不是 native,則該pc寄存器包含當(dāng)前正在執(zhí)行的 Java 虛擬機(jī)指令的地址。如果線程當(dāng)前正在執(zhí)行的方法是native,則 Java 虛擬機(jī)pc 寄存器的值是未定義的
The Java Virtual Machine can support many threads of execution at once (JLS §17). Each Java Virtual Machine thread has its own pc (program counter) register. At any point, each Java Virtual Machine thread is executing the code of a single method, namely the current method (§2.6) for that thread. If that method is not native, the pc register contains the address of the Java Virtual Machine instruction currently being executed. If the method currently being executed by the thread is native, the value of the Java Virtual Machine's pc register is undefined. The Java Virtual Machine's pc register is wide enough to hold a returnAddress or a native pointer on the specific platform.
8、本地方法棧(Native Method Stacks)
對于一般的方法,都是在java虛擬機(jī)棧指向,如果當(dāng)前線程執(zhí)行的方法是Native類型的,這些方法就會在本地方法棧中執(zhí)行,學(xué)習(xí)本地方法??梢院吞摂M機(jī)棧對比。
native方法實(shí)例,可以點(diǎn)到String源碼里看,如圖,這個方法就是一個native方法:
異常情況:
1.棧深度大于已有深度:StackOverflowError
2.可擴(kuò)展深度大于能夠申請的內(nèi)存:OutOfMemoryError
以上就是Java JVM運(yùn)行時數(shù)據(jù)區(qū)(Run-Time Data Areas)的詳細(xì)內(nèi)容,更多關(guān)于JVM 運(yùn)行時數(shù)據(jù)區(qū)的資料請關(guān)注腳本之家其它相關(guān)文章!
- JAVA JVM運(yùn)行時數(shù)據(jù)區(qū)詳解
- JVM內(nèi)存模型/內(nèi)存空間:運(yùn)行時數(shù)據(jù)區(qū)
- 面試時必問的JVM運(yùn)行時數(shù)據(jù)區(qū)詳解
- JVM運(yùn)行時數(shù)據(jù)區(qū)劃分原理詳解
- Java內(nèi)存模型與JVM運(yùn)行時數(shù)據(jù)區(qū)的區(qū)別詳解
- JVM運(yùn)行時數(shù)據(jù)區(qū)原理解析
- Java內(nèi)存模型JMM與volatile
- Java內(nèi)存模型(JMM)及happens-before原理
- JVM?運(yùn)行時數(shù)據(jù)區(qū)與JMM?內(nèi)存模型
相關(guān)文章
SpringBoot?項(xiàng)目的創(chuàng)建與啟動步驟詳解
這篇文章主要介紹了SpringBoot?項(xiàng)目的創(chuàng)建與啟動,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03Java中new關(guān)鍵字和newInstance方法的區(qū)別分享
在初始化一個類,生成一個實(shí)例的時候,newInstance()方法和new關(guān)鍵字除了一個是方法一個是關(guān)鍵字外,最主要的區(qū)別是創(chuàng)建對象的方式不同2013-07-07Java高性能新一代構(gòu)建工具M(jìn)aven-mvnd(實(shí)踐可行版)
這篇文章主要介紹了Java高性能新一代構(gòu)建工具M(jìn)aven-mvnd(實(shí)踐可行版),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06