Introduction

In the world of programming, it is crucial to have an understanding of how memory is managed, as this can greatly impact the performance of your programs. Memory references in Python is an important and often misunderstood concept, which can make a significant difference in the effective use of memory. Understanding memory references in Python is an essential step in becoming an efficient programmer, especially when dealing with large amounts of data. This tutorial will provide an in-depth understanding of memory references in Python and how they work, explaining key concepts such as reference counting, garbage collection etc.

Table of Contents :

  • What are memory references in python
  • Python References Example
  • find the memory address : id() function
  • Reference counts
  • Example For Reference count
  • Counting references
  • Implementing Reference Counts in Python

What are memory references in python

  • In many programming languages variable is a label given to a value.
  • This is not the case in Python.
  • In Python variables are references.
  • In programming, a reference is a value that points to some other value in the program's memory.
  • In other words, a variable is a reference to an object that holds a value.
  • In Python, everything is an object that has an identity, a type, and a value.
  • References in Python are lightweight and are used extensively to manipulate data and objects.
  • The example below will explain things clearly.

Python References Example :

  • Let us assign a value 10 to a variable:  num = 10 
  • The process of assigning a value to a variable is as follows
    • a new integer object is created in the memory.
    • the  num  variable is bound to that memory address.
  • The process of accessing the value of a variable is as follows
    • Python looks up for the memory location bound to the variable.
    • In that memory location it finds the object referenced by the variable.
    • The value of the object is fetched and returned.
  • In our above example   print(num)     # 10

find the memory address : id() function

  • We can find the memory address of an object referenced by a variable by using the built-in  id()  function.
  • we need to pass the variable as a parameter to the  id()  function.
  • Code Sample : For example, the following returns the memory address of the integer object referenced by the  num  variable

num = 10
print(id(num))

Output:

# memory address of the integer object referenced by the num variable:


  • The  id()  function returns the memory address as a  base-10  number.
  • We can convert this memory address to hexadecimal by using the  hex()  function
  • The  hex()  function returns a string
  • Code Sample : 

counter = 100

print(id(counter))
print(hex(id(counter)))

Output:



Reference counts

  • In Python more than one variables can refer to the same memory address.
  • In this case, the object at that memory location has more than one references at the same time.
  • The number of variables that refer to an object at a time is called the reference count of that object.
  • Every object in memory has a reference count that keeps track of how many references to the object exist in memory.
  • In Python, reference counting is a method of tracking objects in memory.
  • If an object is left with zero references, Python's memory manager destroys that deject to reclaim the memory.
  • Reference counting is fast and efficient, but it cannot handle circular references, where two or more objects reference each other.

Example For Reference count :

Let us take a simple code sample :


# Step 1
num = 10

# Step 2
cnt = num

# Step 3
cnt = 60

# Step 4
num = 20



  • Step 1 : Initially the integer object with the value of 10 has one reference which is the  num  variable.
  • Step 2 : We assign the  num  variable to another variable  cnt 
    • Now, both  num  and  cnt  variables reference the same integer object.
    • In other words the integer object with the value  10  has two references:  num  and  cnt  
  • Step 3 : We assign a different value to the  cnt  variable:  cnt = 60 
    • The integer object with value  10  is left with only one reference, which is the  num  variable
  • Step 4 : We assign a different value to the  num  variable:  num = 20 
    • The integer object with value  10  is left with zero references

Counting references

  • We can get the number of references of an object by using the  from_address()  method
  • The  from_address()  method is found in the   ctypes  module.
  • To count the references of an object we need to pass the memory address of the object as a parameter to    from_ address() 
  • We need to pass the address as an integer number :   ctypes.c_long.from_address(address).value 

Implementing Reference Counts in Python :

  • Reference counting can be implemented manually in Python using the  sys.getrefcount()  function.
  •  sys.getrefcount()  returns the number of references to an object in memory.
  • Although it is possible to implement reference counting manually, it is not recommended as Python already has an efficient reference counting system built-in.
  • Python's garbage collector is designed to handle circular references, where reference counting falls short.
  • The  gc  module in Python provides developers with the ability to control and manage the garbage collection process.
  • While automated garbage collection in Python is efficient, it is still essential to manage resources explicitly to free up memory.
  • Explicit resource management for larger applications can involve using context management,  with  statements, and try...finally blocks to ensure that resources are released when they are no longer needed.

Prev. Tutorial : Writing to csv files

Next Tutorial : Garbage collection