29

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?

9 Answers 9

62

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))
Sign up to request clarification or add additional context in comments.

7 Comments

is it really giving the size of myint it is returning 14, as far as i know int take 4 bytes, i think its returning the size of whole object, can i just know the size of only "INT"
From the docs: getsizeof calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.
itsaboutcode: Do you want to leave the Python realm? What question do you really want to answer? If you want 4 as the answer, then def sizeof(o): return 4.
And int is an object. There is nothing smaller in the python realm that you can get the size of. If you want the raw machine int, you may have to drop down into C (or Java, or C#, depending on your python).
If you want to store many of int or another C-like datatype, using a minimum of bytes, you can use the array class from the array module.
|
11

if you want to know size of int, you can use struct

>>> import struct
>>> struct.calcsize("i")
4

otherwise, as others already pointed out, use getsizeof (2.6). there is also a recipe you can try.

Comments

6

In Python >= 2.6 you can use sys.getsizeof.

Comments

4

Numpy offers infrastructure to control data size. Here are examples (py3):

import numpy as np
x = np.float32(0)
print(x.nbytes) # 4
a = np.zeros((15, 15), np.int64)
print(a.nbytes) # 15 * 15 * 8 = 1800

This is super helpful when trying to submit data to the graphics card with pyopengl, for example.

1 Comment

This is especially useful if you want to find the underlying representation of a python type, for example: x = float(0.0); print(np.array(x).nbytes);
2

You could also take a look at Pympler, especially its asizeof module, which unlike sys.getsizeof works with Python >=2.2.

Comments

2

on python command prompt, you can use size of function

   $ import python 
    $ import ctypes
    $ ctypes.sizeof(ctypes.c_int)

and read more on it from https://docs.python.org/2/library/ctypes.html

Comments

0

In Python 3 you can use sys.getsizeof().

import sys
myint = 12
print(sys.getsizeof(myint))

Comments

0

The best library for that is guppy:

import guppy
import inspect 

def get_object_size(obj):
    h = guppy.hpy()
    callers_local_vars = inspect.currentframe().f_back.f_locals.items()

    vname = "Constant"

    for var_name, var_val in callers_local_vars:
        if var_val == obj:
            vname = str(var_name)

    size = str("{0:.2f} GB".format(float(h.iso(obj).domisize) / (1024 * 1024)))

   return str("{}: {}".format(vname, size))

Comments

-1

The accepted answer sys.getsizeof is correct.

But looking at your comment about the accepted answer you might want the number of bits a number is occupying in binary. You can use bit_length

(16).bit_length() # '10000' in binary
>> 5

(4).bit_length() # '100' in binary
>> 3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.