2

I would like to know how much memory is being occupied by a variable.

Let us assume this example:

import numpy as np

t = [np.random.rand(10000) for _ in range(1000)]

How much memory in megabytes is t occupying?

I ran:

sys.getsizeof(array_list)

and I am getting 8856 which seems low for the amount of information contained in t

Best Regards.

9
  • This is a lot harder to pin down in languages like Python than, say, C. Are you counting the overhead of the array? Commented May 17, 2023 at 15:03
  • @tadman should be the size of everything. I am using sys.getsizeof(t) but it is giving me 8856 bytes... which seems too low Commented May 17, 2023 at 15:06
  • That could be the size of the array, excluding the size of the values. In other words, also count the getsizeof(e) for each element e. Commented May 17, 2023 at 15:07
  • Yeah, getsizeof is telling you the size of the list, not its contained objects. Commented May 17, 2023 at 15:07
  • I would like the size of everything. How much memory am I alocating to store that array Commented May 17, 2023 at 15:07

1 Answer 1

3
from pympler import asizeof
asizeof.asizeof([np.random.rand(10000) for _ in range(1000)])

#output
80120872

Link : https://pypi.org/project/Pympler/

pip install Pympler
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried this one. It works the best for list of multidimensional arrays. [Although it is an external library]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.