I want to have a table of 10 largest objects in memory, with size.
Equivalent function in R: Tricks to manage the available memory in an R session
lsos()
The simplest is to use Pympler:
from operator import itemgetter
from pympler import tracker
mem = tracker.SummaryTracker()
print(sorted(mem.create_summary(), reverse=True, key=itemgetter(2))[:10])
Output:
[["
Of course, you can also create a pandas dataframe and work with this:
memory = pd.DataFrame(mem.create_summary(), columns=['object', 'number_of_objects', 'memory'])
memory['mem_per_object'] = memory['memory'] / memory['number_of_objects']
print(memory.sort_values('memory', ascending=False).head(10))
print(memory.sort_values('mem_per_object', ascending=False).head(10))
Output:
object number_of_objects memory mem_per_object
11 .Laz... 2 66000 33000.000000
10637 .La... 2 9248 4624.000000
1594