Answer the question
In order to leave comments, you need to log in
How to get rid of memory leak in python script?
Good afternoon!
Created a multi-threaded application using the Thread. Several threads (three) work in an endless while-true loop and, at some intervals, access either the database or another server through the API, variables are overwritten, some logic works. All possible try-catch exceptions are taken into account. The program as a whole performs its functions, but very much "eats" RAM. The program was launched as a service and in 2 days of work it took 1GB 150 MB of RAM. I read about the GC collector, imported it, included the library, wrote the gc.collect() function, but there was no effect. Tell me, please, in the cyclic operation of the script, what are the recommendations for optimization? I believe that after reassignment, the old value of the variable still remains in RAM. What do you advise?
Answer the question
In order to leave comments, you need to log in
Start by looking for what exactly is leaking, use the same https://pypi.org/project/memory-profiler/ Sometimes it happens that this is due to an external library, while the option is to make while not infinite and exit the application, but systemd to restart it.
Apache is designed so that its processes (there are several of them, they do the work) perform a certain number of requests (specified in the config), and then exit. To replace the dead - the main process (it distributes work between them) launches new processes. It's processes, not threads.
Learn "stateless programs", "mini-services".
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question