Python manages memory using a combination of techniques, including:
1. Garbage Collection: Python’s memory management also involves a built-in garbage collector, which recycles all the unused memory for the program so that it can be available for other resources. This means that Python will automatically recycle the memory once it is no longer in use.
1. Reference Counting: Python utilizes a technique called reference counting. This means that Python keeps track of the number of references to each object in your code. When an object’s reference count drops to zero, Python knows it is no longer in use and the memory can be freed.
1. Memory Pool: Python uses a system of memory management known as “pools” for small objects. The Python memory manager has different pools for different object sizes. The object sizes that can be allocated from memory pools are 1 to 256 bytes.
1. Caching & Interning: Python tries to use existing immutable objects before creating a new object with the same value. This process is known as interning. Similarly, Python caches integer objects between -5 and 256, so every time we are using integer within this range Python uses the cached version.