当前位置:网站首页>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
边栏推荐
- [Master / slave] router election in DD message
- 2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
- 【LeetCode】100. Same tree
- Shell之分析服务器日志命令集锦
- Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
- 【发送邮件报错】535 Error:authentication failed
- "Target detection" + "visual understanding" realizes the understanding of the input image
- Annual inventory review of Alibaba cloud's observable practices in 2021
- Odeint et GPU
- Maixll-Dock 快速上手
猜你喜欢

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

The index is invalid

After many job hopping, the monthly salary is equal to the annual salary of old colleagues

VR线上展览所具备应用及特色

嵌入式系统开发笔记79:为什么要获取本机网卡IP地址

Loop filtering based on Unet

283. move zero

Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions

2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank

Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
随机推荐
Extension fragment
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
Haskell lightweight threads overhead and use on multicores
2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试
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
Announcement on the list of Guangdong famous high-tech products to be selected in 2021
Use winmtr software to simply analyze, track and detect network routing
Learn Chapter 20 of vue3 (keep alive cache component)
JMeter learning notes 2 - brief introduction to graphical interface
2. Use of classlist (element class name)
Loop filtering based on Unet
Shell之Unix运维常用命令
Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
One job hopping up 8K, three times in five years
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?
Embedded System Development Notes 80: using QT designer to design the main interface
2022 question bank and answers for safety production management personnel of hazardous chemical production units
C language games (I) -- guessing games
Task04 | statistiques mathématiques