【问题标题】:Python: Ctypes how to check memory managementPython:Ctypes如何检查内存管理
【发布时间】:2020-08-29 21:24:17
【问题描述】:

所以我使用 Python 作为前端 GUI,它与一些 C 文件交互以作为后端进行存储和内存管理。每当 GUI 的窗口关闭或退出时,我都会为分配的变量调用所有析构函数。

是否在退出整个程序之前检查内存泄漏或可用性,例如 C Valgrind 检查,以确保没有任何内存泄漏?

示例退出:

from tkinter import *
root = Tk()  # New GUI
# some code here

def destructorMethods:
    myFunctions.destructorLinkedList()  # Destructor method of my allocated memory in my C file
    # Here is where I would want to run a Valgrind/Memory management check before closing
    root.destroy()  # close the program

root.protocol("WM_DELETE_WINDOW", destructorMethods)  # When the close window option is pressed call destructorMethods function

【问题讨论】:

  • C 代码是你写的吗?因为到目前为止最简单的选择就是修复内存泄漏。 python 代码是否了解所有 C 分配?如果是这样,您可以在 python 中创建一个基本的垃圾收集器来跟踪您的块。

标签: python c memory-management valgrind ctypes


【解决方案1】:

我认为您可以使用支持泄漏检测的调试内存分配器。

像 tcmalloc 这样的东西会做。您只需 LD_PRELOAD 它,您对 malloc 、 free 等的所有调用都会调试泄漏。

【讨论】:

    【解决方案2】:

    trace python memory leaks 看看这篇文章 - 了解更多详情 - 这些问题,在这里:

    【讨论】:

      【解决方案3】:

      如果您想使用Valgrind,那么这个readme 可能会有所帮助。可能,this 可能是另一个很好的资源,可以让Valgrind 友好的 python 并在你的程序中使用它。

      但是,如果您考虑其他类似 tracemalloc 的东西,那么您可以很容易地获得它的一些示例用法 here。这些例子很容易解释。例如根据他们的文档,

        import tracemalloc
        tracemalloc.start()
      
        # ... run your application ...
        snapshot = tracemalloc.take_snapshot()
        top_stats = snapshot.statistics('lineno')
        print("[ Top 10 ]")
        for stat in top_stats[:10]:
        print(stat)
      

      这将输出类似的东西。

       <frozen importlib._bootstrap>:716: size=4855 KiB, count=39328, average=126 B
       <frozen importlib._bootstrap>:284: size=521 KiB, count=3199, average=167 > 
      

      您可以对其进行解析以绘制内存使用情况以供您调查,也可以 使用参考 doc 获得更具体的想法。

      在这种情况下,您的程序可能类似于以下内容:

       from tkinter import *
       import tracemalloc
       root = Tk()  # New GUI
       # some code here
      
       def destructorMethods:
           tracemalloc.start()
           myFunctions.destructorLinkedList()  # Destructor method of my allocated memory in my C file
           # Here is where I would want to run a Valgrind/Memory management check before closing
           snapshot = tracemalloc.take_snapshot()
           top_stats = snapshot.statistics('lineno')
           print("[ Top 10 ]")
           for stat in top_stats[:10]:
               print(stat)
           
           root.destroy()  # close the program
      
       root.protocol("WM_DELETE_WINDOW", destructorMethods)  
      

      另一种选择是,您可以使用内存分析器查看可变时间的内存使用情况。该软件包可用here。安装此软件包后,您可能可以在脚本中使用以下命令来获取 png 文件中一段时间​​内的内存使用情况。

       mprof run --include-children python your_filename.py
       mprof plot --output timelyplot.png
      

      或者您可以根据需要使用memory_profiler 包中提供的不同功能。或许this 教程对你来说会很有趣。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-01
        • 2013-07-26
        • 2011-09-30
        • 1970-01-01
        • 2013-05-28
        • 2012-07-24
        • 1970-01-01
        相关资源
        最近更新 更多