This article is going to be a short read, we will focus on how memory is managed in Python for objects like list and tuple and what can be the key takeaways. Let's get started! ListPython list as we all know is a mutable dynamic array that behaves like a linked list as well. Let's look at some code for creating and updating a list: # ./list_1.py l = [1,2,3] # A new list gets created print('Initial List', l) l.append(4) # Updating list using in-built append method print('Append 4, List updated to', l) l.pop() # Pop last item print('Pop, List updated to', l) l += [5, 6] # Updating list by adding a new list to given list print('Add new list, List updated to', l) Following will be the output: Initial List [1, 2, 3] Append 4, List updated to [1, 2, 3, 4] Pop, List updated to [1, 2, 3] Add new list, List updated to [1, 2, 3, 5, 6] >>> Now let's see what happens in memory when we run the above script, here we will be using sys to help us out on viewing system-related information: # ./list_2.py import sys l = [1,2,3] # A new list gets created print('Initial List', l) print('Initial List Address', id(l)) print('Initial List Memory', sys.getsizeof(l)) l.append(4) # Updating list using in-built append method print('Append 4, List updated to', l) print('Append 4, List Address', id(l)) print('Append 4, List Memory', sys.getsizeof(l)) l.pop() # Pop last item print('Pop, List updated to', l) print('Pop, List Address', id(l)) print('Pop, List Memory', sys.getsizeof(l)) l += [5, 6] # Updating list by adding a new list to given list print('Add new list, List updated to', l) print('Add new list, List Address', id(l)) print('Add new list, List Memory', sys.getsizeof(l)) Let's look at the output first: Initial List [1, 2, 3] Initial List Address 139688582615624 Initial List Memory 88 Append 4, List updated to [1, 2, 3, 4] Append 4, List Address 139688582615624 Append 4, List Memory 120 Pop, List updated to [1, 2, 3] Pop, List Address 139688582615624 Pop, List Memory 120 Add new list, List updated to [1, 2, 3, 5, 6] Add new list, List Address 139688582615624 Add new list, List Memory 120 >>> Following points we can find out after looking at the output:
Now, let's look at the tuples. TuplePython tuple as we all know are immutable and do not extend in the memory further after the initial declaration. Now, let's look at the above same scenario but for tuples: # ./tuple_1.py import sys t = (1,2,3) # A new tuple gets created print('Initial Tuple', t) print('Initial Tuple Address', id(t)) print('Initial Tuple Memory', sys.getsizeof(t)) t += (5, 6,) # Updating tuple by adding a new tuple to given tuple print('Add new tuple, Tuple updated to', t) print('Add new tuple, Tuple Address', id(t)) print('Add new tuple, Tuple Memory', sys.getsizeof(t)) Output: Initial Tuple (1, 2, 3) Initial Tuple Address 140452527817280 Initial Tuple Memory 72 Add new tuple, Tuple updated to (1, 2, 3, 5, 6) Add new tuple, Tuple Address 140452528572728 Add new tuple, Tuple Memory 88 >>> Following points we notice after looking at the output:
Let's look at some miscellaneous points. MISCWhat happens when we create an empty list and empty tuple? # ./misc.py import sys t1 = () t2 = () l1 = [] l2 = [] print('List l1 address', id(l1)) print('List l2 address', id(l2)) print('List l1 memory', sys.getsizeof(l1)) print('List l2 memory', sys.getsizeof(l2)) print('List t1 address', id(t1)) print('List t2 address', id(t2)) print('List t1 memory', sys.getsizeof(t1)) print('List t2 memory', sys.getsizeof(t2)) What did we get? List l1 address 139766851756616 List l2 address 139766851761800 List l1 memory 64 List l2 memory 64 List t1 address 139766871547976 List t2 address 139766871547976 List t1 memory 48 List t2 memory 48 >>> What did we find?
Well, that's it from me. We explored quite a few points on how memory is allocated and how it could be managed in Python. To know more about memory management, I encourage you to explore their code available on python's repo. Feeling it's difficult to go through the source code of python? Don't worry, we can always use available sys module methods and other functions like id to develop an understanding of what could be happening behind the scenes.
Till next time! Namaste 🙏
|