aquagremlin
aquagremlin

Reputation: 3549

what is the correct way to use sys.getsizeof to get the size of a database in python?

I have a large leveldb database named, db.

tot=0
i=0
for key,value in db.RangeIter():
    tot=sys.getsizeof(key)+sys.getsizeof(value)
    i=i+1
print(f'total rows:  {i}')
print(f'total size:  {tot}')

gives

total rows:  119113
total size:  143

which is really odd.

So I wrote this:

keyTot=0
valTot=0
for key,value in db.RangeIter():
    keyTot += len(key)
    valTot += len(value)

print(f'Total key size:  {keyTot}  Total value size:    {valTot}    Total:  {keyTot+valTot}')

which gave

Total key size:  1404729  Total value size:    452897532    Total:  454302261

So what was sys.getsizeof measuring when I passed it the key? And how did it come up with such a small number after iterating through 119113 rows?

Upvotes: 0

Views: 43

Answers (1)

donkopotamus
donkopotamus

Reputation: 23206

Do you imagine this is keeping a running total?

tot=sys.getsizeof(key)+sys.getsizeof(value)

It is not, you are overwriting tot on every iteration

Upvotes: 1

Related Questions