How is memory managed in Python?

Can anyone explain how memory managed in python

Memory management in Python depends on the interpreter.

In Jython, memory is managed by the JVM.

In IronPython, memory is managed by the .Net runtime.

In CPython, the garbage collector is a “reference counter”: every time
you refer to an object, the reference count increases by one. When you
free that reference, the count drops by one. When the count drops to
zero, then the object is no longer in use, and the garbage collector
immediately frees it.

CPython has some caches for frequently used objects so that they never
get freed, so that the cached object can be used instead of creating a
new one. Small ints like 1, 2, 3 are usually found in the cache.

CPython also has a second garbage collector that runs to detect and
clear cycles: if object “A” refers to object “B”, and “B” refers to “A”,
then there is a reference cycle and the reference counter will never
free them. That would lead to a memory leak, except that CPython
contains a second garbage collector that can detect cycles and clear

You can read more about garbage collection here:

but remember that other Python interpreters will use completely
different garbage collectors.

More about CPython’s memory management here:

The low-level C interface is described here:

This is only needed if you are writing extensions in C. And remember
that other Python interpreters, like PyPy, Stackless, IronPython and
Jython will use different memory management.