Memory Management
A python program is a collection of
Methods (operation, example while adding, add is the method)
References (names given to access the value)
** The reference does not point to a value in Python but points to the memory address of an object
Objects (everything is an object in python)
In python, memory is managed by the Python manager which determines where to put the application data in the memory
The Manager finds the free space and provides it to the application. The procedure of providing memory to objects is called allocation.
Basics
A bit is a binary digit, the smallest increment of data on a computer.
A bit can hold only one of two values: 0 or 1, corresponding to the electrical values of off or on
Because bits are so small, you rarely work with information one bit at a time.
Bits are usually assembled into a group of eight to form a byte.
A byte contains enough information to store a single ASCII character, like "h"
The abbreviations for numbers of bits use a lower-case "b" instead of an upper-case "B"
Speed of 3 Mbps internet speed is equivalent to .375 MBps (calculated as 3/8), significantly lower
1 kilobyte (KB)
1,024 bytes
1 megabyte (MB)
1,048,576 bytes
1 gigabyte (GB)
1,073,741,824 bytes
1 terabyte (TB)
1,099,511,627,776 bytes
1 petabyte (PB)
1,125,899,906,842,624 bytes
Python Memory Allocation
Memory allocation is a process, which basically allots free space in the computer's virtual memory, and there are two types of virtual memory that works while executing programs
Static Memory Allocation
Static memory allocation happens at the compile time
For example - In C/C++, we declare a static array with the fixed sizes
We cannot use the memory again in the further program
The stack data structure is used to store static memory
Dynamic Memory Allocation
Dynamic memory allocates the memory to the objects at the runtime of the program
We use the Heap to implement dynamic memory management
We can use the memory throughout the program
Static memory allocation
Dynamic memory Allocation
Memory is allocated at compile time.
Memory is allocated while the program starts executing.
It is a faster way of memory allocation.
It is a slower way of memory allocation.
Once static memory is allocated, neither its size can be changed, nor it can be re-used. Hence, less efficient.
We can change the memory size after allocation and can be reused as well. Hence, more efficient.
In this case, variables get allocated permanently, and allocated memory remains blocked until the program terminates.
In this case, variables get allocated only when the program unit gets active and releases the memory when variables get out of scope.
Uses stack for memory management.
Uses heap for memory management.
Few important basics
Python does not have anything like ‘variables’, instead, it uses ‘names’
Python ‘name’ is just a label for an object and used to refer to a value.
We never specify ‘type’ information while creating an object.
A python ‘name’ can change its type.
A single Python object can have lots of names.
Two names will point to the same object if the id(x) method returns the same value.
Memory Allocation to Each Data-type
To get the size of an object in python, we can import the sys library and use it
Boolean: 28 bytes for True and 24 bytes for False (similar to 0)
Int: 24 bytes for a positive number, 28 bytes for a negative number, and 64 bytes for long
Float: 24 bytes for all float object types
String:
List: 64 bytes for an empty list and 8 bytes for each object added to the list
Set and dictionary requires more space in comparison to other data types
A tuple requires less space compared to a list. Set and dictionary require more space compare to list and tuple.
Tuple requires an average of 8 bytes per element, a list requires an average of 9 bytes per element, a set requires an average of 33 bytes per element, dictionary requires an average of 41 bytes per pair. This comparison is for int data used in list, tuple, set, and dict (done on 1 Million elements of int data type)
Memory size may not be the only criteria to select a data type. Rather, the time required to perform an operation on data type can be a critical criterion.
Python Garbage Collector
Python removes those objects that are no longer in use or can say that it frees up the memory space
This process of vanish the unnecessary object's memory space is called the Garbage Collector
The Python garbage collector initiates its execution with the program and is activated if the reference count to an object falls to zero
When we assign the new name or placed it in containers such as a dictionary or tuple, the reference count increases its value. If we reassign the reference to an object, the reference counts decrease its value. It also decreases its value when the object reference goes out of scope or an object is deleted
Python uses dynamic memory allocation which is managed by the heap data structure
Python memory manager manages the allocation or de-allocation of the heap memory space through the API functions
References for Further Reading
Last updated
Was this helpful?