pycharm 中的 MemoryError,但应该还有足够的内存

MemoryError in pycharm while there should be plenty memory left

我有一个 sqlite 文件,其中 table 接近 600 万行。 对于某个脚本,我需要立即阅读整个 table。 当使用查询 select 整个 table 我得到一个 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

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