Why does Silverlight leak memory when using COM?

We found this problem when placing an inherited COM component in our Out Of Browser Silverlight application, believing that this is a problem with our COM component.

However, narrowing it down to the placement of the most basic COM component that you could imagine could still have a memory leak. This COM component used for testing is written in .NET and simply sends events back to the Silverlight application every time the timer fires. Each event contains only one row.

As the Silverlight application starts, the memory usage of the process continues to grow. Profilers do not show an increase in managed memory, which indicates a leak in the implementation of the Silverlight / COM runtime.

Has anyone else seen this problem, and if so, could you get around it?

Edit: The Repro project is now available at http://bitbucket.org/freed/silverlight-com-leak

+7
memory-leaks silverlight com
source share
4 answers

Looking at your code, the line you pass back and forth (11 characters + zero termination) = 24 bytes in Unicode. COM automation uses BSTR, which adds 4 bytes for the leading pointer (32-bit), and you multiply it by 10000, i.e. 10000 * 28 = 280,000 bytes.

This means that every milliseconds (the timer value is 1) you will allocate a lot of memory, and in .NET 2800 bytes are likely to be allocated to a bunch of large objects (> 85000 bytes). The result of an impact on LOH in most cases ... memory problems, as here, for example: Fragmentation of large objects

This might be something you should check out. One simple thing to test is to reduce the size of your BigMessage. You can also dive in depth using WinDBG: http://blogs.msdn.com/b/tess/archive/2008/08/21/debugging-silverlight-applications-with-windbg-and-sos-dll.aspx and check what really happens under the covers.

+1
source share

Make sure the COM component frees up any lines it allocates.

0
source share

Not familiar with Silverlight, but another possible cause of interaction headaches is event handling: http://www.codeproject.com/KB/cs/LingeringCOMObjects.aspx

0
source share

Could it be that you have your own resources, rather than garbage collection? Perhaps this is one of the few cases where calling GC.Collect can be beneficial. Interesting reading here

Just for the test you can call GC.Collect a couple (or even three times) and see what happens (I can’t believe that I really offer this ...).

0
source share

All Articles