Stack 和 Heap的区别

作者: V哥的博客 | 来源:发表于2019-03-10 04:40 被阅读0次
  • 谁在用

    • 一般情况下每个thread都有自己的Stack,通过main开始创建,到func结束后消失;每个program都有自己的Heap
  • 根据variable

    • Basic type,eg:int string double,还有一些复杂的类型相如array,都会存在stack
    • 一些large array 或者需要占用很大内存的variable,或者某个variable需要保持很长时间(全局变量),都会存在heap中
    int x = 1;            //Priamary type //Stack
    int y = 2;            //Priamary type //Stack
    Form1 frm = new From1();  //Reference type //heap
    
  • Stack将会在main function创建,在function结束时会被清除,也就是说,stack中的东西会自行管理,不用用户“瞎操心”,速度极快,同时也是最常用的内存地区

  • Heap会通过malloc realloc来开始和结束heap,或是From1 frm = new From1()frm就是存在Heap中

Stack

What 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

Heap

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 to use the Heap?

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.

Reference

https://www.gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html

http://net-informations.com/faq/net/stack-heap.htm


想要看到更多玮哥的学习笔记、考试复习资料、面试准备资料?想要看到IBM工作时期的技术积累和国外初创公司的经验总结?

image

敬请关注:

玮哥的博客 —— CSDN的传送门

玮哥的博客 —— 简书的传送门

玮哥的博客 —— 博客园的传送门

相关文章

  • heap和stack的区别

    1.heap是堆,stack是栈。2.stack的空间由操作系统自动分配和释放,heap的空间是手动申请和释放的,...

  • Stack 和 Heap的区别

    谁在用一般情况下每个thread都有自己的Stack,通过main开始创建,到func结束后消失;每个progra...

  • (转)Objective-C 拾遗:从Heap and Stac

    Objective-C 拾遗:从Heap and Stack到Block Stack和Heap heap和stac...

  • JS-深拷贝和浅拷贝

    要了解其本质区别,就需要了解堆和栈,值与引用的概念及区别 1.堆(stack)和栈(heap) stack为自动分...

  • swift~基础(整理)

    1,object-c和swift中struct、class的区别(包括heap和stack的区别) 大部分开发人员...

  • JVM内存模型

    JVM的内存中的堆(Heap)和栈(Stack)有什么区别 Stack是存放方法的局部变量的内存空间,每个方法都会...

  • 堆和栈(Heap and Stack)的区别!

    堆和栈最明显的区别是: 堆(Heap):队列优先,先进先出(FIFO—first in first out); 栈...

  • 浅析 Rust 所有权

    一、什么是 stack 和 heap stack 和 heap 都用于变量的内存存储。对于大多数的编程人员来讲,都...

  • 二、JavaScript 中的堆内存和栈内存

    1 栈 stack”和“堆 heap” 简单的来讲,stack上分配的内存系统自动释放,heap上分配的内存,系统...

  • 堆栈

    栈 (stack)和 堆 (heap) stack 为自动分配的内存空间,它由系统自动释放;而heap则是动态分...

网友评论

    本文标题:Stack 和 Heap的区别

    本文链接:https://www.haomeiwen.com/subject/qtiypqtx.html