当前位置:网站首页>JVM之栈空间
JVM之栈空间
2022-06-29 23:53:00 【Java面试365】
JVM之栈空间
Java虚拟机内存区域模型

**黄色区域:**所有线程共享的内存区域,会存在垃圾回收。
**灰色区域:**线程私有不会产生垃圾回收。
栈空间是运行时数据区中的一部分,那么栈空间如何存储数据呢?
Java虚拟机栈是什么
在运行时数据区中JAVA栈、本地方法栈、程序计数器三个都是线程所私有,栈的生命周期和线程的生命周期是一样的同生共死,虚拟机栈描述的是Java方法执行的内存模型,每个方法在执行的同时都会创建一个栈帧,用于存储局部变量表、操作数栈、动态链接、方法出口等信息,每个方法从调用直至执行完成的过程,就对应着一个栈帧在虚拟机中入栈到出栈的过程。

需要注意的是在Java虚拟机规范中,对栈空间规定了两种异常情况
如果线程请求的栈深度大于虚拟机所允许的深度,将抛出StackOverflowError异常。
如果虚拟机栈可以动态扩展(当前大部分的Java虚拟机都可动态扩展,只不过Java虚拟机规范也允许固定长度的虚拟机栈)。如果扩展时无法申请到足够的内存,就会抛出OutOfMemoryError异常
栈帧如何使用
一个方法从调用到结束就意味着一个栈帧从栈中入栈到出栈的过程,那么具体怎么做呢?参考如下代码
public class App {
private String name;
private Object object = new Object();
public int add() {
int a = 2;
int b = 3;
int c = (a + b) * 8;
return c;
}
/**
* 程序入口
*/
public static void main(String[] args) throws InterruptedException {
App app = new App();
int result = app.add();
System.out.println(result);
}
}
我们可以查看App.class编译后的文件,javap -c App.class得到结果,如add方法的字节码如下
public int add();
Code:
0: iconst_2
1: istore_1
2: iconst_3
3: istore_2
4: iload_1
5: iload_2
6: iadd
7: bipush 8
9: imul
10: istore_3
11: iload_3
12: ireturn
iconst_2:表示将数值2压入操作数栈中

istore_1:表示将操作数栈的栈顶出栈,放入局部变量表索引为1的位置(也就是给变量a赋值,当然先要在局部变量表中分配内存)

iconst_3和istore_2就类似了,执行完结果如下所示

iload_1:把局部变量表索引为1的值(也就是变量a的值)放入操作数栈栈顶,iload_2类似,执行完结果如下所示

iadd:将操作数栈栈顶元素和栈顶下一个元素进行加法运算,运算后放入操作数栈栈顶

bipush 8:将一个八位带符号整数压入操作数栈中

imul:乘法计算,类似上面的加法将操作数栈的栈顶元素和栈顶下一个元素出栈,然后将结果入栈到操作数栈中

istore_3:将操作数栈的栈顶元素出栈,放入局部变量表索引为3的位置(就是为变量c赋值)

iload_3:将局部变量表索引为3的值(也就是变量c的值)放入操作数栈栈顶

ireturn:返回操作数栈栈顶元素也就是40
到这里add方法就结束了返回,方法出口记录应该返回的位置,也就是add方法栈帧出栈,然后进入main方法栈帧执行逻辑,所以整体逻辑如下所示。
注意
在活动的线程中,只有位于栈顶的栈帧才是有效的,称之为当前栈帧,与这个栈帧相关联的方法称为当前方法。
执行引擎的所有字节码指令都只针对当前栈帧进行操作。
边栏推荐
- Koa2 learning and using
- Leetcode (633) -- sum of squares
- 架构实战营模块5作业
- 打造一个 API 快速开发平台,牛逼!
- [LeetCode] 只出现一次的数字【136】
- AI empowers new retail, the way to win "wisdom" lies in ecological thinking | selected excerpts from digital intelligence night talk live broadcast
- How to write controller layer code gracefully?
- Web APIs 环境对象 丨黑马程序员
- Xutils3 transfer set
- The concept and significance of mean, variance, standard deviation and covariance
猜你喜欢

About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb

Sword finger offer 15 Number of 1 in binary

Sword finger offer 13 Range of motion of robot
500 error occurred after importing skins folder into solo blog skin

Serialization of binary tree 297 Serialization and deserialization of binary tree 652 Find duplicate subtrees

Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column

Halcon实用:焊点检出设计思路

FPGA Development (2) -- IIC communication

Zhongang Mining: Fluorite helps the construction and development of lithium battery in fluorine industry

Common knowledge of ECS security settings
随机推荐
FPGA开发(1)——串口通信
Siemens low code version 9.14: meet different needs
High performance and high availability computing architecture of "microblog comments" in microblog system
MySQL:SQL概述及数据库系统介绍 | 黑马程序员
QT learning 01 GUI program principle analysis
333333333333333333333333333333
Applet plug-in access, development and precautions
Implementation of aut, a self-developed transport layer protocol for sound network -- dev for dev column
数莓派 4怎么样?可能的玩法有哪些?
[leetcode] a number that appears only once [136]
Solr basic operation
Solr basic operation 1
The role of VMware virtual machine
QPainter的使用入门:绘制象棋界面
RRDTOOL draws MRTG log data
剑指 Offer 14- I. 剪绳子
25 interview questions about Apache
Divisor
After crossing, she said that the multiverse really exists
Yunhe enmo, gaiguoqiang, identify it and grasp it before the domestic database boils