1

The sys.getsizeof() function returns the size of an object in memory, in bytes. As this function may return inaccurate results for third-party objects, how do I determine how much memory a BioPython object is using?

I loaded a 286MB file using example code (modified):

from Bio import SeqIO
handle = open("example.gb", "rU")
records = list(SeqIO.parse(handle, "genbank"))
handle.close()

and sys.getsizeof(records) indicates that it is using 268KB.

Community
  • 1
  • 1
SabreWolfy
  • 5,392
  • 11
  • 50
  • 73

1 Answers1

3

sys.getsizeof(obj) returns the size of the object obj itself, not the size of any other object it might reference one way or another:

>>> l = []
>>> sys.getsizeof(l)
72
>>> zero = 0
>>> sys.getsizeof(zero)
24
>>> l.append(zero)
>>> sys.getsizeof(l)
104
>>> 72 + 24
96
>>> l.append(zero)
>>> sys.getsizeof(l)
104
>>> l.append(zero)
>>> sys.getsizeof(l)
104
>>> l.append(zero)
>>> sys.getsizeof(l)
104
>>> l.append(zero)
>>> sys.getsizeof(l)
136
>>> 
>>> class Foo(object):
...     def __init__(self, bar=None):
...         self.bar = bar
... 
>>> f = Foo()
>>> sys.getsizeof(f)
64
>>> f.bar = 1
>>> sys.getsizeof(f)
64
>>> f.bar = l
>>> sys.getsizeof(f)
64
>>> 
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118