kiranvizru / psutil

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

Memory leak on windows #432

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I found a serious memory leak when my project run period of hours. Here is the 
test code:

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

import logging
import psutil
import time
import _psutil_mswindows

while True:
    try:
        # # leak
        # for process in psutil.process_iter():
        #     m = process.get_memory_info()

        # # leak too
        # for pid in psutil.get_pid_list():
        #     process = psutil.Process(pid)
        #     m = process.get_memory_info()
        #     del process

        # # not leak
        # for pid in psutil.get_pid_list():
        #     m = _psutil_mswindows.get_process_memory_info(pid)

        # leak
        for pid in psutil.get_pid_list():
            m = _psutil_mswindows.get_process_memory_info_2(pid)
    except Exception as ex:
        logging.warning(ex)
    finally:
        time.sleep(0.01)

Run this code and open windows task manager, you will see memory usage of the 
process grows rapidly.

Finally I located the problem in _psutil_mswindows.get_process_memory_info_2(). 
I checked the corresponding C source code and found in _psutil_mswindows.c, 
get_process_memory_info_2() calls get_process_info() in process_info.c, and 
get_process_info() uses internal pointer "process" and "buffer" as return 
value, and get_process_memory_info_2() doesn't free these two pointers.

Maybe using pointer as return value is not a good choice, and all places using 
get_process_info() should be checked.

Original issue reported on code.google.com by shajunx...@gmail.com on 22 Sep 2013 at 11:24

GoogleCodeExporter commented 9 years ago
This should be a duplicate of issue 413.

Original comment by g.rodola on 22 Sep 2013 at 12:41

GoogleCodeExporter commented 9 years ago

Original comment by g.rodola on 26 Sep 2013 at 6:46