【问题标题】:MemoryError in pycharm while there should be plenty memory leftpycharm中的MemoryError,而应该有足够的内存
【发布时间】:2020-10-21 10:04:33
【问题描述】:

我有一个包含接近 600 万行的某个表的 sqlite 文件。 对于某个脚本,我需要一次读取整个表格。 当使用查询来选择整个表时,我得到一个 MemoryError。

使用查询加载多达 400 万行即可:

query = "select * from event where NAME NOT IN (%s) limit 4000000" % placeholders
cursor.execute(query, list_excluded_events)

使用:

print('The lenght of the results in bytes = ' + str(sys.getsizeof(results)))

给我结果的大小:17873392 字节或 17 MB。

我有 4GB 内存分配给 pycharm,所以 200 万行相同的行应该没问题。 那么为什么我不断收到内存错误呢?

import sqlite3
import sys


def connection(table, *args):
    conn = sqlite3.connect(
        table)
    cursor = conn.cursor()

    if args != ():
        list_excluded_events = args[0]
          # <- Connect to the database using the variable declared in main

        placeholder = '?'
        placeholders = ', '.join(placeholder for unused in list_excluded_events)


        query = "select * from event where NAME NOT IN (%s) limit 4500000" % placeholders

        cursor.execute(query, list_excluded_events)

    else:
        cursor.execute("select * from event")

    results = cursor.fetchall()
    #print(results)

    results = [list(elem) for elem in results]  # <- Change list of tuples to a list of lists
    print('The lenght of the results in bytes = ' + str(sys.getsizeof(results)))
    return results

【问题讨论】:

  • str(len(str(results)))这怎么等于results的大小?
  • @AbhinavMathur,我假设每个字符等于 1 个字节,因此结果的 len 应该给出字节大小。用于存储字符串本身的任何更多内存都是微不足道的
  • 我使用 sys.getsizeof 编辑了原始帖子,现在结果更小了,所以我原来的问题仍然存在
  • 我猜?您正在做的事情会使您未发布的处理代码中的结果大小加倍。该错误显然不在您发布的代码中,或者您无法打印结果的大小。相关:stackoverflow.com/questions/4285185/upper-memory-limit
  • @JaredSmith 我在帖子中添加了完整的代码。 table 变量只是一个指向文件的字符串,在 args 中是一个最大大小为 6 的列表。

标签: python pycharm out-of-memory


【解决方案1】:

升级到 64 位 python 版本解决了这个问题。

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 2019-11-01
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    相关资源
    最近更新 更多