- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Memory Management in Python
Writing a memory-efficient and a code that executes quickly is what every developer wants while working on any programming language. In Python, memory allocation and deallocation is not manual, since Python has a Garbage Collector.
Now, what is a Garbage Collector.
Garbage Collection is how the memory is freed when not use and how it can be made available for other objects. Python deletes the objects that are no longer in use. This is what we call Garbage Collection. The garbage collector initiates its execution with the program and is activated if the reference count drops to zero.
Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero. An object's reference count changes as the number of aliases that point to it changes.
Let us now see how memory is allocated in Python −
Static Memory Allocation – Stack
In static memory allocation, the memory is allocated at the compile time. The Stack data structure stores the static memory.
A quick example where the memory allocates on stack −
static int x=2;
Dynamic Memory Allocation – Heap
In dynamic memory allocation, the memory is allocated at the run time. The Heap stores the dynamic memory. It frees up the memory space if the object is no longer needed.
A quick example where the memory allocates on heap for 2 integers −
x = *2
As we discussed above, the garbage collector initiates its execution with the program and is activated if the reference count drops to zero. Let’s see what is a reference count.
The Python garbage collector initiates its execution with the program and is activated if the reference count drops to zero. Let us see when the reference count increase or decreases
The reference count value increase when −
When a new name is assigned or in a dictionary or tuple, the reference count increases its value.
If we reassign the reference to an object, the reference counts decrease its value.
The reference count value decreases when −
- The value decreases when the object's reference goes out of scope.
- The value decreases when an object is deleted.
Therefore, reference counting is actually how many times other objects reference an object. With that, The de-allocation occurs when the reference count drops to zero.
- Related Articles
- Memory Management
- Memory management in Java
- Global memory management in C++ : Stack or Heap?
- C++ Program for Best Fit algorithm in Memory Management
- C++ Program for First Fit algorithm in Memory Management
- What are the changes in Memory Management in Java 9?
- Major Activities of an Operating System with Regard to Memory Management
- What are the basic components of the memory management unit in computer architecture?
- Memory-mapped file support in Python (mmap)?
- How to optimize Python dictionary memory usage?
- Can you please explain Python dictionary memory usage?
- Risk Management Strategies in Project Management
- Difference between Virtual memory and Cache memory
- Difference between Cache Memory and Virtual Memory