This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Memory leak in threading library with Python 3.6-3.8
Type: resource usage Stage: resolved
Components: Windows Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Cristian Martinez de Morentin, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Priority: normal Keywords:

Created on 2020-05-20 11:35 by Cristian Martinez de Morentin, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
script.py vstinner, 2021-09-24 16:04
Messages (2)
msg369443 - (view) Author: Cristian Martinez de Morentin (Cristian Martinez de Morentin) Date: 2020-05-20 11:35
Hi everyone,

I have found a memory leak when using Queue and Condition from threading library.

The issue can be reproduced with the following code:

========================================================
import queue
import threading


class MemoryTest(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.queue = queue.Queue()
        self.cv = threading.Condition()

    def put(self, msg):
        self.queue.put(msg)

        with self.cv:
            self.cv.notify()

    def run(self):
        while True:
            while not self.queue.empty():
                self.queue.get()
                self.queue.task_done()

            with self.cv:
                self.cv.wait_for(lambda: not self.queue.empty())
================================================================

If you run a MemoryTest object in another thread, by calling its start() method, and you send it messages by using its put() method, you will see how RAM memory usage starts increasing.

This behaviour has been observed in Windows (64 bits) with Python 3.6, 3.7 & 3.8, but not with Python 3.5.

Thank you so much.
msg402576 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-09-24 16:04
I fail to reproduce the leak using attached script.

I close the issue.

I get constant memory usage on Linux with the main branch of Python (future 3.11):

(...)
VmRSS:	   11344 kB
VmRSS:	   11344 kB
VmRSS:	   11344 kB
VmRSS:	   11344 kB
VmRSS:	   11344 kB
VmRSS:	   11344 kB
VmRSS:	   11344 kB
VmRSS:	   11344 kB
VmRSS:	   11344 kB
VmRSS:	   11344 kB
(...)
History
Date User Action Args
2022-04-11 14:59:31adminsetgithub: 84876
2021-09-24 16:04:39vstinnersetstatus: open -> closed
files: + script.py


nosy: + vstinner
messages: + msg402576
resolution: not a bug
stage: resolved
2020-05-20 11:35:48Cristian Martinez de Morentincreate