当前位置:网站首页>Stack space of JVM
Stack space of JVM
2022-06-30 00:09:00 【Java interview 365】
JVM Stack space of
Java Virtual machine memory region model

** Yellow area :** The area of memory shared by all threads , There will be garbage collection .
** Grey areas :** Thread private does not generate garbage collection .
Stack space is part of the runtime data area , So how does stack space store data ?
Java What is the virtual machine stack
In the runtime data area JAVA Stack 、 Native Method Stack 、 All three program counters are private to threads , The life cycle of the stack is the same as that of the thread , The virtual stack describes Java Memory model for method execution , Each method creates a stack frame as it executes , Used to store local variables 、 The stack of operands 、 Dynamic links 、 Method exit information , The process of each method from call to execution , Corresponding to a stack frame in the virtual machine stack to stack out of the process .

It is important to note that in Java In the virtual machine specification , Two exceptions are specified for stack space
If the stack depth of the thread request is greater than the depth allowed by the virtual machine , Will throw out StackOverflowError abnormal .
If the virtual machine stack can be expanded dynamically ( Most of the current Java Virtual machines can be dynamically expanded , It's just Java The virtual machine specification also allows for a fixed length of virtual machine stacks ). If you can't request enough memory while expanding , Will throw OutOfMemoryError abnormal
How to use stack frames
A method from call to end means a stack frame from stack to stack , So how to do it ? Refer to the following code
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;
}
/**
* Program entrance
*/
public static void main(String[] args) throws InterruptedException {
App app = new App();
int result = app.add();
System.out.println(result);
}
}
We can see App.class Compiled file ,javap -c App.class Get the results , Such as add The bytecode of the method is as follows
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: Indicates that the value will be 2 Push into the operand stack

istore_1: Indicates that the stack of the operand stack is pushed out of the stack , Put the local variable table index as 1 The location of ( That is, give variables a assignment , Of course, first allocate memory in the local variable table )

iconst_3 and istore_2 It's like , The execution result is as follows

iload_1: Index the local variable table to 1 Value ( That's the variable a Value ) Put it on the top of the operand stack ,iload_2 similar , The execution result is as follows

iadd: Add the top element of the operand stack and the next element at the top of the stack , Put it on the top of the operand stack after operation

bipush 8: Push an octet signed integer onto the operand stack

imul: Multiplication , Similar to the above addition, the top element of the operand stack and the next element at the top of the stack are pushed out of the stack , Then put the result on the operand stack

istore_3: Take the top element of the operand stack out of the stack , Put the local variable table index as 3 The location of ( Is a variable c assignment )

iload_3: Index the local variable table to 3 Value ( That's the variable c Value ) Put it on the top of the operand stack

ireturn: Returns the top element of the operand stack, that is 40
Come here add Method ends and returns , Method exit records the location that should be returned , That is to say add Method stack frame out of stack , Then enter main Method stack frame execution logic , So the overall logic is as follows .
Be careful
In the active thread , Only the stack frame at the top of the stack is valid , Call it the current stack frame , The method associated with this stack frame is called the current method .
All bytecode instructions of the execution engine operate only on the current stack frame .
边栏推荐
- Solr基础操作13
- New CorelDRAW technical suite2022 latest detailed function introduction
- EB-5 immigration in the United States reappears to be positive, and the reauthorization policy of the regional center is suspended
- 西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
- Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
- 6.28 problem solving
- Golang泛型的巧妙应用,防止变量空指针错误,防止结构体字段空指针错误
- Solr基础操作7
- HPE launched ARM CPU general server product
- Golang6 reflection
猜你喜欢

Koa2 learning and using

视频ToneMapping(HDR转SDR)中的颜色空间转换问题(BT2020转BT709,YCbCr、YUV和RGB)

这个简单的小功能,半年为我们产研团队省下213个小时

Summarize Flink runtime architecture in simple terms

爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记

ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)

【微信小程序】认识小程序项目的基本组成结构

项目一:部署 LAMP ecshop电商平台

After working in the software development industry for six years, I changed my ideas in those years

Sword finger offer 14- ii Cutting rope II
随机推荐
FPGA Development (1) -- serial port communication
我想知道今天还可以开户么?另外想问,现在在线开户安全么?
Label Troubleshooting: unable to open the marked image
6.28 problem solving
Bee常用配置
Do mysqlcdc data not support windowing functions like row_ Number, lead
QT learning 05 introduction to QT creator project
Start harvesting! Nailing: adjust the maximum number of free users of "nailing team". If the number exceeds 10, it will not work normally
Clone undirected graph [bfs accesses each edge but not only nodes]
How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
云原生爱好者周刊:炫酷的 Grafana 监控面板集合
This simple little function saves 213 hours for our production research team in half a year
数莓派 4怎么样?可能的玩法有哪些?
Simple understanding of B tree and b+ tree
Sword finger offer 15 Number of 1 in binary
After working in the software development industry for six years, I changed my ideas in those years
Which securities company is good for opening a mobile account? In addition, is it safe to open a mobile account?
Cloner un Graphe non recté [bfs accède à chaque bord et pas seulement aux noeuds]
QT learning 02 GUI program example analysis
Leetcode (76) -- Minimum Covering substring