Hanfei Sun
Hanfei Sun

Reputation: 47071

Is there a way to view how much memory a SciPy matrix used?

I know in python it's hard to see the memory usage of an object.

Is it easier to do this for SciPy objects (for example, sparse matrix)?

Upvotes: 3

Views: 171

Answers (2)

tiago
tiago

Reputation: 23502

If you are inside IPython, you can also use its %whosmagic function, which gives you information about the session's variables and includes how much RAM each takes.

Upvotes: 0

EnricoGiampieri
EnricoGiampieri

Reputation: 6095

you can use array.itemsize (size of the contained type in bytes) and array.flat to obtain the lenght:

# a is your array
bytes = a.itemsize * a.size

it's not the exact value, as it ignore the whole array infrastructure, but for big array it's the value that matter (and I guess that you care because you have something big)

if you want to use it on a sparse array you have to modify it, as the sparse doesn't have the itemsize attribute. You have to access the dtype and get the itemsize from it:

bytes = a.dtype.itemsize * a.size

In general I don't think it's easy to evaluate the real memory occupied by a python object...the numpy array is an exception being just a thin layer over a C array

Upvotes: 5

Related Questions