Do you know about stack memory !!

Shainil P SShainil P S
4 min read

I always wondered how memory management works in programming language. i saw lot of videos , read some blog and finally came to understand these concepts. Well you might know already how memory management works in programming but still you can read this, who knows you might find some things to correct me or yourself. Well i'm gonna explain stack memory using java only cause the blog will be too long to explain about heap,class memory etc.

What are the key points you will learn:

  • What is stack and stack memory?

  • How stack memory works ?

  • How functions works in stack?

  • How stack memory will be freed ?

lets begin with one by one.

What is stack and stack memory?

The basic definiton of stack memory is "Stack memory is a region of computer memory used for managing function calls and local variables. It follows a last-in, first-out (LIFO) approach, where the last item added is the first to be removed"

what do you mean by this ? The stack memory obviously follows the principle of a stack data structure. for those who don't know about stack , stack is a data structure similar to array where adding of new element and removal of new element takes same end. that's why it follows LIFO approach. In stack the first inserted element can be removed at last. means if you have an element already the next element will sit on top of it. now you can't remove the last element without removing the previous element. You can imagine stack as a jar filled with burgers in which each burger will sit on top of another burger and if you want to remove the first inserted burger you have to remove the every other burgers.

How stack memory works ?

Now you know what stack and stack memory is , let us know how this works in programmming. In the end we are here to know how stack memory works in run time

Now let us consider this example

class Main{

public static void main(String[] args){

int num1=10;

int num2=12;

int sum=num1+num2;

System.out.println(sum);

}}

This code doing a addtion of two numbers . where all the variables are of type int, which means 4 bytes in Java.

Now let us see how the memory allocation takes in the runtime. first, the variable num1 will go to stack memory and allocate 4 bytes of space. (point to be noted is num1 and other variable name is just used by compiler hence the variable name will not go to stack instead the actual values will be stored). then num2 will go then sum . when sum goes to stack it will be like num1+num2 (10+12) . After the last line everyting will be get destoyed from bottom to top.

Stack Memory:
+------------------------+
|                        |
|         main()         |
|      ------------      |
|      |  args[]  |      |    <--- args array (command-line arguments)
|      ------------      |
|      |   num1   |  10  |    <--- num1 variable with value 10
|      ------------      |
|      |   num2   |  12  |    <--- num2 variable with value 12
|      ------------      |
|      |   sum    |  22  |    <--- sum variable with value 22
|      ------------      |
|                        |
|                        |
+------------------------+

from the above diagram we can see how this working internally. ignore the args[] it is just an argument of command line. here a frame is created as main() .inside that frame every things is inserted. we are gonna look into frames in below. Here in main frame varialbe a,b,sum values are inserted. after the exection the memory will be freed from bottom to top.

How functions works in stack ?

In stack memory funtions create a frame as main() function as showed above.You can consider a frame as a box which have it's own methods and variables. so whenever the function get executed, then that frame will be discarded. Now you know why variable that are inside a function cannot be accessed from outside. Its becasue the varialble/value is inside a frame. After the frame execute that will be automatically discarded.

How stack memory will be freed ?

Unlike heap memory, which is responsible for holding non-primitive values, stack does not have any garbage collection to free the space. Now you can ask how exaclty the memory will be freed ? right. In stack once the variables get unusable that will be discarded. consider a variable inside a function/frame after the execution of that function the variable that are inside that frame gets unusable so it will be discarded automatically. The same actions happens with the functions. This is more efficient than the heap and garbage collector because the memoy allocation and deacllocation are done at rapid speed. This helps to run the program in fast.

Conclusion:

Stack memory is really efficient at storing the primitive values. This is the core memory of program that is running. everything that are needed to execute the program utlimately get's stored here. Even if it does not store non-primitive values stack does holds the reference of that. This makes this memory most important one. This is like a RAM, fast and efficient and but small in size.

0
Subscribe to my newsletter

Read articles from Shainil P S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shainil P S
Shainil P S

Hey, This is shainil. I m learning backend development along with dsa in cpp<3