How to properly dispose of an errored pipeline

I have a pipeline that’s streaming videos to an rtmp endpoint. Sometimes the rtmp connection would get severed so I’m trying to completely shutdown the pipeline and recreate it. I do that by setting the pipeline state to null and unreffing it (in rust, it just goes out of scope so it’s dropped). Then I create a new pipeline.

The problem is that I see an increased memory usage after repeating this process for a few times. Most probably some buffered data that’s not being freed?

I tried to also manually remove all the elements from the pipeline (set pipeline to null, iterate through elements and remove them from the pipeline) but the same increase in memory usage happens.

Is there a specific protocol to completely shutdown/free a pipeline (without killing the whole process)?