Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

How are variables stored in python – Stack or Heap?

Last Updated on June 23, 2022 by Ria Pathak

Memory allocation is the process of allocating memory to a computer program. In Python, the process of memory allocation and deallocation is handled automatically as there is a garbage collector which is created in python so that users don’t have to handle the process of garbage collection.

Garbage Collection

It is a process in which memory is deallocated when it is not in use to make the space available for other objects. In case, when no one is referencing the object in memory then in the virtual machine, the garbage collector comes to work to automatically delete the object in the heap memory.

Reference Counting

It is a procedure that works by counting the number of times an object is referenced by other objects in the computer. This count for an object is decremented when reference to an object is removed. And when this count becomes zero, then that object is deallocated.
For example, in the case of two variables having the same value. Then the virtual machine instead of making two different objects, it makes two pointers pointing at the same object created in the private heap.

Example:

b = 3
a = 6

b = 6
print(id(a) == id(b))

Output: True

Memory allocation in Python

There are two parts of memory:

  • Stack memory
  • Heap memory

The function calls and the references are stored in the stack memory whereas all the value objects are stored in the heap memory.

Work of Stack Memory

It is called stack memory allocation as the allocation happens in the function call stack. At the time of allocation, the memory size is known to the compiler and when there is a function call that happens, then its variable gets memory allocated on the stack.
It is the memory that is needed inside a particular function. When the function is called, it will be added to the program call’s stack. Variable initialization inside the function is temporarily stored in the function call stack, where it will be deleted after the completion of the function. This memory allocation onto a contiguous memory is handled by the compiler, developers do not have to worry about it.

Example:

def func():
      
    a = 10
    b = ""
    c = {}

Work of Heap Memory

Heap memory allocation is done when memory is allocated at the time of execution of a program written by the programmer. As there is a pile of memory space available in the process of allocation and de – allocation, that’s why it is known as heap memory. The variable which is required globally in the program is stored in heap memory.

Example:

# This memory for 5 integers is allocated in the heap.
arr = [0] * 5

This article tried to discuss How are variables stored in python – Stack or Heap?. Hope this blog helps you understand the concept. To practice more problems feel free to check MYCODE | Competitive Programming at Prepbytes.

Leave a Reply

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