Find memory size of an object or a variable - Python

 

 What is memory ?

something remembered from the past.

Why memory is important ?

Computer uses memory for storage of data. After processing some operation on processor the result  is stored in the memory. Memory is very important like a processor. because if we store a complex result in memory we don't need to calculate it again if it is needed in future. we can save time of processor and use it for other operations. so, we must use memory effectively.

How to find object memory in python ?

In [1]: import sys
In [2]: s = "Hello World"
In [3]: sys.getsizeof(s)
Out[3]: 48
result is in terms of bytes

Python Data Types And Memory Usage

In [1]: import sys

In [2]: import decimal

In [3]: d = {
...: "int": 0,
...: "float": 0.0,
...: "dict": dict(),
...: "set": set(),
...: "tuple": tuple(),
...: "list": list(),
...: "str": "a",
...: "unicode": u"a",
...: "decimal": decimal.Decimal(0),
...: "object": object(),
...: }
...: for k, v in sorted(d.iteritems()):
...: print k, ">>>>", sys.getsizeof(v)
...:
decimal >>>> 80
dict >>>> 280
float >>>> 24
int >>>> 24
list >>>> 72
object >>>> 16
set >>>> 232
str >>>> 38
tuple >>>> 56
unicode >>>> 56

Python is dynamic programming language. So, memory size of a variable always varies with system to system.

Buy a product to Support me