Skip to content

Memory leak in the windows version of get_ppid_map #448

Description

@giampaolo

From uli.kl...@googlemail.com on November 30, 2013 00:08:36

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: http://code.google.com/p/psutil/issues/detail?id=448

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtypeimportedimported from the Google Code migration (deprecated)memleakmemory is leaked at the C level: a refcount never released, memory not freed, ...windowsplatform : Windows specific

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions