当前位置:网站首页>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 .
边栏推荐
- 这次的PMP考试(6月25日),有人欢喜有人忧,原因就在这...
- [wechat applet] understand the basic composition of the applet project
- Gradle serialization 7- configuration signature
- [rust weekly library] Tokei - a utility for statistics of code lines and other information
- Sword finger offer 15 Number of 1 in binary
- 爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
- Serialization of binary tree 297 Serialization and deserialization of binary tree 652 Find duplicate subtrees
- Cloud native enthusiast weekly: cool collection of grafana monitoring panels
- shell-位置参数变量和预定义变量
- DOM 知识点总结
猜你喜欢

Construction of module 5 of actual combat Battalion

Use of jetpack's room in combination with flow

JS绘制极坐标颜色渐变
![[rust weekly library] Tokei - a utility for statistics of code lines and other information](/img/6c/4569cc0edaa01e4605c9c256193c31.png)
[rust weekly library] Tokei - a utility for statistics of code lines and other information

This simple little function saves 213 hours for our production research team in half a year

There is no web-based development for the reward platform. Which is suitable for native development or mixed development?

Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query

Start harvesting! Nailing: adjust the maximum number of free users of "nailing team". If the number exceeds 10, it will not work normally
![多数元素II[求众数类之摩尔投票法]](/img/8f/5925f97c0f5f8c50c19a9ef6d7723c.png)
多数元素II[求众数类之摩尔投票法]

FPGA Development (2) -- IIC communication
随机推荐
Which securities company is good for opening a mobile account? In addition, is it safe to open a mobile account?
Project 1: deploy lamp ECSHOP e-commerce platform
What is flush software? Is it safe to open an account online?
Andorid source build/envsetup. SH details to know
6.28 problem solving
MySQL multi table query
设置安全组、域名备案、申请ssl证书
克隆無向圖[bfs訪問每條邊而不止節點]
333333333333333333333333333333
Sword finger offer 14- ii Cutting rope II
Which securities company should I choose to open an account online? Also, is it safe to open an account online?
Solr basic operation 6
Cloner un Graphe non recté [bfs accède à chaque bord et pas seulement aux noeuds]
手机开户后多久才能通过?另外,手机开户安全么?
如何实现搜索引擎中的拼写纠错功能——思路
Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)
[wechat applet] understand the basic composition of the applet project
MySQL functions and constraints
Golang6 reflection
Analysis of define incdir command in TCL script of Modelsim