1

I have a simple application which uses ffmpeg for decoding.

Usually it works just fine. However, when I try to play a certain file its starts eating up my memory. Even when I release all resource used by the decoder the memory is not freed.

I have tried running a memory leak detector (intel parallel inspector), but it doesn't detect the leak as the memory is probably release during shutdown.

My question is how can I see where and how much memory has been allocated during runtime?

EDIT: Added windows tag.

ronag
  • 49,529
  • 25
  • 126
  • 221
  • 6
    Maybe try valgrind – Rom1 May 15 '11 at 09:47
  • Will do. What does valgrind do differntly from intel parallel inspector? – ronag May 15 '11 at 09:51
  • 1
    Keep in mind that even when memory is free()'d/deleted , it is often *not* released back to the operating system. It's just released back to the applicaton memory allocation pool. – nos May 15 '11 at 09:52
  • Unfortunately I do not have a Linux installation, and my application will not run on Linux. – ronag May 15 '11 at 09:53
  • @nos: Good point. I'm using the TBB memory allocator, so maybe it is keeping the memory. However, I need some way to confirm this. – ronag May 15 '11 at 09:55
  • The memory might not be being leaked formally. It could be being kept unexpectedly in something that's reachable, which would not be a leak in a technical sense, but would smell the same. Alas, these “not quite” leaks are really hard to hunt even with tools… – Donal Fellows May 15 '11 at 10:29
  • Both release and debug builds leak. – ronag May 15 '11 at 10:37

2 Answers2

3

Try using valgrind with the option --leak-check=full to see if any memory is being leaked. http://valgrind.org/

EDIT: Now that I see the target platform is windows, this question might be of interest: Is there a good Valgrind substitute for Windows?

Community
  • 1
  • 1
shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • Good idea. However my platform is Windows. I forgot to add the tag in my original post. My apologies. – ronag May 15 '11 at 10:03
  • @ronag: There are commercial tools for leak hunting for Windows that plug into the VC++ IDE. (My memory says “Purify” — which started out as a tool for commercial Unix — but I could be off base as I don't develop on Windows.) I don't know of any zero-cost ones. – Donal Fellows May 15 '11 at 10:32
  • I do have "Intel Parallel Inspector" which is a memory leak detector. However, it doesn't seem to find it. – ronag May 15 '11 at 10:34
1

Use the CRT memory leak reporting functionality, if you can run your application with the debug CRT.

The debug CRT tracks your allocations and can tell you where memory is leaking when the application exits. One thing to keep in mind is that in order to use this approach, you have to ensure that all resources are cleaned up before exiting the main function, otherwise they will be reported as leaks.

Details here http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx.

If this doesn't find any leaks, I would suggest letting your program run for a few hours and checking if there's an upper limit on how much memory it can use; it may not be a leak.

Collin Dauphinee
  • 13,664
  • 1
  • 40
  • 71
  • The problem is that my application does not leak when it exits. It leaks somewhere when its running. Then at exit everything is freed. – ronag May 15 '11 at 10:49
  • Depends on how you define leak. In my case it allocates all available memory and throws either a bad_alloc or access violation after a few seconds. – ronag May 15 '11 at 10:54
  • 1
    IIRC you do not need to wait for your application to exit, but you can place a call to _CrtDumpMemoryLeaks at any point in your application where you think a leak may have happened. It's been a while since I used it though. – Steve Fallows May 15 '11 at 10:56
  • 1
    You can still use this functionality, it's just a bit more work. You'll have to call `_CrtDumpMemoryLeaks` when you know your application has leaked a lot and analyze the output for suspicious entries. Make sure you also define `_CRTDBG_MAP_ALLOC`, so you can see where the allocations occur. Aside from this, I don't think you have many options. – Collin Dauphinee May 15 '11 at 10:59
  • I've tried using memcheckpoint, memdiff, memdump. However, I'm getting suspicious results (it says that barely any memory have been allocated). I guess I will have to do some more research on those functions. – ronag May 15 '11 at 11:14
  • Make sure that you aren't leaking kernel objects (handles). You can use Application Verifier to check for this, I think. – Collin Dauphinee May 15 '11 at 13:23