I have a problem with memory usage of my code and it is that the memory increase dramatically, 3 GB per hour and I need around 80 hours for my calculation. How can I check memory usage for each part of my calculation?
You can insert gc.collect()
in places where you think it would be beneficial. Of course, if you are holding references to memory it will not be freed. Also, if you are allocating memory in C, then you are responsible for freeing it.
The normal memory handling facilities of Python
do a very good job, so I would only recommend calling gc.collect()
if you are sure you are deleting references to large amounts of memory and it is not getting released fast enough.
As to where you should put these calls, there is no hard fast rule, just keep in mind that only unreferenced memory will be freed.