Static, Heap and Stack

STATIC MEMORY

Static memory persists throughout the entire life of the program, and is usually used to store things like global variables, or variables created with the static clause. Global variables are static, and there is only one copy for the entire program. Inside a function the variable is allocated on the stack. It is also possible to force a variable to be static using the static clause. For example, the same variable created inside a function using the static clause would allow it to be stored in static memory.

  • int counter;
  • static int intLoop;

 

STACK MEMORY

The stack is used to store variables used on the inside of a function (including the main() function). It’s a LIFO, “Last-In,-First-Out”, structure. Every time a function declares a new variable it is “pushed” onto the stack. Then when a function finishes running, all the variables associated with that function on the stack are deleted, and the memory they use is freed up.

 

Stack Overflow

Note that there is generally a limit on the size of the stack. If a program tries to put too much information on the stack, stack overflow will occur. Stack overflow happens when all the memory in the stack has been allocated, and further allocations begin overflowing into other sections of memory.

  • the stack is managed by the CPU;
  • variables are allocated and freed automatically;
  • the stack it not limitless – most have an upper bound;
  • the stack grows and shrinks as variables are created and destroyed;
  • stack variables only exist whilst the function that created them exists;

HEAP MEMORY

 

The heap is a large pool of memory that can be used dynamically – it is also known as the “free store”. This is memory that is not automatically managed – you have to explicitly allocate and free the memory. Failure to free the memory when you are finished with it will result in what is known as a memory leak – memory that is still “being used”, and not available to other processes. Unlike the stack, there are generally no restrictions on the size of the heap, other than the physical size of memory in the machine. Variables created on the heap are accessible anywhere in the program.

  • the heap is managed by the programmer;
  • in C, variables are allocated and freed using functions like malloc() and free()
  • the heap is large, and is usually limited by the physical memory available
  • the heap requires pointers to access it

In short, the Stack is the temporary memory where variables are stored while a function is executing. When the function finishes, the memory is cleared automatically. The Heap is memory that the programmer can use for the application in a more manual way. You have to allocate memory, use it, and then free it up afterwords all by hand.

 ESP32

We simply call the getFreeHeap method of the ESP extern variable. This method takes no arguments and returns as output the free heap, in bytes.

  • Serial.println(ESP.getFreeHeap());

 

More Information

 

 

RELATED ARTICLES

Leave a comment

Your email address will not be published. Required fields are marked *

Please note, comments must be approved before they are published