none
Silverlight memory usage

    General discussion

  • I work on an application using Silverlight and memory usage is a constant issue. I recently performed some tests using ANTS memory profiler and found that we could run out of memory (or rather, get an out of memory exception) at 1.2GB RAM usage (according to task manager - private bytes), but only 280MB managed memory (As ANTS displays it, memory in all heaps)

     

    So when investigating I started looking at UI cost and created a simple repro (Stick this in the constructor of a new SL5 project's user control):

     

             t.Interval = TimeSpan.FromSeconds(5);
             t.Tick += (o, e) =>
              {
                 for (int i = 0; i < 1000; i++)
                    LayoutRoot.Children.Add(new Button() { Content = "1" });
                 GC.Collect();
              };
             t.Start();
          

    This adds 1000 buttons every 5 seconds.

    Every 5 seconds, memory usage increases by 33MB. That means that 1 Button has a cost of 30Kb (approx) in terms of memory -
    pretty expensive!

     

    A further look into ANTs in this scenario - and it turns out of that 33MB around 3MB is managed - the remainder is unmanaged.

    What is going on in that space?Why is the UI so heavy in terms of memory usage? is this a bug?

     

    WPF doesnt have this issue (adding 1000 buttons ends up with 3MB increases - mostly managed).....

    Monday, March 05, 2012 3:49 AM