当前位置:网站首页>Introduction to JVM stack and heap

Introduction to JVM stack and heap

2022-07-01 04:38:00 xmh-sxh-1314

bae4694ca5d84a82841d03b454a69196.jpg 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

 

原网站

版权声明
本文为[xmh-sxh-1314]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010431349150.html