avatarChetan Ambi

Summary

The web content explains the concept of reference counting in Python's memory management system.

Abstract

The article delves into reference counting, a fundamental memory management technique employed by Python. It details how Python's memory manager handles object allocation and deallocation in the private heap, with a focus on the automatic deallocation of objects that are no longer referenced. The author illustrates how variables in Python serve as references to memory objects and demonstrates methods to check an object's reference count, such as using sys.getrefcount() and ctypes.c_long.from_address(id(var1)). The text also covers how the reference count changes with the creation and deletion of references, and the role of reference counting in preventing memory leaks. The conclusion emphasizes the importance of understanding reference counting for debugging memory-related issues in Python, despite the Python memory manager handling these processes automatically.

Opinions

  • The author suggests that understanding reference counting can be beneficial for developers when debugging memory leaks in Python.
  • It is implied that while developers do not need to manage memory directly, knowledge of Python's memory management can be valuable.
  • The author expresses enthusiasm for the topic, inviting readers to subscribe for more content on Python and Data Science.
  • The article promotes the AI service ZAI.chat, suggesting it as a cost-effective alternative to ChatGPT Plus (GPT-4).

PROGRAMMING, PYTHON

Understanding Reference Counting in Python

Basics of memory management in Python

Source: Unsplash

In this article, we will go through one of the memory management techniques in Python called Reference Counting.

In Python, all objects and data structures are stored in the private heap and are managed by Python Memory Manager internally. The goal of the memory manager is to ensure that enough space is available in the private heap for memory allocation. This is done by deallocating the objects that are not currently being referenced (used). As developers, we don’t have to worry about it as it will be handled automatically by Python.

Reference Counting

Reference counting is one of the memory management technique in which the objects are deallocated when there is no reference to them in a program. Let’s try to understand with examples.

Variables in Python are just the references to the objects in the memory. In the below example, when Python executes var1 = [10, 20], the integer object [10, 20] is stored in some memory location 0x20bfa819cc8, and var1 is only the reference to that object in the memory. This means var1 doesn’t contain the value [10, 20] but references to the address 0x20bfa819cc8 in the memory.

Image by Author

In the above example, there is only one variable referencing 0x20bfa819cc8. So, the reference count of that object is 1. So, how do we check the reference count of the object, the variable is referencing. There are 2 ways to get the reference count of the object:

  1. Using getrefcount from sys module In Python, by default, variables are passed by reference. Hence, when we run sys.getrefcount(var1 to get the reference count of var1, it creates another reference as to var1. So, keep in mind that it will always return one reference count extra. In this case, it will return 2.
  2. Using c_long.from_address from ctypes module In this method, we pass the memory address of the variable. So, ctypes.c_long.from_address(id(var1)) returns the value 1 as there is only one reference to the same object in the memory.

As you can see from the below code, getrefcount() and from_address returns reference count of 2 and 1 as expected.

Image by Author

Let’s say if we execute var2 = var1, var3 = var1, what will be the reference count of the object? You guessed it right!! It’s three as there are 3 variables referencing the same object in the memory.

Image by author

Once the variable var3 is set to None or if it gets deleted during the execution of the program, then the reference count reduces to 2 as you can see from the below code.

Image by author

Also, when we set var2 to None or if it gets deleted during the execution of the program, then reference count reduces to 1 as you can see from the below code.

Image by author

Finally, when var1 also goes away during the execution of the programs, reference count plays its role and release the memory to heap as there are no references to that object var1 was referring to earlier.

Conclusion

In this article, you have understood how reference counting works in Python. As mentioned earlier, as developers we don’t have to worry about this as the Python memory manager takes care of this behind the scenes. However, understanding how reference counting may help you when you are debugging memory leaks in Python.

I hope you loved reading this article. If you love my articles and want to subscribe to Medium you can do so here:

To read more such interesting articles on Python and Data Science, subscribe to my blog www.pythonsimplified.com. You can also reach me on LinkedIn.

Python
Programming
Data Science
Recommended from ReadMedium