Understand and Optimize Python memory with slots

Viewed 43

I'm working with a large existing Python code base, which has an internal graph model, with nodes and edges being regular Python classes. I'd like to optimize the memory footprint by converting these to slotted classes -- currently, the memory usage is creating severe issues.

I think using slots may help, as there are a few dozens of classes, but hundreds of thousands of instances of these classes which create the graph model.

To that end, I have a couple of questions:

  1. How to get the overall application memory usage? I'm using psutil.Process().memory_info.rss - is that the preferred option?

  2. How to know which specific classes to focus on for adding slots? Ideally a tool/report which can show number of instances x memory per instance for all user defined classes? I have been trying out Pympler, but that requires adding tracking code for all classes individually.

In both of the above, I'd like to know if there are better approaches that I may have missed.

0 Answers
Related