Basics of Memory Management in Python

Python - like many other languages (Java, c# etc.) uses Garbage Collection rather than manual memory management. Python memory is managed by Python private heap space . All Python objects and data structures are located in a private heap. The developer does not have an access to this private heap and interpreter takes care of this Python private heap.
How does Memory Allocation work in Python

Python Memory Manager

The Python memory manager manages Python's memory allocations. The allocation of Python heap space for Python objects is done by Python memory manager. You just freely create objects and the Python's memory manager periodically (or when you specifically direct it to) looks for any objects that are no longer referenced by your program. The Core API gives access to some tools for the programmer to code. Python also have an inbuilt garbage collector , which recycle all the unused memory and frees the memory and makes it available to the heap space . So if you want to hold on to an object, just hold a reference to it. If you want the object to be freed (eventually) remove any references to it.
def getDays(days): for day in days: print day getDays(["Sunday", "Monday", "Tuesday"]) getDays(["Wednesday", "Thursday", "Friday"])
Each of these calls to getDays creates a Python list object initialized with three values. For the duration of the getDays call they are referenced by the variable days, but as soon as that function exits no variable is holding a reference to them and they are fair game for the garbage collector to delete.
How does Python manage memory?

Python Memory Allocation

Memory reclamation is mostly handled by Reference Counting . That is, the Python Virtual Machine keeps an internal journal of how many references refer to an object, and automatically garbage collects it when there are no more references referring to it. Though python uses 'Reference count' and 'Garbage Collector' to free memory (for the objects that are not in used), this free memory is not returned back to the operating system (in windows its different case though). This mean free memory chunk just return back to python interpreter and not to the Operating System. So ultimately your python process is going to hold the same memory. However, Python will use this memory allocate to some other objects.