Reference Counting and Generational Garbage Collection

Seyma Tas

You are on the proper position when you’ve got those questions whilst studying Python:

  • How is reminiscence controlled in Python?

Let’s see if I will be able to resolution those questions and a few extra on this article:

I’m beginning with the basics.

Python Is a Dynamically Typed Language.

We don’t claim the kind of a variable after we assign a price to the variable in Python. It states the type of variable within the runtime of this system. Other languages like C, C++, Java, and so forth.., there’s a strict declaration of variables sooner than assigning values to them.

As you’ll see beneath, we simply assign a variable to an object and Python detects the kind of the thing.

Python detects the kind of an object dynamically. Image by creator made with Canva

How are Python items saved in reminiscence?

In C, C++, and Java we’ve got variables and items. Python has names, now not variables. A Python object is saved in reminiscence with names and references. A reputation is only a label for an object, so one object could have many names. A reference is a reputation(pointer) that refers to an object.

Every Python object has Three issues.

Python items have 3 issues: Type, worth, and reference depend. When we assign a reputation to a variable, its kind is robotically detected by Python as we discussed above. Value is said whilst defining the thing. Reference depend is the selection of names pointing that object.

Every Python object has 3 issues. Image by creator made with Canva

Garbage assortment is to unencumber reminiscence when the thing is now not in use. This gadget destroys the unused object and reuses its reminiscence slot for brand spanking new items. You can consider this as a recycling gadget in computer systems.

Python has an automatic rubbish assortment. It has an set of rules to deallocate items which can be now not wanted. Python has two techniques to delete the unused items from the reminiscence.

The references are at all times counted and saved in reminiscence.

In the instance beneath, we assign c to 50. Even if we assign a brand new variable, the thing is identical, the reference depend will increase by 1! Because each and every object has its personal ID, we print the IDs of items to look if they’re the similar or other.

Image by creator made with Canva

When we alter the price of a like in beneath, we create a brand new object. Now, a issues to 60, b and c level to 50.

When we alter a to None, we create a none object. Now the former integer object has no reference, it’s deleted by the rubbish assortment.

We assign b to a boolean object. The earlier integer object isn’t deleted as it nonetheless has a reference by c.

Image by creator made with Canva

Now we delete c. We lower the reference depend to c by one.

Image by creator made with Canva

As you’ll see above, del() remark doesn’t delete items, it gets rid of the title (and reference) to the thing. When the reference depend is 0, the thing is deleted from the gadget by the rubbish assortment.

Goods and bads of reference counting:

There are benefits and drawbacks of rubbish assortment by reference counting. For instance, it’s simple to put into effect. Programmers don’t have to fret about deleting items when they’re now not used. However, this reminiscence control is dangerous for reminiscence itself! The set of rules at all times counts the reference numbers to the items and shops the reference counts within the reminiscence to stay the reminiscence blank and ensure the techniques run successfully.

Everything appears to be like good enough till now, however …

There is an issue!

The maximum essential factor in reference counting rubbish assortment is that it doesn’t paintings in cyclical references.

What is a cyclical reference or reference cycle?

It is a scenario wherein an object refers to itself. The most simple cyclical reference is appending a listing to itself.

The most simple cyclical reference. Image by creator made with Canva

Reference counting on my own cannot wreck items with cyclic references. If the reference depend isn’t 0, the thing can’t be deleted.

The option to this downside is the second one rubbish assortment manner.

2. Generational Garbage Collection:

Generational rubbish assortment is one of those trace-based rubbish assortment. It can smash cyclic references and delete the unused items although they’re referred by themselves.

How generational Garbage Collection works?

Python helps to keep observe of each and every object in reminiscence. Three lists are created when a program is administered. Generation 0, 1, and a pair of lists.

Newly created items are put within the Generation Zero listing. A listing is created for items to discard. Reference cycles are detected. If an object has no out of doors references it’s discarded. The items who survived after this procedure are put within the Generation 1 listing. The similar steps are implemented to the Generation 1 listing. Survivals from the Generation 1 listing are put within the Generation 2 listing. The items within the Generation 2 listing keep there till the tip of this system execution.

Generational rubbish assortment. Image by creator made with Canva

Python is a high-level language and we don’t need to do the reminiscence control manually. Python rubbish assortment set of rules may be very helpful to open up area within the reminiscence. Garbage assortment is applied in Python in two techniques: reference counting and generational. When the reference depend of an object reaches 0, reference counting rubbish assortment set of rules cleans up the thing instantly. If you’ve a cycle, reference depend doesn’t achieve 0, you look ahead to the generational rubbish assortment set of rules to run and blank the thing. While a programmer doesn’t need to take into consideration rubbish assortment in Python, it may be helpful to grasp what is occurring underneath the hood.

Hope I may resolution the questions at first of the thing. For the questions you can’t to find solutions to:

Further studying:

  1. Mutable and immutable items in Python.

Here is an unpopular however nice article in Medium

2. How to grasp variables in Python.

Here is a fascinating article about tuples.

3. Local and international namespaces.

Here is an in depth clarification from

4. Tracing rubbish assortment.

Here is the Wikipedia hyperlink to “trace-based algorithms”.

5. Stack and heap reminiscence.

Here is a proof to “how variables are saved” from geekforgeeks.

Any ideas to shall be very liked!


Please enter your comment!
Please enter your name here