当前位置:网站首页>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 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
- 使用WinMTR软件简单分析跟踪检测网络路由情况
- What is uid? What is auth? What is a verifier?
- Redis (VII) optimization suggestions
- 2022年聚合工艺考试题及模拟考试
- Web server: how to choose a good web server these five aspects should be paid attention to
- 尺取法:有效三角形的个数
- OdeInt與GPU
- Task04 mathematical statistics
- Leetcode learning - day 36
猜你喜欢
Common thread methods and daemon threads
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
How to use maixll dock
OdeInt与GPU
Mallbook: how can hotel enterprises break the situation in the post epidemic era?
Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
How to do the performance pressure test of "Health Code"
Use winmtr software to simply analyze, track and detect network routing
Threejs opening
【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
随机推荐
OdeInt与GPU
Loop filtering based on Unet
尺取法:有效三角形的个数
2022 t elevator repair new version test questions and t elevator repair simulation test question bank
206. reverse linked list
[leetcode skimming] February summary (updating)
JD intelligent customer service Yanxi intention system construction and intention recognition technology introduction
Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
【发送邮件报错】535 Error:authentication failed
How to ensure the idempotency of the high concurrency interface?
Use winmtr software to simply analyze, track and detect network routing
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
Daily question - line 10
The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears
Concurrent mode of different performance testing tools
嵌入式系统开发笔记81:使用Dialog组件设计提示对话框
JS rotation chart
嵌入式系统开发笔记79:为什么要获取本机网卡IP地址
Rule method: number of effective triangles