Gio.MemoryInputStream公司关闭时不释放内存

2024-09-29 17:19:57 发布

您现在位置:Python中文网/ 问答频道 /正文

在Windows7上运行Python3.4Gio.MemoryInputStream公司不会像应该的那样释放内存。测试代码为:

from gi.repository import Gio
import os, psutil

process = psutil.Process(os.getpid())

for i in range (1,10) :
    input_stream = Gio.MemoryInputStream.new_from_data(b"x" * 10**7)
    x = input_stream.close_async(2)
    y = int(process.memory_info().rss / 10**6)  # Get the size of memory used by the program
    print (x, y)

这将返回:

True 25
True 35
True 45
True 55
True 65
True 75
True 85
True 95
True 105

这表明,在每个循环中,即使close函数返回True,程序使用的内存也会增加10 MB。 一旦流关闭,怎么可能释放内存?你知道吗

另一个好的解决方案是重用流。但设置\数据或替换\数据会引发以下错误: '不支持数据访问方法。改用普通Python属性' 好吧,那是哪家酒店?你知道吗

在python3.4中我需要一个内存流。我用PyPDF2创建了一个Pdf文件,然后我想用Poppler预览它。由于Poppler中有一个bug(请参见Has anyone been able to use poppler new_from_data in python?),我不能使用new\u from\u data函数,我想使用new\u from\u stream函数。你知道吗


Tags: 数据函数内存infromimporttruenew
2条回答

这是a bug in GLib’s Python bindings,不能简单地修复。你知道吗

相反,您应该使用^{},它以不同的方式处理释放内存,并且不应该遭受相同的错误。你知道吗


更详细地说,new_from_data()的bug是由introspection annotations引起的,GLib使用它来允许语言绑定自动公开其所有API,而不支持new_from_data()GDestroyNotify参数,该参数需要设置为非NULL函数以释放传递给其他参数的分配内存。在gdb下运行脚本表明pygobject将NULL传递给GDestroyNotify参数。它做得再好不过了,因为目前没有办法表示data参数的内存管理语义依赖于传递给destroy的内容。你知道吗

谢谢你的回答,菲利浦·维斯纳尔。我测试了你提出的解决方案,效果很好。为了帮助其他人理解,以下是我的测试代码:

from gi.repository import Gio, GLib
import os, psutil

process = psutil.Process(os.getpid())

for i in range (1,10) :
    input_stream = Gio.MemoryInputStream.new_from_bytes(GLib.Bytes(b"x" * 10**7))
    x = input_stream.close()
    y = int(process.memory_info().rss / 10**6)  # Get the size of memory used by the program
    print (x, y)

现在y不再生长。你知道吗

相关问题 更多 >

    热门问题