使用GStreamer filesink时内存占用持续增长问题求助
Jetson Xavier上GStreamer filesink内存泄漏问题分析与解决思路
问题现象
在Jetson Xavier开发板上长时间运行包含filesink的GStreamer管道后,出现OOM(内存不足)错误。内存占用呈线性持续增长,且停止进程后缓存未释放。
最小复现示例
gst-launch-1.0 videotestsrc ! omxh265enc ! qtmux ! filesink location=test.mp4 -e
内存变化验证
含filesink的管道内存状态
- 运行前:
sh-5.1# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 3 0 0 5384300 307304 18665008 0 0 9 59 28 51 6 2 92 0 0
- 运行2分钟后:
sh-5.1# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 10 0 0 5151928 307560 18820992 0 0 9 59 29 1 6 2 92 0 0
- 运行10分钟后:
sh-5.1# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 4 0 0 4794396 308008 19138836 0 0 9 60 31 5 6 2 92 0 0
- 停止进程后:
sh-5.1# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 4836532 308040 19140984 0 0 9 60 31 5 6 2 92 0 0
替换为fakesink后的对比
将filesink替换为fakesink后无内存泄漏问题,对应管道:
gst-launch-1.0 videotestsrc ! omxh265enc ! qtmux ! fakesink
- 运行前:
sh-5.1# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 4836532 308040 19140984 0 0 9 60 31 5 6 2 92 0 0
- 运行2分钟后:
sh-5.1# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 4608692 308192 19141064 0 0 9 60 32 7 6 2 92 0 0
问题定位
替换filesink为fakesink后内存泄漏消失,且更换splitmuxsink、mp4mux或编码器仍存在问题,推测内存泄漏与filesink组件直接相关,大概率是Jetson平台适配的filesink实现存在未正确释放内存的情况。
解决建议
使用Jetson专用的nvfilesink替代filesink
Jetson平台提供了针对本地文件写入优化的nvfilesink组件,替换后可规避通用filesink的内存泄漏问题,示例管道:gst-launch-1.0 videotestsrc ! omxh265enc ! qtmux ! nvfilesink location=test.mp4 -e调整filesink参数
尝试启用flush-on-close=true参数,确保进程退出时强制刷新并释放缓存:gst-launch-1.0 videotestsrc ! omxh265enc ! qtmux ! filesink location=test.mp4 flush-on-close=true -e另外也可尝试设置
buffer-mode=unbuffered关闭文件写入缓冲,减少内存占用。升级JetPack/GStreamer版本
该问题可能是已知bug,NVIDIA在后续JetPack版本中可能修复了filesink的内存泄漏问题,建议升级到最新稳定版JetPack。调试内存泄漏点
启用GStreamer的内存调试日志跟踪内存分配:GST_DEBUG=GstBuffer:6 gst-launch-1.0 videotestsrc ! omxh265enc ! qtmux ! filesink location=test.mp4 -e或使用
valgrind(Jetson平台需提前安装)排查具体泄漏位置:valgrind --leak-check=full gst-launch-1.0 videotestsrc ! omxh265enc ! qtmux ! filesink location=test.mp4 -e临时缓存清理方案
若上述方法暂时无法生效,可定时手动清理系统缓存(仅作为临时应急方案):echo 3 > /proc/sys/vm/drop_caches
内容的提问来源于stack exchange,提问作者eden_debuggin
相关产品推荐
相关产品推荐

