Differences between Heap memory and Stack memory in programming

Tram Ho

1. Introduction

  • As we all know, Memory Management for a programmer is very important.
  • An important purpose of memory management is to provide ways to dynamically allocate memory cells to the program when requested and to free those memory cells when they are no longer needed. This is very important for any high-end computer system because there will be more work to be done at all times.
  • Many methods have been found to increase the efficiency of memory management. Virtual memory systems help separate the currently used memory addresses from the real addresses, thereby enabling business sharing and increasing RAM efficiently by marking addresses or moving to storage areas. second storage. The quality of virtual memory management can have a huge impact on overall system performance.

2. Stack and Heap?

  • Heap memory and Stack memory are essentially the same memory that is created and stored in RAM when the program is executed.
  • Stack memory is used to store local variables in functions, parameters, etc. Access to this memory is very fast and is executed when the program is compiled.
  • Heap memory is used to store memory for cursor variables dynamically allocated by malloc – calloc – realloc (in C) or new (in c ++, c #, java, …).Example in C ++ programming language:

    In addition, there are many key points to compare the difference between Heap memory and Stack memory such as:
  • Device size
    Stack: The size of the Stack memory is fixed, depending on the operating system, for example, Windows operating system is 1 MB, Linux operating system is 8 MB (note that figures may vary depending on the knowledge). structure of your operating system).
    Heap: Heap memory size is not fixed, can increase or decrease thus meeting the data storage needs of the program.
  • Device characteristics
    Stack: Stack memory is managed by the operating system, data stored in the Stack will automatically destroy when the function is done its job.
    Heap: Heap memory is managed by the programmer (in C or C ++), the data in the Heap will not be destroyed when the function is done, which means you have to manually destroy the memory area with the free command (in C), and delete or delete [] (in C ++), otherwise a memory leak will occur. In high-level programming languages ​​like .NET, Java, … there is automatic garbage collection (Garbage Collection), you do not need to manually destroy the Heap memory anymore.

Note: automatic cleaning of devices depends on the intermediate compiler.

  • Error occurred with the device
    Stack: because the Stack memory is fixed, if the program you use too much memory exceeds the storage capacity of the Stack, Stack Overflow is likely to occur. you initialize too many local variables, infinite recursive functions, …
    Example of Stack Overflow with infinite recursive function:

    Heap: If you continually allocate memory without releasing it, there will be an Heap overflow.
    If you initialize a memory that is too large for the Heap to be stored at once, you will fail to initialize the Heap.
    For example, if the initialization of Heap is too large:

3. So when should I use Heap memory and Stack memory?

  • You use Stack if you know the exact amount of data you allocate before compiling and the data is not too large. On the contrary, you should use Heap …

Note: In multithreading applications, each thread (thread) will have its own Stack memory area, while all threads share a Heap memory area. Sharing the Heap memory means syncing to avoid conflicts between threads, so the Heap memory allocation has to install some more mechanisms so it takes longer than Play the Stack memory. Constant allocation and disposal of Heap memory can result in memory fragmentation, since memory fragmentation can lead to a failed memory allocation error as described above.

4. References

  • This article I have shared with you some knowledge about memory that I have learned. Hopefully the article will bring some valuable information to you.
  • You can read more knowledge related to Memory Management at the following link: Memory management in programming
Share the news now

Source : Viblo