当前位置:网站首页>Briefly describe the difference between heap and stack
Briefly describe the difference between heap and stack
2022-07-29 09:24:00 【Hua Weiyun】
Pile up (Heap)
Java The heap is an area of memory Shared by all threads , Created when the virtual machine starts ;
Java Virtual machine specification description : All object instances and arrays are allocated on the heap ;
Java The heap can be in a physically discontinuous memory space , As long as it's logically continuous ;
( Thread sharing ) Objects in heap memory are visible to all threads . Objects in heap memory can be accessed by all threads ;
( Abnormal prompt ) If there is no free space in the heap memory to store the generated objects ,JVM Will throw out java.lang.OutOfMemoryError;
( Memory allocation ) Dynamic memory allocation
Stack (Stack)
Store references to basic types of data and objects , That is, store variables ;
If the basic type data is stored ( Nonstatic variable ), Then the variable name and value are directly stored in stack In memory ;
If it's a reference type , Then store the variable name on the stack , Then point to it new The object of ( Store in a pile );
( Thread private ) Stack memory belongs to a single thread , Each thread will have a stack memory , The stored variables can only be seen in the thread to which they belong , That is, stack memory can be understood as thread's private memory ;
( Memory allocation ) Stack memory is much smaller than heap memory , If you use recursion , Then your stack will soon be full of . If recursion doesn't jump out in time , It is likely that StackOverFlowError problem ;
( Abnormal prompt ) If there is no space available in the stack memory to store method calls and local variables ,JVM Will throw out java.lang.StackOverFlowError;
( Memory allocation ) Fixed memory allocation ;
Access speed is faster than heap , Next to registers , Stack data can be shared ;
attach Stack data sharing
Stack has a very important particularity , Data stored in the stack can be shared . Suppose we define it at the same time :
int a = 3;
int b = 3;
The compiler processes int a = 3; First, it creates a variable in the stack as a References to , Then find out if there is any in the stack 3 This value , If not , will 3 Store in , And then a Point to 3. And then deal with int b = 3; After creating b After referencing variables for , Because there are already 3 This value , It will be b Direct to 3. such , And that's what happened a And b At the same time, they all point to 3 The situation of .
At this time , If you order again a=4; Then the compiler will re search the stack for any 4 value , without , Will 4 Store in , And order a Point to 4; If there are already , Will directly a Point to this address . therefore a Changes in values do not affect b Value .
Note that this sharing of data is different from the sharing of two references to one object at the same time , Because of this situation a The modification of will not affect b, It's done by the compiler , It helps save space . An object reference variable modifies the internal state of the object , Affects another object reference variable .
String It's a special packaging data . It can be used :
String str = new String("abc");
String str = "abc";
Two ways to create , The first is to use new() To create a new object , It will be stored in the heap . Each call creates a new object .
And the second is to create a pair in the stack first String Class object reference variables str, Then find out if there is any storage in the stack "abc", without , Will "abc" Put it on the stack , And order str Point to ”abc”, If there is already ”abc” Direct order str Point to “abc”.
When comparing whether the values in a class are equal , use equals() Method ; When testing whether references to two wrapper classes point to the same object , use ==, Let's illustrate the above theory with examples .
String str1 = "abc";
String str2 = "abc";
System.out.println(str1==str2); //true
It can be seen that str1 and str2 It's pointing to the same object .
String str1 =new String ("abc");
String str2 =new String ("abc");
System.out.println(str1==str2); // false
use new The way is to generate different objects . One at a time .
So create multiple... In the first way ”abc” character string , There is only one object in memory . This writing method is conducive to saving memory space . At the same time, it can improve the running speed of the program to a certain extent , because JVM It will automatically determine whether it is necessary to create new objects according to the actual situation of the data in the stack . And for String str = new String("abc"); Code for , Create new objects in the heap , Whether the string value is equal or not , Is it necessary to create new objects , Thus increasing the burden of the procedure .
On the other hand , it is to be noted that : We are using things like String str = "abc"; When defining a class , Always assume that , Created String Class object str. Worry about the trap ! The object may not have been created ! It might just point to a previously created object . Only through new() Method to ensure that a new object is created each time .
because String Class immutable nature , When String When a variable needs to change its value frequently , Should consider using StringBuffer( Thread safety , Low efficiency ) class , To improve program efficiency .
边栏推荐
猜你喜欢
Notes on network principles (five layer network)

Flowable 高级篇

四元数与其在Unity中的简单应用

MySQL事务与MVCC如何实现的隔离级别

《UnityShader入门精要》总结(2):初级篇

First order traversal / second order traversal determines the approximate shape of the tree

On the charm of code language

Leetcode:132. split palindrome string II

Retinal Vessel Segmentation via a Semantics and Multi-Scale Aggregation Network

乱打日志的男孩运气怎么样我不知道,加班肯定很多
随机推荐
【集中培训】HCIP-Cloud Computing 资源交流帖
Network knowledge summary
基于ArkUI eTS开发的坚果新闻(NutNews)
查看端口占用情况
Acwing game 59 [End]
Retinal Vessel Segmentation via a Semantics and Multi-Scale Aggregation Network
【Unity入门计划】常用学习网址收藏
[LOJ 6485] LJJ binomial theorem (unit root inversion) (template)
数仓项目踩坑记录与解决方法总结
Flowable 基础篇1
[centralized training] hcip cloud computing resource exchange post
Configuration file settings for remote connection to Windows version server redis
基于C语言实现的NFA确定化和DFA最小化
分布式Session共享的4类技术方案,与优劣势比较
原型链继承和构造函数继承的 “毛病”
Reptile practice (10): send daily news
One article tells you the salary after passing the PMP Exam
云原生管理实践:业务引领的DevOps持续交付体系
Leetcode question brushing (6)
文件上传及拓展