How can I see the size (i.e. memory usage) of all objects currently in memory?
Running the following does not provide correct figures for memory usage
[(m, sys.getsizeof(m)) for m in dir() if not m.startswith('__')]
# Returns
# ('ElasticNet', 59),
# ('ElasticNetCV', 61),
# ('In', 51),
# ...
Whereas for example sys.getsizeof(ElasticNet)
shows me that ElasticNet
has a size of 1064
.
Additionally, is there a convenient tool for assessing which objects are taking up large amounts of RAM, so as to del
ete and garbage collect them during the script or session?
NB Total memory used by Python process points out how to profile memory at the level of the Python process, whereas I want to determine memory usage for all objects separately, and moreover do so conveniently by retrieving size of the object that m
(inside the list comprehension) is referencing.