Python字典大小的最大限制 [英] Python Dictionary Size Maximum Limit

查看:3093
本文介绍了Python字典大小的最大限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解以下MemoryError的原因. python中的字典有一些预定义的限制吗?

I am trying to understand the reason for the following MemoryError. Is there some pre-defined limit on dictionaries in python?

self.text是从文件中读取的长字符串(约4.5 MB) L等于4641652

self.text is long string read in from a file (about 4.5 MB) L is equal to 4641652

    L = len(self.text) 
    test = {}
    for i in xrange(L,0,-1):
        try:
            test[i] = self.text[i-1:]
        except MemoryError:
            print "Memory Error at the " + str(i) +"th iteration!"
            print sys.getsizeof(test)
            print len(test)
            exit()

输出

Memory Error at the 4577890th iteration!
1573004
63762

如果有帮助,我正在Windows计算机上运行该程序,内存为16gb.

I am running the program on a windows machine with 16gb of ram if that helps.

推荐答案

您正在循环中存储1 + 2 + 3 + ... + 4641650 + 4641651 + 4641652 ...字节.通过所讨论的迭代,您已经遍历了63762次,即2032796322字节.再来看一遍doubline和lo,请注意,您已经超过了32位整数限制,这对我来说似乎是发生内存错误的合理位置.

You are storing 1 + 2 + 3 + ... + 4641650 + 4641651 + 4641652... bytes in your loop. By the iteration in question, you have gone around 63762 times, which is 2032796322 bytes. One more doubline and lo and behold, you are over the 32 bit integer limit, which seems like a reasonable place to run into a memory error to me.

这篇关于Python字典大小的最大限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