What About Single Reference
And Multiple Objects?
Before reading these notes first
you have to see Lecture-14 video. So that you can easily understand what is
written below.
So from 2 lectures, We are regularly
discussing that if we have a single reference and what if we create multiple
objects.
So let’s discuss it with an
example
a=10
a=20
a=30
So when the above 3 lines will run
then Python creates 3 objects one by one and the variable a starts pointing 30
So let’s understand with a diagram
This arises a question that what
will happen with the other objects whose values are 20 and 30
So before understanding what
will happen to the other objects we have to understand 3 important
terminologies.
1. 1. Reference Counting
2.
2. Garbage Block
3.
3. The Garbage Collector
Let’s understand all the terminologies one by one.
First, we’ll talk about Reference Counting.
So let’s understand it with an example.
a=10
b=10
Let’s understand with a diagram
that by writing these lines what happens in the memory.
So here we can see that a
and b both the references were pointing the same object this means the reference count of the object will be 2.
REFERENCE COUNT – Reference count
is a number which tells the interpreter that the object was pointed by how many
references.
Then we will write b=20
Let’s see the effect
So the address of the object who is pointing 20 is stored in both the references a and b and the object who is pointing 10 whose reference count now becomes 0.
So, The object block whose reference count is zero and no
one is referring to the object this object block is known as the Garbage block.
As we see in the diagram that no one is referring to the
objects which is pointing to 10 this object is now a garbage block.
Until then we saw that what is reference counting and to
whom do we call garbage block.
Now what after this, we already understood what is the reference
counting and garbage block. So what after this?
So Now comes Garbage Collector
The Garbage collector always keeps a round of
memory as every morning the vehicle of Municipal Corporation has come in your
house and collect the garbage from your house, Same work is with your garbage
collector this always keeps a round of memory and if it sees any object which is
not referenced by any reference then it collects that object and clears the
memory. And this is called a Garbage collector in Python.
So as we see in our example that the object whose value is
10 is now not referencing by any reference. So this object is now removed by
Garbage collector and our memory is free now.
So this is the way in Python for memory management.
Now we will talk about their advantages and disadvantages.
So the main advantage of this approach is that the unused
memory is clear now and we programmers are able to re-use the memory.
But the disadvantage of this approach is that our garbage
collector will have to check regularly that any object’s reference count is
zero or not. And this is one of the reasons why Python is slower than
other languages.
I hope that you must have understood this how Python
manages memory and in what way.
Even after that if you have any doubt then you will ask
your questions on the comment section and I will try to reply you as soon as
possible.
DIGITAL
COMMUNITY
We Make IT Happen.
No comments:
Post a Comment
If you have any doubts, let me know.