【问题标题】:Python Dictionary Size Maximum LimitPython 字典大小最大限制
【发布时间】:2015-03-06 01:48:27
【问题描述】:

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

self.text 是从文件中读取的长字符串(大约 4.5 MB) L 等于 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

如果有帮助,我将在具有 16gb 内存的 Windows 机器上运行该程序。

【问题讨论】:

  • 哇,快回复@Marcin。我正在编辑我的问题以提供该答案。
  • 是的。通常人们会在问题出现的那一刻阅读问题。所以它很快:-)

标签: python dictionary memory-leaks


【解决方案1】:

您正在循环中存储 1 + 2 + 3 + ... + 4641650 + 4641651 + 4641652... 字节。通过有问题的迭代,您已经走了大约 63762 次,即 2032796322 字节。再看一次,你瞧,你已经超过了 32 位整数限制,这对我来说似乎是一个合理的地方遇到内存错误。

【讨论】:

  • 在我看来也很有道理
猜你喜欢
  • 2014-06-25
  • 2016-05-19
  • 2011-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
相关资源
最近更新 更多