Stack 和 Heap的区别
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Stack 和 Heap的区别相关的知识,希望对你有一定的参考价值。
参考技术AWhat is the stack? It\'s a special region of your computer\'s memory that stores temporary variables created by each function (including the main() function). The stack is a "LIFO" (last in, first out) data structure, that is managed and optimized by the CPU quite closely. Every time a function declares a new variable, it is "pushed" onto the stack. Then every time a function exits, all of the variables pushed onto the stack by that function, are freed (that is to say, they are deleted). Once a stack variable is freed, that region of memory becomes available for other stack variables.
The advantage of using the stack to store variables, is that memory is managed for you. You don\'t have to allocate memory by hand, or free it once you don\'t need it any more. What\'s more, because the CPU organizes stack memory so efficiently, reading from and writing to stack variables is very fast.
Another feature of the stack to keep in mind, is that there is a limit (varies with OS) on the size of variables that can be stored on the stack. This is not the case for variables allocated on the heap .
https://www.gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html
The heap is a region of your computer\'s memory that is not managed automatically for you, and is not as tightly managed by the CPU. It is a more free-floating region of memory (and is larger). To allocate memory on the heap, you must use malloc() or calloc() , which are built-in C functions. Once you have allocated memory on the heap, you are responsible for using free() to deallocate that memory once you don\'t need it any more. If you fail to do this, your program will have what is known as a memory leak . That is, memory on the heap will still be set aside (and won\'t be available to other processes). As we will see in the debugging section, there is a tool called valgrind that can help you detect memory leaks.
Unlike the stack, the heap does not have size restrictions on variable size (apart from the obvious physical limitations of your computer). Heap memory is slightly slower to be read from and written to, because one has to use pointers to access memory on the heap. We will talk about pointers shortly.
Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope.
When should you use the heap, and when should you use the stack? If you need to allocate a large block of memory (e.g. a large array, or a big struct), and you need to keep that variable around a long time (like a global), then you should allocate it on the heap. If you are dealing with realtively small variables that only need to persist as long as the function using them is alive, then you should use the stack, it\'s easier and faster. If you need variables like arrays and structs that can change size dynamically (e.g. arrays that can grow or shrink as needed) then you will likely need to allocate them on the heap, and use dynamic memory allocation functions like malloc() , calloc() , realloc() and free() to manage that memory "by hand". We will talk about dynamically allocated data structures after we talk about pointers.
https://www.gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html
http://net-informations.com/faq/net/stack-heap.htm
想要看到更多玮哥的学习笔记、考试复习资料、面试准备资料?想要看到IBM工作时期的技术积累和国外初创公司的经验总结?
敬请关注:
玮哥的博客 —— CSDN的传送门
玮哥的博客 —— 的传送门
玮哥的博客 —— 博客园的传送门
heap&stack的区别
1.
heap (堆)是一个可动态申请的内存空间,一般所有创建的对象和数组都放在这里。
stack (栈)是一个先进后出的数据结构,通常用于保存方法(函数)中的参数,局部变量。
stack (栈)的空间小,但速度比较快, 存放对象的引用,通过栈中的地址索引可以找到堆中的对象。
2.
stack的空间由操作系统自动分配和释放,
heap的空间是手动申请和释放的,heap常用new关键字来分配。
stack空间有限,heap的空间是很大的自由区。
在Java中,
若只是声明一个对象,则先在栈内存中为其分配地址空间,
若再new一下,实例化它,则在堆内存中为其分配地址。
栗子:
数据类型 变量名;这样定义的东西在栈区。
如:Object a =null; 只在栈内存中分配空间
new 数据类型();或者malloc(长度); 这样定义的东西就在堆区
如:Object b =new Object(); 则在堆内存中分配空间
当在一段代码块定义一个变量时,Java就在栈中为这个变量分配内存空间,当超过变量的作用域后,Java会自动释放掉为该变量所分配的内存空间,该内存空间可以立即被另作他用。
以上是关于Stack 和 Heap的区别的主要内容,如果未能解决你的问题,请参考以下文章