Suppose you do the following:
dom = ElementTree()
dom.parse(some_file_path)
I’d like to log the rough amount of memory that this dom is now using in my process. I don’t need something precise, something rough would do.
I don’t think I can derive it from the size of the source XML file. I have a 500 kilobyte file that seems to add about 5MB to the memory usage of my python process after it’s loaded as in the example above.
I looked over the ElementTree API and didn’t see any API to provide this information. Anyone know of a way to know how much memory the ElementTree instance is using after parsing/loading an XML file?
Essentially you want to find memory consumption of a particular python object. That’s what it is. Here its an
ElementTreeobject but it can be anything.To cut the chase short as far as I know There’s no easy way to find out the memory size of a python object. One of the problems you may find is that Python objects – like lists and dicts – may have references to other python objects (in this case, what would your size be? The size containing the size of each object or not?). There are some pointers overhead and internal structures related to object types and garbage collection. Finally, some python objects have non-obvious behaviors. For instance, lists reserve space for more objects than they have, most of the time; dicts are even more complicated since they can operate in different ways (they have a different implementation for small number of keys and sometimes they over allocate entries).