0

I have a massive Python script I inherited. It runs continuously on a long list of files, opens them, does some processing, creates plots, writes some variables to a new text file, then loops back over the same files (or waits for new files to be added to the list).

My memory usage steadily goes up to the point where my RAM is full within an hour or so. The code is designed to run 24/7/365 and apparently used to work just fine. I see the RAM usage steadily going up in task manager. When I interrupt the code, the RAM stays used until I restart the Python kernel.

I have used sys.getsizeof() to check all my variables and none are unusually large/increasing with time. This is odd - where is the RAM going then? The text files I am writing to? I have checked and as far as I can tell every file creation ends with a f.close() statement, closing the file. Similar for my plots that I create (I think).

What else would be steadily eating away at my RAM? Any tips or solutions?

What I'd like to do is some sort of "close all open files/figures" command at some point in my code. I am aware of the del command but then I'd have to list hundreds of variables at multiple points in my code to routinely delete them (plus, as I pointed out, I already checked getsizeof and none of the variables are large. Largest was 9433 bytes).

Thanks for your help!

tgbnez
  • 23
  • 6
  • 1
    Have you run a profiler on the script? (Difficult for us to help without seeing the code or profiler output.) – S3DEV Jun 08 '22 at 17:03
  • I can - what is a profiler? – tgbnez Jun 08 '22 at 17:16
  • Does this answer your question? [Python memory leaks](https://stackoverflow.com/questions/1435415/python-memory-leaks) – jurez Jun 08 '22 at 17:33
  • [Another SO post](https://stackoverflow.com/q/552744/6340496) you might find useful. Additionally, a link to the [tracemalloc](https://docs.python.org/3/library/tracemalloc.html) documentation. – S3DEV Jun 08 '22 at 17:48
  • Thanks, I will dig into those references. Much appreciated! – tgbnez Jun 08 '22 at 18:15

0 Answers0