A memory issue can occur, especially if there are constraints on the total amount of memory available when several objects are active on RAM during program execution. Below is an overview of some methods of reducing object size which can reduce substantially the amount of RAM required in pure Python programmes.

For simplicity, structures in Python are considered to represent points with co-ordinates X, Y, Z by name.

Dict

In small programs, especially in scripts, it is quite simple and convenient to use the built-in dict to represent structural information:

ob = {‘x’:1, ‘y’:2, ‘z’:3}
x = ob[‘x’]
ob[‘y’] = y

With the advent of a more compact implementation in Python 3.6 with an ordered set of keys, dict has become even more appealing. However, let’s look at the size of its footprint in RAM:

print(sys.getsizeof(ob))

240

It takes a lot of memory, especially if you suddenly need to create a large number of instances:

Image for post

#python #coding #python3 #programming

How to Reduce the Size of Memory in Python
2.95 GEEK