0

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);

golstar
  • 56
  • 1
  • 7
  • Are you freeing the packet with av_free_packet after each write operation? – Maxito Feb 25 '15 at 21:45
  • Yes and I'm also freeing the memory assigned to `packet.data`. The problem is in `av_write_frame` directly, or maybe indirectly, since my memory usage grows linearly with the .webm file size. – golstar Feb 26 '15 at 13:36

3 Answers3

1

If your memory gets freed when you delete the file, this would indicate that you might be writing your data on a RAM disk or to a folder that is a symbolic link to a RAM disk. In some Linux systems for example, the /tmp folder is a separate partition on the RAM.

  • You might want to check if the file still exists after a reboot. If not, you probably never wrote the file to disk.

Writing your data onto RAM can be a good idea when measuring execution time, as you are free from the latency introduced by writing to disk. Just be aware of it cause they are non-persistent.

(Would have made this a comment to another answer, but I cannot comment yet due to insufficient reputation)

Niko
  • 642
  • 6
  • 12
0
  1. Please make sure you use the latest ffmpeg and VP8 libraries. av_write_frame should not allocate any memory which should be freed. You can confirm by writing one frame and then closing the stream, and running this program under Valgrind. There are many other things to free, but since you are sure commenting out av_write_frame stops the leak, this does not apply to you.

  2. I assume somewhere in your code you have:

    stream = avformat_new_stream( fctx, codecCtx->codec );
    

right? Then you also also need to free the streams:

    for ( unsigned int i = 0; i < fctx->nb_streams; i++ )
    {
        av_freep(&fctx->streams[i]->codec);
        av_freep(&fctx->streams[i]);
    }
George Y.
  • 11,307
  • 3
  • 24
  • 25
  • Thank you very much. Steps in 2. you have mentioned are already done in `avformat_free_context(fctx);` therefore they obviously led to segmentation fault. My problem is that data from the WEBM file are somehow glued to Memory - when I delete WebM file, Memory is also freed, which is very weird for me. – golstar Feb 26 '15 at 15:23
  • Strange. Did you do this before avformat_free_context(fctx) or after? Regarding the first bug, it indeed sounds very weird. – George Y. Feb 26 '15 at 22:24
  • If you mean freeing streams, it is done directly inside `avformat_free_context(fctx);`. Source(line 2719, 2722): https://libav.org/doxygen/release/0.8/libavformat_2utils_8c_source.html – golstar Feb 27 '15 at 09:16
0

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

Community
  • 1
  • 1
golstar
  • 56
  • 1
  • 7