当前位置:网站首页>Introduction to JVM stack and heap
Introduction to JVM stack and heap
2022-07-01 04:38:00 【xmh-sxh-1314】
One 、 Basic understanding
java There are two types of data : Basic types and reference types . The basic type of variable holds the original value , Reference type variables hold reference values . A reference value represents a reference to an object , Not the object itself , The object itself is placed at the address represented by the reference value .
Two 、 Heap and stack
3、 ... and 、 Detailed instructions
A stack is a unit of runtime , Heap is the unit of storage
Stack to solve the program running problems , That is, how the program is executed , Or how to deal with the data ; Heap solves the problem of data storage , That is how to put the data 、 Where to put it
stay java There will be a thread stack corresponding to one thread in , Because different threads have different execution logic , So we need a separate thread stack . The heap is shared by all threads . The stack is the running unit . So it stores data related to the current thread . Include local variables 、 Program running state 、 Method return value, etc ; The heap is only responsible for storing object information .
Why separate heap from stack , Stack can also store data ?
1、 From the perspective of software design , The stack represents the processing logic , The heap represents the data , So separate , Make the processing logic clearer . The idea of divide and rule , This isolation 、 The idea of modularity is embodied in many aspects of software .
2、 Separation of heap and stack , So that the contents of the heap can be shared by multiple stacks ( That is, multiple threads access the same object ). The benefits of this sharing are many , This sharing provides an effective way of data interaction ( Shared memory ), On the other hand , Shared constants and caches in the heap can be accessed by all stacks , Save memory .
3、 Stack because it is necessary to run , For example, save the context of system operation , The division of address segment is needed , Because the stack can only grow up , So limit the stack's ability to store content , And the heap can grow dynamically on demand , So stack and heap splitting , Make dynamic heap growth possible , The corresponding stack only needs to remember one address in the heap .
4、 Object oriented is the perfect combination of heap and stack . Actually , There is no difference in execution between object-oriented program and previous structured program . however , The introduction of object-oriented , It changes the way we think about problems , And thinking in a more natural way . When we take objects apart , You'll find that , The properties of an object are actually data , Store in a pile ; And the behavior of the object ( Method ), It's running logic , In the stack . When we write objects , In fact, I wrote the data structure , Also write the logic of data processing . Have to admit , Object oriented design , It's really beautiful
stay java in ,main Function is the starting point of the stack , It's also the starting point of the program .
What's in the heap , What's in the stack ?
There are objects in the heap , The stack stores basic data types and references to objects in the heap , The size of an object cannot be estimated , Or it can change dynamically , But in the stack , An object only corresponds to one 4byte quote
object , In a sense , It's made up of basic types . You can think of an object as a tree , If the property of an object is still an object , It's still a tree ( That is, non leaf nodes ), The basic type is the leaf node of the tree . When program parameters are passed , The passed value itself cannot be modified , however , If the value is a non leaf node ( That is, an object reference ), You can modify all the contents under this node .
Heap and stack , Stack is the most fundamental thing for program running . Programs can run without heaps , But it can't be without stacks . Heap is a data storage service for stack , To put it bluntly, a heap is a piece of shared memory . however , It's because of the separation of heap and stack , So that Java It's possible to recycle our garbage .
Java in , The stack size passes through -Xss To set up , When there is more data stored in the stack , We need to increase this value properly , Otherwise java.lang.StackOverflowError abnormal . The common exception is recursion that cannot be returned , Because the information stored in the stack at this time is the record point returned by the method
边栏推荐
- 2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
- How to choose the right server for website data collection?
- Possible problems and solutions of using scroll view to implement slider view
- 【LeetCode】100. Same tree
- 嵌入式系统开发笔记79:为什么要获取本机网卡IP地址
- 2022年上海市安全员C证考试题模拟考试题库及答案
- 软件研发的十大浪费:研发效能的另一面
- TCP server communication flow
- 2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
- OdeInt与GPU
猜你喜欢
Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
2022危险化学品生产单位安全生产管理人员题库及答案
[human version] Web3 privacy game in the dark forest
2022年T电梯修理题库及模拟考试
ThreeJS开篇
Hololens2 development environment building and deploying apps
JD intelligent customer service Yanxi intention system construction and intention recognition technology introduction
Leetcode learning - day 36
About the transmission pipeline of stage in spark
MySQL advanced -- you will have a new understanding of MySQL
随机推荐
2022年化工自动化控制仪表操作证考试题库及答案
Threejs opening
Caijing 365 stock internal reference | the first IPO of Beijing stock exchange; the subsidiary of the recommended securities firm for gambling and gambling, with a 40% discount
Common thread methods and daemon threads
(12) Somersault cloud case (navigation bar highlights follow)
网站服务器:好用的网站服务器怎么选这五方面要关注
什么是权限?什么是角色?什么是用户?
1. Mobile terminal touch screen event
"Target detection" + "visual understanding" realizes the understanding of the input image
Coinbase in a bear market: losses, layoffs, stock price plunges
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
一些小知识点
Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
TCP server communication flow
TASK04|數理統計
VIM简易使用教程
C language games (I) -- guessing games
Internet winter, how to spend three months to make a comeback
Learn Chapter 20 of vue3 (keep alive cache component)