Search code examples
memory-leaksffmpeglibavcodeclibavlibavformat

Libav - how to correctly free memory leaking with av_write_frame


I am using LIBAV on Ubuntu to save user's video stream(RTP VP8) on server in WebM format. The problem is, memory is leaking when using av_write_frame. Memory usage is constantly growing (equally with the webm file size) and is never freed after finishing the video recording. The only way to free the memory(RAM) is deleting the WebM file from the storage (HD) afterwards.

I have 2 questions:

  1. Is it possible to free the memory consumed by av_write_frame during runtime? I am freeing the packet.data correctly. Memory usage is not growing when av_write_frame line is commented.
  2. What is the correct way to close the file? This is what I'm doing (it does not free the memory):

    av_write_trailer(fctx); avcodec_close(vStream->codec); avio_close(fctx->pb); avformat_free_context(fctx);


Solution

  • This is Linux kernel memory management thing. As I am a Linux newbie, I didn't know. Memory is not leaking, Linux is just caching file content into RAM.

    For better explanation take a look at: https://askubuntu.com/questions/155768/how-do-i-clean-or-disable-the-memory-cache/155771#155771