Memory Leak in Deepstream parallel branches

Please provide complete information as applicable to your setup.

• Hardware Platform (Jetson / GPU) dGPU (L4 GPU)
• DeepStream Version 7.0
• JetPack Version (valid for Jetson only)
• TensorRT Version:8.6
• NVIDIA GPU Driver Version (valid for GPU only):12.2
• Issue Type( questions, new requirements, bugs) BUG
• How to reproduce the issue ? (This is for bugs. Including which sample app is using, the configuration files content, the command line used and other details for reproducing) No Idea
• Requirement details( This is for new requirement. Including the module name-for which plugin or for which sample application, the function description)

Hi I have the following setup from where I am observing memory leak. The leak is approximately 2 GB per day which increases to 3-4 gb per day when increase in number of sources to 7. I have not tested beyond that yet.
No of cameras: 4
No. of parallel branches:2
I ran my pipeline with below command to get the leak:
PYTHONMALLOC=malloc valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all --track-origins=yes --trace-children=yes --log-file=valgrind.log --suppressions=/usr/lib/valgrind/python3.supp python3 main.py --logging --measure_latency

Uploaded my valgrind log in below drive link.

Please let me know if anything else is requreed.

Thanks for the sharing! is this a custom code? what is the complete media pipeline? can you reprodcue this issue with the latest DS7.1? can you simplify the code to narrow down this issue? for example, if Using one branch or removing some element temporarily, does the issue persist?

is this a custom code?
Yes Fanzh, this is a custom code built using gst and deeepstream plugins only. Plugins are all already available, I did not build any new plugin.

what is the complete media pipeline?
Pipeline diagram I am sharing with you in message (with subject: Pipeline diagarm for memory leak July 23) because we cannot share publicly . I will be happy to share relavent codes via private communications if needed

can you reproduce this issue with the latest DS7.1
I need to migrate my codebase to 7.1, let me see if my team has bandwidth to do so on urgent basis. Will update you on that.

can you simplify the code to narrow down this issue?
I will test with single branch to if issue persists. Can you please review my pipeline diagarm and valgrind log and suggest me the steps to narrow down the issue and reproduce it?
Valgrind logs are pretty huge and I tried to filter some relavent ones in “valgrind_definitely_lost.txt”. Kindly look into once and see if you can point me in right direction.

  1. Notcing you are using new-streammux, what is the resoltuion and fps of the RTSP source? if there are different resolutions, you need to convert to the same resolution with nvvideoconvert plugin because new-streammux does not support scaling. please refer to related doc heterogeneous-batching.
  2. about “The leak is approximately 2 GB per day”, wondering the actual memory usage after running stably, could you share a nvmemstat log by referring to this topic?
  3. after checking the valgrind log, there are some memory leaks in new-streammux, which is opensource. Here is one fix demux-fix.txt (748 Bytes).

Hi @fanzh , I have done following experiments:

  1. Tested with single branch and memory leak still persists.
  2. Tested with old streammux, still getting memory leak. Added valgrind logs with single branch+old streammux in same drive and “single_branch_leak”

Now coming to your suggestions:
Notcing you are using new-streammux, what is the resoltuion and fps of the RTSP source?
We observed streams with different resolutions causes them to randomly pick one resolution so we have handled that from the source side i.e 1920*1080. The fps is variable which lies between 25-30 fps. One stream is 60 fps.

about “The leak is approximately 2 GB per day”, wondering the actual memory usage after running stably, could you share a nvmemsta
Let me do that and get back to you.

after checking the valgrind log, there are some memory leaks in new-streammux
Can you please check on this, you have shared fix for demux but you are saying new-streammux.

Regarding migrating to 7.1:
We are migrating our codebase. Will update you once we have tested

  1. noticing there are many “gst_element_factory_create” in single_branch_leak-20250728T093744Z-1-001.zip, did you restart pipeline many times? or recreate plugins many times?
  2. about “you have shared fix for demux but you are saying new-streammux.”, if you run “export USE_NEW_NVSTREAMMUX=yes”, both nvstreammux and nvstreamdexmux will use the new version, which is opensource. the code path is /opt/nvidia/deepstream/deepstream-7.1/sources/gst-plugins/gst-nvmultistream2.

1. did you restart pipeline many times? or recreate plugins many times?
No fanz, just once. Not sure why this is happenning. Since I am running rtsp feeds, I had added a function to stop the pipeline after some time to add valgrind logs. Maybe because of that.

def stop_after_hour():
            print("🏁 One hour elapsed: shutting down pipeline")
            #self.pipeline.set_state(Gst.State.NULL)
            
            loop.quit()
            
            return True 
        bus = self.pipeline.get_bus()
        bus.add_signal_watch()
        #bus.add_signal_watch_full()
        bus.connect("message", bus_call, loop)
        
        branch_image_dict = self.branch_for_camera()
        
        for key,value in self.probes_for_metadata.items():
            value["element"].get_static_pad("src").add_probe(Gst.PadProbeType.BUFFER, osd_sink_pad_buffer_probe, [value["branch_name"]])
           

        self.capsfilter.get_static_pad("src").add_probe(Gst.PadProbeType.BUFFER, extract_raw_frames, branch_image_dict)
        
        if self.args.measure_latency:
            GLib.timeout_add(1000, perf_data.perf_print_callback)
        GLib.timeout_add(120000, stop_after_hour) #two hrs
        self.pipeline.set_state(Gst.State.PLAYING)
        
        
        Gst.debug_bin_to_dot_file(self.pipeline, Gst.DebugGraphDetails.ALL, "pipeline_graph")
        #self.add_probes()
        try:
            loop.run()
        except:
            pass

        # Cleanup and shutdown when done
        self.pipeline.set_state(Gst.State.NULL)
        #Gst.deinit()

  1. about “you have shared fix for demux but you are saying new-streammux.”, if you run “export USE_NEW_NVSTREAMMUX=yes”, both nvstreammux and nvstreamdexmux will use the new version, which is opensource.

Since single branch does not require demux and using old streammux and still facing memory leak, we can safely assume there is another point as well causing leak., sharing piplene diagram for single branch in dm.

the pipeline of pipeline_diagram_single_branch is similar to NV sample deepstream_test_3.py , which supports rtspsrc, nvinfer and tracker. can you use deepstream-test3 to reproduce this memory leak issue. if no, you can compare your custom code with test3, if so, could you provide valgrind and nvmemstat log mentioned above? Thanks!

sure fanzh, let me do that.

@akash.kumar1 did you try deepstream_test_3.py? could you share the test results regarding memory leak? Thanks!

Hi @fanzh , yes I tried a couple of things and narrowed down the issue. It’s not coming from any deepstream plugins. It is coming when I try to reconnect the streams when eos is reached. I have a custom reconnect logic as following.
When we recive eos from source-bin we set source to null state and remove it from the pipeline and create a new source bin and link it to the same streammux pad. Have shared my source connection script to you privately. CAn you please take a glance and let me know if you can help us there?

Edit:
You might siggest nvurisourcebin that have pre-built reconnection logic but, we observed many times it simply fails to reconnect. That’s why we decided to build our custom logic

Thanks for the update! Please open a new topic if having other DeepStream problems.