Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find number of bytes taken by python variable

Tags:

python

Is there anyway i can know how much bytes taken by particular variable in python. E.g; lets say i have

int = 12 print (type(int)) 

it will print

<class 'int'>  

But i wanted to know how many bytes it has taken on memory? is it possible?

like image 247
itsaboutcode Avatar asked Nov 24 '09 11:11

itsaboutcode


1 Answers

You can find the functionality you are looking for here (in sys.getsizeof - Python 2.6 and up).

Also: don't shadow the int builtin!

import sys myint = 12 print(sys.getsizeof(myint)) 
like image 103
ChristopheD Avatar answered Oct 10 '22 16:10

ChristopheD