Python dictionary memory usage

I was working on a project that involves loading a relatively large dictionary into memory from a file. The dictionary has less than 2 million entries, each entry (key and value combined) is less than 20 bytes. The file size on the disk is 38 MB.

My problem is that when I try to load a dictionary, my program immediately expands to 2.5 gigabytes of used memory.

Here is the code I use to read the dictionary from disk:

f = open('someFile.txt', 'r')
rT = eval(f.read())
f.close()
+5
source share
2 answers

I think memory is used to parse the syntax of the word AST.

, cPickle repr/eval.

import cPickle

x = {}
for i in xrange(1000000):
    x["k%i" % i] = "v%i" % i
cPickle.dump(x, open("data", "wb"), -1)

x = cPickle.load(open("data", "rb"))

-1 , , , python. , , /.

+7

, / .

, .

0

All Articles