python - 在同一进程中使用 Gstreamer 和 multiprocessing.Manager().Namespace() 时发生内存泄漏
问题描述
我正在使用 Gstreamer 为一些视频制作故事板。我将每一帧放在 multiprocessing.Manager().Namespace() 中,以便在进程之间传输。在这种情况下,观察到内存泄漏。在 10 小时的应用程序工作期间,内存消耗增加了 400 MB。如果注释掉 的调用self.update_frame(buf.extract_dup(0, buf.get_size()))
,则不会发生内存泄漏。
内存泄漏测试的示例:
import unittest
import gi
import traceback
import os, psutil
from multiprocessing import Process, Manager, Event
gi.require_version('Gst', '1.0')
from gi.repository import Gst
class RtpNamespaceTest(unittest.TestCase):
pipeline_str = '''
videotestsrc pattern=ball ! \
appsink name=handle-app-sink \
emit-signals=True \
max-buffers=1 \
drop=True \
'''
name_space = Manager().Namespace()
pipeline = None
event_interrupt: Event = Event()
def start(self):
# initializing gstreamer, subscribing to rtp-stream
Gst.init(None)
print(self.pipeline_str)
self.pipeline = Gst.parse_launch(self.pipeline_str)
self.pipeline.set_state(Gst.State.PLAYING)
self.appsink = self.pipeline.get_by_name('handle-app-sink')
self.appsink.connect("new-sample", self.on_new_buffer)
bus = self.pipeline.get_bus()
# listen to messages until storyboarding is stopped
while not self.event_interrupt.is_set():
bus.timed_pop_filtered(10000, Gst.MessageType.ANY)
# stops the pipeline, free up resources
self.pipeline.set_state(Gst.State.NULL)
def on_new_buffer(self, src):
sample = src.emit("pull-sample")
buf = sample.get_buffer()
# writing a frame in binary format in Namespace()
self.update_frame(buf.extract_dup(0, buf.get_size()))
print(f"RAM = {psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024}")
return Gst.FlowReturn.OK
def update_frame(self, frame: bytes):
self.name_space.frame = frame
def test_repository(self):
while True:
try:
self.start()
except Exception as ex:
traceback.print_exc()
time.sleep(float(10))
解决方案
推荐阅读
- python - 如何克服人脸检测安全问题,使其在有人展示图片时无法检测到人脸?
- java - 在 javaFX 和 Scene Builder 中切换场景时遇到问题
- mongodb - 遇到错误 mongodb 配置组:get 不存在
- sql - BigQuery 获取范围内的所有值
- python - 识别缺失词所属的句子中的位置
- google-maps - 无法在颤动中拖动谷歌地图上的标记
- c# - 如何修复“添加运算符中的数据类型 datetime 和 uniqueidentifier 不兼容。”
- javascript - 数据库表输出结构更改以下格式说明
- java - 如何使用 Jackson 序列化符合自定义标准的列表
- multithreading - 使用 Delphi Threads 的 4 核处理器效率为 25%