Silverlight Performance Tip: Set NULL and Clear your Children when Hiding

In the old version, we kept all pages in memory. Then we decided to “hide” unseen pages by offloading them from memory to Isolated Storage.

So we thought, yaayyy, lesser memory…

NOT.

This boggles our mind, and after extensive testing, the following code does reduce the memory footprint when hiding pages:

public void HidePage(int index, bool thumbnail)
{
    if (_visible[index])
    {
        // PREVENT MEMORY LEAK
        foreach (var element in _pages[index].Children)
        {
            if (element is Glyphs)
            {
                var glyph = element as Glyphs;
                glyph.UnicodeString = null;
                glyph.Indices = null;
                glyph.FontUri = null;
            }
            if (element is Path)
            {
                var path = element as Path;
                path.Fill = null;
            }
        }
        _visible[index] = false;
        _pages[index].Children.Clear();
    }
    
    ...
}
Share this post: | | | |
Published Monday, January 11, 2010 4:06 AM by zeddy

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems