baweaver / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

Memory leak in the windows version of get_ppid_map #448

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

The following script will consume an increasing amount of memory:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import logging
import psutil
import time

while True:
    try:
        for pid in psutil.get_pid_list():
            m = psutil.Process(pid)
        psutil._psmswindows.get_ppid_map()
    except Exception as ex:
        logging.warning(ex)
    finally:
        time.sleep(0.01)

What is the expected output?

No memory leaks

What do you see instead?

A memory leak

What version of psutil are you using? What Python version?

hg clone from  Tue Nov 26 18:00:55 2013 +0100
changeset:   1690:578f037d6dbb

On what operating system? Is it 32bit or 64bit version?

Windows 7 32 bit

Please provide any additional information below.
The following changeset resolves the problem for me

changeset:   1691:4862f1b232e9
tag:         tip
user:        Ulrich Klank <ulrich.klank@scitics.de>
date:        Sat Nov 30 00:01:59 2013 +0100
summary:     Fix for memory leak in windows version of get_ppid_map

diff -r 578f037d6dbb -r 4862f1b232e9 psutil/_psutil_mswindows.c
--- a/psutil/_psutil_mswindows.c        Tue Nov 26 18:00:55 2013 +0100
+++ b/psutil/_psutil_mswindows.c        Sat Nov 30 00:01:59 2013 +0100
@@ -2868,14 +2868,22 @@

     if (Process32First(handle, &pe)) {
         do {
+
             pid = Py_BuildValue("I", pe.th32ProcessID);
             if (pid == NULL)
                 goto error;
+
             ppid = Py_BuildValue("I", pe.th32ParentProcessID);
             if (ppid == NULL)
                 goto error;
+
             if (PyDict_SetItem(py_retdict, pid, ppid))
                 goto error;
+
+            Py_XDECREF(pid);
+            Py_XDECREF(ppid);
+            pid = NULL;
+            ppid = NULL;
         } while (Process32Next(handle, &pe));
     }

Original issue reported on code.google.com by uli.kl...@googlemail.com on 29 Nov 2013 at 11:08

GoogleCodeExporter commented 8 years ago
Thanks. Fixed in revision 385524f5c752.

Original comment by g.rodola on 2 Dec 2013 at 7:16

GoogleCodeExporter commented 8 years ago
Closing out as fixed as 2.0.0 version is finally out.

Original comment by g.rodola on 10 Mar 2014 at 11:36