python - 我如何查看垃圾收集器中的内容

标签 python

我正在学习安全挑战。其中一项挑战要求我检索已被破坏并保存在内存中直到垃圾收集器触发的数据。

我尝试了很多东西。我想知道是否可以从 python 控制台转储进程内存。

另外,挑战给了我们一个python控制台,但是有一个秒表而不是“>>>”。他们怎么能做那样的事? 我只有一个可以启动 python 控制台的 ssh 访问权限。

谢谢

最佳答案

您可以使用下面的代码查看垃圾中有什么。

import gc

def dump_garbage():
    """
    show us what's the garbage about
    """
    # force collection
    print("\nGARBAGE:")
    gc.collect()

    print("\nGARBAGE OBJECTS:")
    for x in gc.garbage:
        s = str(x)
        if len(s) > 80: s = s[:80]
        print(type(x),"\n  ", s)

if __name__=="__main__":
    import gc
    gc.enable()
    gc.set_debug(gc.DEBUG_LEAK)

    # make a leak
    l = []
    l.append(l)
    del l

    # show the dirt ;-)
    dump_garbage()

关于python - 我如何查看垃圾收集器中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45734315/

相关文章:

python - 错误 : sqlite3. 操作错误 : no such table: main. m

python - 如何更改 Django 表单集中 DELETE 字段的小部件类型

Python while 循环永不结束(在 numworks 计算器上)

python - 在 appengine 中通过代码处理 404 抛出

python - 具有多个 y 轴的误差条图会产生 ValueError

python - 类型错误 : __init__() got an unexpected keyword argument 'many'

python - GitPython 并向 Git 对象发送命令

python - 在后台运行基于 GUI 的应用程序的 .exe

python - 从 PIL 图像转换后,Numpy 数组不显示灰度图像的颜色尺寸

python - 为什么 Flask bool 查询参数总是评估为真?