Why does time.sleep cause a memory leak?

When I ran the code below, the memory increased. However, if I deleted time.sleep(3) , it was 0.1 in top and never increased.

The process does not seem to end correctly, but why?

code ( Python 2.7.11 ):

 import time import multiprocessing def process(): #: FIXME time.sleep(3) return def main(): pool = multiprocessing.Pool(processes=10) while 1: pool.apply_async(process) pool.close() pool.join() if __name__ == '__main__': main() 
+5
source share
1 answer

As far as I know, I think that when you create new processes in the same pool, garbage collection is never performed, so you do not release memory from old processes, even if you finish using them. One fix would be forcing garbage collection in your while loop:

 import time import multiprocessing import gc def process(): time.sleep(3) return def main(): pool = multiprocessing.Pool(processes=10) while 1: pool.apply_async(process) gc.collect() pool.close() pool.join() if __name__ == '__main__': main() 

This fixed a memory leak for me as you force garbage collection before starting another set of processes. Hope someone can explain in more detail the cause of this memory leak.

+1
source

Source: https://habr.com/ru/post/1240910/


All Articles