2. Memory management
Memory management is the process of
efficiently allocating, de-allocating, and
coordinating memory so that all the
different processes run smoothly and
can optimally access different system
resources.
Memory management also involves
cleaning memory of objects that are no
longer being accessed.
3. In Python, the memory manager is responsible for these
kinds of tasks by periodically running to clean up, allocate,
and manage the memory.
Unlike C, Java, and other programming languages, Python
manages objects by using reference counting.
This means that the memory manager keeps track of the
number of references to each object in the program.
When an object's reference count drops to zero, which means
the object is no longer being used, the garbage collector (part
of the memory manager) automatically frees the memory
from that particular object.
The user need not to worry about memory management as
the process of allocation and de-allocation of memory is
fully automatic. The reclaimed memory can be used by other
objects.
4. Python Objects in Memory
Each variable in Python acts as an
object. Objects can either be simple
(containing numbers, strings, etc.) or
containers (dictionaries, lists, or user
defined classes).
Furthermore, Python is a dynamically
typed language which means that we do
not need to declare the variables or their
types before using them in a program.
5. If you look at the first 2 lines of the above program, object x is
known. When we delete the object x and try to use it, we get
an error stating that the variable x is not defined.
7. What is a reference?
A name or a container object pointing at
another object.
What is a reference count?
Reference counting is a simple technique
in which objects are allocated when there is
reference to them in a program
9. What does del do?
The del statement doesn’t delete objects. It:
• removes that name as a reference to that
object
• reduces the ref count by 1
X = 300
Y = 300
del X
X
Y
300
References: 1
11. What is Garbage
Collection?
A way for a program to automatically release
memory when the object taking up that space is no
longer in use.
The Python Garbage Collector (GC) runs during the
program execution and is triggered if the reference
count reduces to zero.
The memory is a heap that contains objects and
other data structures used in the program. The
allocation and de-allocation of this heap space is
controlled by the Python Memory manager through
the use of API functions.
Two main types of garbage collection:
1. Reference counting
2. Tracing