1

I always thought numpy array is more compact and takes less memory size compare to list, however, for a 3-D float64 np array,

print (sys.getsizeof(result2)/1024./1024./1024.)
print (sys.getsizeof(result2.astype('float16'))/1024./1024./1024.)
print (sys.getsizeof(list(result2))/1024./1024./1024.)
print (sys.getsizeof(result2.tolist())/1024./1024./1024.)

The output is,

0.6521792411804199
0.16304489970207214
0.00033977627754211426
0.0003019943833351135

The list took much smaller memory. Is it validate to use sys.getsizeof for list? If it is, can I do anything to improve np array memory use?

######################

Using pympler @J_H, (it seems pympler can't deal with arraies in a list, like list(a 3-D array ). )

print (result2.nbytes/1024./1024./1024.)
print (asizeof.asizeof(result2)/1024./1024./1024.)
print (result2.astype('float16').nbytes/1024./1024./1024.)
print (asizeof.asizeof(list(result2))/1024./1024./1024.)
print (asizeof.asizeof(result2.tolist())/1024./1024./1024.)

0.6521791219711304
0.6521792411804199
0.1630447804927826
0.004566863179206848
4.078836984932423

Thank you all!

3
  • 2
    getsizeof is non-recursive, i.e. it will not account for the conent of a container. Commented Jul 26, 2020 at 1:16
  • See stackoverflow.com/questions/5022725/… Commented Jul 26, 2020 at 1:21
  • 1
    The memory use of a ndarray` is easy - arr.nbytes, or product of shape and bytes per element. Memory use of a list is hard to estimate. Commented Jul 26, 2020 at 1:21

1 Answer 1

1

You show that each of your list elements consumes 8 bytes.

But each element is just a pointer to a 24-byte float object.

Additionally, when you start with a 3-D array, you'll be looking at lists within lists. You could recurse through the data structures yourself to accurately add up the allocated bytes. Or you could use pympler.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.