Python list memory error

后端 未结 2 557
小蘑菇
小蘑菇 2021-01-27 05:02

I am trying to make a list with 2 raised to 30 elements but am getting a memory error. Why so? Is it outside the maximum limit of a list in python?

m=[None]*(2**         


        
2条回答
  •  天涯浪人
    2021-01-27 05:40

    You are trying to create a huge list here and you do not have enough free RAM to do this. How much RAM will you need?

    It will be approximately 8Gb on my machine. You can find the size of None on your machine:

    import sys
    sys.getsizeof(None) # 16 here
    

    But for a list you can approximate it this way:

    sys.getsizeof([None] * (2**20)) # 8388680
    

    and hoping that for 2**30 it will take approximately 2**10 times more you will end up with 8388680 * 2**10 which is approximately 8Gb.

提交回复
热议问题