How is memory managed in Python? Complete Guide

How is memory managed in Python? Complete Guide

Hey readers, in this article we will understand how memory is managed in python. Even though all the memory management is done automatically by python itself, it is essential for us to know how it is done to become more efficient and makes it easier to work with memory-related issues. Let us get started without any delay.

Introduction

Python is a high-level interpreted programming language. Memory is managed in python automatically unlike other programming languages like C, where the user should do the memory management manually. When writing code in any programming language, we often declare variables, functions, etc and when it comes to large projects there will be many variables. We need to store them for a limited time i.e we restrict them only to that particular project. So there is a need to manage them properly. If not done correctly, there are high chances of slow execution of code. Python memory manager takes the entire responsibility for ensuring the proper management of memory. Let us know about it now.

Python Memory manager

Python memory manager is a tool responsible for the allocation of memory to objects and also its usage. All things in python are objects. All the datatypes, functions, etc get automatically converted to the object data type and get stored in the heap memory. The references to those are stored in the stack memory. There are 2 types of memory allocation in python. Let us understand them.

Static memory allocation

In the static memory allocation type, the memory allocation happens at the time of compilation. We cannot use this memory in the later programs. The memory is pre-allocated to the program before execution and the size of the allocated memory is fixed. A stack data structure is one of the data structures in python which uses the Last In First Out( LIFO) approach to store and fetch data. These are implemented using static memory allocation. An example of memory allocation on a stack is a function. When a function is called, a new stack is created. The parameters and the return value of the function are stored in that stack. Let us understand static memory allocation with an example.

list1 = ["a", "b", "c"]
print(list1)Code language: Python (python)

In the above example, the memory to the list gets allocated before the program runs as the list is declared before the program runs. The size of the list is fixed. So this type of memory allocation is known as static memory allocation. It is one of the ways in which memory is managed in python.

Dynamic memory allocation

In the dynamic memory allocation type, the memory allocation happens at the program’s run time. This type of memory allocation uses memory efficiently as allocated memory can be changed according to the need. The size of the allocated memory is fixed like in the static memory allocation. A heap data structure is one of the data structures in python. These are implemented using dynamic memory allocation.

As you already know, memory management in python is an automated process. Python uses a mixture of garbage collection and references counting to manage memory. Reference counting means the number of times an object is used in the program. The reference count becomes zero when there are no references to the object i.e object is no longer used. When the reference count becomes zero, it gets collected by the garbage collector. Let us understand them in detail. Let us understand static memory allocation with an example.

list1 = []
list1.append("a")
list1.append("b")
print(list1)Code language: Python (python)

In this example, at first, before the program runs, the list declared is an empty one. So memory for the empty list only gets allocated before the program runs. As we know that the list append() function adds elements to the list. As the program runs, the elements get added to the list. Memory to the list gets allocated as the program runs. This type of memory allocation is known as dynamic memory allocation. It is one of the ways in which memory is managed in python.

Reference counting

Reference counting in python is used to keep track of the number of times an object is used in the program. Let us understand reference counting with the help of an example.

a = "codedamn"
b = aCode language: Python (python)

Here, the reference count of a is two. When there are two variables with the same values, instead of creating two objects, python reference the second object to the first object. So in cases of multiple variables with the same values, a lot of memory can be saved.

a = "codedamn"
b = a
del bCode language: Python (python)

In this case, the reference count of a is one. Here only one variable is present as the second variable with the same value as that of the first value is deleted.

a = "codedamn"
b = a
del aCode language: Python (python)

In this case, the reference count drops to zero as the object gets deleted. In this case, as the reference count is zero, it gets collected by the garbage collector. So reference count plays an important role in how memory is managed in python.

Garbage Collector

When the reference count of an object becomes zero, it gets collected by the garbage collector. The garbage collector is an inbuilt module in python. This ensures that the unused memory is retained and used by the other objects. It is an important feature that ensures the efficiency of code by freeing the memory that is not utilized. It gets back the memory used by an object whose references drop to zero. The garbage collector automatically runs in the background thereby avoiding the time-consuming manual memory management. It gets activated only when the reference of an object drops to zero. It is especially useful when there are large programs as it reduces memory utilization and helps in increasing the efficiency of the code. Let us understand how the garbage collector works with the help of an example.

a = "codedamn"Code language: Python (python)

The reference to the object of the variable declared is one.

a = "codedamn"
del aCode language: Python (python)

The del keyword in python is used to delete objects. As we have deleted the objects for the string, the reference gets deleted and drops to zero. This is when the garbage collector comes into action. It recovers the memory previously used by the variable as it is no longer used. The memory that is recovered can be used by other objects which are declared later. This effectively manages memory and also ensures the efficiency of the code.

Tips for memory management

Using the needed data types

A variable is a name given to a value. These values can be of any data type. There are many different data types in Python, including strings, integers, floats, booleans, lists, dictionaries, and more. Using appropriate data types can save a lot of memory in python by only taking the required amount of space necessary for the datatype instead of taking all the unnecessary memory. For example, you will save a lot of memory when you use an integer instead of a string because integers take less memory than that a string. Similarly using of integer instead of a float and using of boolean instead of an integer can also save memory.

Avoiding global variables

A variable declared outside a function is known as a global variable. These variables can be accessed from both inside a function as well as outside of a function, unlike the local variables which can only be accessed inside a block or a function where it is defined. Using frequent global variables can increase memory usage and thus reduce efficiency. 

Libraries and Frameworks

Using libraries and frameworks can help you a lot in memory management. They provide structured code making it easier to debug. They contain less number of lines of code for a particular task thus reducing the amount of memory needed.

Conclusion

Memory management in python has become very important due to the wide range of applications of python from web development to artificial intelligence, machine learning, game development, and many more. In this article, we have discussed the basics of memory management in python with some tips for efficient memory management. That’s it for this article. Learning is a continuous process. Do check out this python course on codedamn that teaches you python from scratch. The outcome of any learning is practicality. Codedamn has inbuilt playgrounds, in which you can practice and also work on projects. So there is no need for you to create an environment to code as all the things are already available. Join the codedamn community to instantly solve all of your doubts.

Frequently Asked Questions – (FAQ)

Why memory management is important?

Memory management is necessary to ensure the efficiency of the program.  If not done correctly, there are high chances of slow execution of code.

What is python memory manager?

Python memory manager is a tool responsible for the allocation of memory to objects and also its usage.

What is reference counting in python?

Reference counting in python is used to keep track of the number of times an object is used in the program.

What is a garbage collector in python?

When the reference count of an object becomes zero, it gets collected by the garbage collector.

What is static memory allocation?

Static memory allocation is a type of memory allocation where the memory allocation occurs at the time of compilation. The memory is pre-allocated to the program before execution and the size of the allocated memory is fixed.

What is dynamic memory allocation?

Static memory allocation is a type of memory allocation where the memory allocation occurs at the run time of the program.

Sharing is caring

Did you like what Manish wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far