top

Search

Python Tutorial

.

UpGrad

Python Tutorial

Memory Management in Python

Introduction

For software engineers to operate effectively with any programming language, memory management in Python is crucial.  Python is a well-known and popular programming language that is utilized in practically every technical field. 

Overview

Memory management is connected to developing memory-efficient code, unlike programming languages. While implementing a significant quantity of data, memory management is crucial and cannot be ignored. Application and server-side components become sluggish due to poor memory management. It also contributes to incorrect functioning. So, in order to create manageable and effective code, we need to be familiar with the Python memory management.

Python Memory Allocation  

Understanding Memory allocation is critical for any software developer since designing efficient code also implies writing memory-efficient code. Memory allocation is the process of assigning a block of memory in a computer to a program. The memory allocation and deallocation methods in Python are automated because the Python developers designed a garbage collector for Python so that the programmer does not have to do regular garbage removal.

Static Memory Allocation

Programs can use either static memory allocation or dynamic memory allocation when using virtual memory.

Static RAM is allocated during the compilation process. For instance, in C/C , a static array with fixed sizes is declared. During compilation, memory is allotted. However, we are unable to utilize the memory once more in the subsequent application.

static int b=100;  

Dynamic Memory Allocation

In contrast to static memory allocation, dynamic memory gives the software real time memory allocation. For instance, in C/C , the size of the integer and float data types are predefined, while the sizes of other data types are not. The objects are given memory at run time. To implement dynamic memory management, we employ the heap. The entire application can make use of the RAM.

 int *a;   p = new int;   

Because Python treats everything as an object, dynamic memory allocation motivates Python memory management. When an object is no longer in use, the Python memory management automatically deletes the object.

Python's memory allocation and deallocation is completely automated. The user is not required to preallocate or deallocate memory in the same way that dynamic memory allocation is used in languages such as C or C . Python employs two memory allocation strategies:  Garbage collection and reference counting

Garbage Collection  

Programming languages employ garbage collection as a memory management approach to automatically free up memory that is no longer needed or accessible by the application. It aids in the prevention of memory leaks, the optimization of memory utilization, and the efficient allocation of memory for the application.

Advantages of Garbage Collection  

  • Automated memory management
  • Memory management simplified
  • Fast memory cleanup
  • Customizable settings

Reference Counting  

Python and other computer languages use reference counting, a memory allocation technique, to manage memory dynamically by measuring how many times an instance is referred. In the Python programming language, a reference count, or the number of references which connect to an object, is a property of each object. When an object's reference count approaches 0, it is no longer referable and its memory can be released.

Work of Stack Memory  

Memory is divided into two parts: stack and heap memory in Python.

The methods/method calls and pointers are kept in stack memory, but the data are kept in a private heap.

Memory space is allocated in contiguous chunks. Because the allocation takes place in the function call stack, we refer to it as stack memory allocation. The compiler is aware of the size of memory that needs to be allocated, and whenever a function is called, memory is allocated for its variables on the stack. It is the memory that is only required during the call to a certain function or procedure. 

Work of Heap Memory  

The RAM is allocated when programmers are executing their instructions. Keep in mind that the heap data structure has nothing to do with the word heap. The reason it is called a heap is because it is a collection of memory that programmers may allocate and free up. The variables that are used in numerous functions globally or that are required outside of method or function calls are kept in heap memory in Python.

Default Python Implementation 

An object-oriented, open-source programming platform called Python is by default developed in the C programming language. It's a fascinating truth that the most popular language is written in another language. But this is only somewhat of the truth—not the whole truth. The Python programming language is primarily written in English. The reference handbook defines it, although it isn't really helpful on its own. Therefore, we require an interpreter-based code for the manual's rule. The advantage of the default approach is that it both runs our Python code on the computer and transforms it into instructions. 

Python Objects in Memory  

Everything in Python is an object, as we all know. The object may be straightforward (containing only strings, integers, etc.) or it may be a container ( list, or user-defined class). In Python, we may use variables and their types without having to declare them beforehand.

Let's analyze the following example.

aa= 100  
print(aa)  
 del aa  
print(aa)  

Output:

100
Traceback (most recent call last):
  File "", line 1, in 
    print(x)
NameError : name 'aa' is not defined

The value was allocated to object x and printed in the above example. An error stating that the variable x is not declared will appear when we delete the object x and attempt to access it in other code.

Reference Counting in Python 

How many times an item is referenced by other objects is determined by reference counting. The count of the object is increased by one whenever a reference to it is assigned. The count of an item decreases as references to it are eliminated or destroyed. De-allocation is carried out by the memory optimization in Python when the reference count reaches zero. Let's make it easy to comprehend.

