This is a mirror of official site: http://jasper-net.blogspot.com/

Out of memory errors with the Windows Media Format SDK

| Tuesday, June 8, 2010
Over the last few years I’ve worked with a number of engineers that have reported out of memory errors when encoding high definition (HD) video using the Windows Media Format SDK. This issue typically only occurs on 32 bit systems. As we know on a 32 bit Windows OS the maximum memory that your application can allocate is 2 GB. If we try to allocate more than this we get an out of memory error. Out of memory error can also occur because of heap fragmentation. I’ll go into heap fragmentation in detail below.

The Format SDK will use up to the available memory. Internally the Format SDK creates its own heaps. It carefully monitors the memory usage of its heaps. This careful management of its internal heaps should prevent us from ever reaching the 2 GB limit. We might get close but we should never go over. Unfortunately it’s not a perfect world.

Most encoding applications tend to be rather large by their very nature. These large and complex video encoding and editing applications host the format SDK. With the main footprint of the encoding application taking up in the neighborhood of 500 MB to begin with, it’s easy to see how we could quickly hit the 2 GB limit. The Format SDK doesn’t manage the other heaps in the system such as the CRT heap. Because of this the other heaps grow and shrink dynamically. The Format SDK checks to see how much memory is available before it starts encoding. Allocates the heaps and then starts the encoding process.

When dealing with HD video, the amount of memory that must be allocated is substantial. Most people don’t realize that due to the “look ahead, look behind” nature of the VC 1 codec, more than one frame of data must be held in memory at a time. Depending on the encoding settings we can sometimes hold seconds of data in memory. Keep in mind that this is uncompressed data in addition to compressed data. As I’m sure you are aware an uncompressed HD video frame is on the order of 8 MB or more. If we have 5 seconds of cached data at 30 frames per second we’re looking at 1.2 GB. If our application footprint is already at 500 MB we only have 200 MB to work with. This doesn’t offer much breathing room. Factor in heap fragmentation and we start seeing out of memory errors.

Read more: Windows Media and DirectShow tips, gotchas and code

Posted via email from jasper22's posterous

0 comments: