The objsize Python package allows for the exploration and
measurement of an object’s complete memory usage in bytes, including its
child objects. This process, often referred to as deep size calculation,
is achieved through Python’s internal Garbage Collection (GC) mechanism.
The objsize package is designed to ignore shared objects, such as
None, types, modules, classes, functions, and lambdas, because they
are shared across many instances. One of the key performance features of
objsize is that it avoids recursive calls, ensuring a faster and
safer execution.
Key Features
Traverse objects’ subtree
Calculates the size of objects, including nested objects (deep size), in bytes
Exclude non-exclusive objects
Exclude specified objects subtree
Provides flexibility by allowing users to define custom handlers for:
Some objects, however, as illustrated in the above example, have unique
functions not shared by other objects. Due to this, it may be useful to
count their sizes. You can achieve this by providing an alternative
filter function.
Some objects handle their data in a way that prevents Python’s GC from
detecting it. The user can supply a special way to calculate the actual
size of these objects.
The above approach may neglect the object’s internal structure. The user
can help objsize to find the object’s hidden storage by supplying it
with its own referent and filter functions:
import objsize
import gc
import torch
def get_referents_torch(*objs):
# Yield all native referents
yield from gc.get_referents(*objs)
for o in objs:
# If the object is a torch tensor, then also yield its storage
if type(o) == torch.Tensor:
yield o.untyped_storage()
# `torch.dtype` is a common object like Python's types.
MySharedObjects = (*objsize.SHARED_OBJECT_OR_FUNCTION_TYPE, torch.dtype)
def filter_func(o):
return not objsize.safe_is_instance(o, MySharedObjects)
To mitigate this, you can provide a method that attempts to fetch the
proxy’s referents:
import weakref
import gc
def get_weakref_referents(*objs):
yield from gc.get_referents(*objs)
for o in objs:
if type(o) in weakref.ProxyTypes:
try:
yield o.__repr__.__self__
except ReferenceError:
pass
Pympler also supports
determining an object deep size via pympler.asizeof(). There are two
main differences between objsize and pympler.
objsize has additional features:
Traversing the object subtree: iterating all the object’s
descendants one by one.
Excluding non-exclusive objects. That is, objects that are also
referenced from somewhere else in the program. This is true for
calculating the object’s deep size and for traversing its
descendants.
objsize has a simple and robust implementation with significantly
fewer lines of code, compared to pympler. The Pympler
implementation uses recursion, and thus have to use a maximal depth
argument to avoid reaching Python’s max depth. objsize, however,
uses BFS which is more efficient and simple to follow. Moreover, the
Pympler implementation carefully takes care of any object type.
objsize archives the same goal with a simple and generic
implementation, which has fewer lines of code.
License: BSD-3
Copyright (c) 2006-2026, Liran Funaro.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.