Transforming the Garbage Collector  

While reference counting is a straightforward method to free memory occupied by objects with a reference count of zero, it falls short when dealing with cyclic references. Objects with interdependencies won't be released from memory. To address this limitation, Python introduced the concept of Generational Garbage Collection (GC).

Generational Garbage Collection categorizes objects into three generations, each with its threshold value. This threshold indicates the number of objects allowed in a generation before garbage collection is triggered.

The garbage collector promotes an object to a higher generation each time it survives a garbage collection cycle.

In Python, the gc module provides control over the internal Garbage Collector. It allows you to check the current number of objects in each generation using the get_count() function. To manually initiate garbage collection, you can use the collect() function. To view and adjust the thresholds of each generation, the get_threshold() and set_threshold() functions come in handy.

The usage of the gc module is shown in the following example:

import gc
gc.get_count()
# (529, 6, 3)
gc.collect()
# 517
gc.get_count()
# (20, 0, 0)
gc.get_threshold()
# (700, 10, 10)
gc.set_threshold(1200, 25, 25)
gc.get_threshold()
# (1200, 25, 25)

We chose the threshold values of 1200, 25, and 25 for the above-mentioned case. The trash collector has to run the collection cycle less often as a result.

Importance of Performing Manual Garbage Collection  

The Python interpreter manages the program's object references, as we've already described. Whenever the number of references drops to zero, the Python memory profiler is immediately released. If reference counting doesn't work when the program has referenced cycles, this is a typical way to try. When two or more items are referred to by one another, the reference cycle takes place. The reference count never decreases to zero as a result.

C Python Memory Management  

On top of the current malloc provided by the C language, CPython provides its own memory management. When allocating memory to newly generated objects, this memory manager offers Python the advantage. The memory manager in Python is designed to handle modest amounts of data at once. Since data in Python is often entered or removed as a single object or as a component of a collection like a list or dict. The effectiveness of CPython's memory management over the conventional malloc appears to be a significant benefit.

The CPython memory allocation scheme consists of three primary parts:

Arena: The biggest memory blocks in memory, aligned on a page border. The page boundary, which is the border of a fixed-length contiguous chunk of memory, is used by the operating system. Python makes the 256 kilobyte page size assumption for the OS.

Pools: It is made up of only one size class. A double-linked list is managed by a pool of the same size. A pool has to be either full, empty, or in use. Blocks of memory for storing data make up an utilized pool. All the assigned and contained data is present in a complete pool. Since an empty pool has no data, any block size class may be assigned to it as needed.

Blocks: Pools carry a reference to the memory block that is currently "free" for them. There is a pointer in the pool that points to the available block of memory. These blocks are not touched by the allocator until they are truly required.

Common Ways to Reduce the Space Complexity  

Prevent list-slicing:

In Python, we specify a list, and the memory allocator allocates space for the Heap in accordance with the indexing of the list. If the supplied list needs a sub-list, list slicing is what we would do. The process of getting the sublist from the main list is simple. It somehow works for tiny amounts of data but not for vast amounts. List slicing produces copies of the objects in the list as a result. Just the reference to them is copied. As a consequence, the Python memory allocator allocates and makes a duplicate of the object. Therefore, we must avoid list slicing.

Use list indexing with caution:

To reduce time and space, the developer should strive to utilize "for item in array" rather than "for index in range(len(array))". Don't utilize it if our software doesn't require the indexing of the list element.

Concatenation of Strings:

String concatenation is not a good option for reducing complexity or space requirements. Since strings are immutable, we should try to avoid using ' ' when concatenating strings. Python produces a new string and allots it to a new address when we add a new string to an old string.

Depending on the character and its length, every string requires a predetermined amount of memory. When we modify the string, we must reallocate memory since its requirements have changed.

Conclusion

Python's memory management, while abstracted from the developer, plays a critical role in the language's efficiency and reliability. By grasping Python memory allocation, garbage collection, and reference counting, developers can write more efficient code and ensure their Python applications perform optimally.

FAQs

1. What are Python's two memory management techniques?

Python supports both static and dynamic memory allocation.

2. What is memory management?

Memory management is the process of managing and coordinating a computer's core memory. It ensures that memory space blocks are correctly regulated and allocated, ensuring that the operating system and applications have the memory they need to fulfill their duties.

3. What does Python's memory management consist of?

In order to manage memory, Python uses a private heap that houses all of its objects and data structures. The Python memory manager is responsible for internal administration of this private heap.

4. What is private heap in python?

The private heap in Python is where objects and data structures are stored, managed by the Python memory manager.

Leave a Reply

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