Memory Management in Python
Memory Management in Python
if id(x) == id(y):
print("x and y are pointing to the same memory location")
else:
print("x and y does NOT point to the same memory location")
Output
x and y are pointing to the same memory location
It is also important to note that Python also supports manual garbage collection.
There are two types of manual garbage collection:
Time-based Garbage Collection: This is a type of manual garbage
collection where the garbage collector executes itself after a fixed interval
of time.
Event-Based Garbage Collection: In this type of garbage collection, we
specify a particular event whose occurs triggers the garbage collector. For
instance, when you exit an application, the space occupied by the same
needs to be cleared up for further use by other objects.
Concept of Reference Counting:
Reference counting refers to the concept of counting the number of times
an object is referenced by other objects in the system. This count gets
altered each time a new reference to the object is added or removed.
These objects get deallocated from the memory once the reference count
reaches zero.
For instance, consider a variable(say x), with the value (say,100). Now
the Python VM will create an object for x with the value 100 in the
private heap.
x = 100
Now let’s create another variable(say, y), with the value same as the
variable x.
x= 100
y= x
Now doing this will make the variable y point to the original value stored
in the private heap rather than creating another object of y in the private
heap with the value 100. Just to make sure that both x and y are referring
to the same object run the below code:
x = 100
y=x
if id(x) == id(y):
print("x and y are pointing to the same object")
else:
print("x and y does NOT point to the same object")
Output:
x and y are pointing to the same object
Now that we have established that both x and y are referring to the same object
in the heap, let’s change the value of x and observer the behavior of the same
code above.
x = 100
y=x
if id(x) == id(y):
print("x and y are pointing to the same object")
else:
print("x and y does NOT point to the same object")
Output:
x and y does NOT point to the same object
This behavior is because the variables x and y are not referencing the same
object as x got changed. So now x creates a new reference object and y will still
be referencing the original value of x ie, 100.
Static memory allocation happens at the compile time. we declare a static array
with the fixed sizes. Memory is allocated at the time of compilation. However,
we cannot use the memory again in the further program.
1. static int a=10;
Stack Allocation
The Stack data structure is used to store the static memory. It is only needed
inside the particular function or method call. The function is added in program's
call stack whenever we call it. Variable assignment inside the function is
temporarily stored in the function call stack; the function returns the value, and
the call stack moves to the text task. The compiler handles all these processes,
def func():