Recently Rich & I've had problems with adding items to list control where the data template is chocked full of XAML goodies - triggers, opacity masks, images, overlay, loads of things I know little about... ;) The culmination of all this UI goodness when used with a bound list control is the blowing of the '90 mb limit' for WP7 devices. As I posted before, we knew the problem wasn't with the data model so we knew it was the data template, now there is plenty of coverage of issues with data templates in Silverlight and this post it not adding to this list, its about a technique to reduce the memory footprint once you've done as much as you can with the data template.
So we're developing an app which exhibits high memory usage, now obviously we want to reduce the memory consumption for the app so we trimmed down the data template for the list control, but we're still seeing the 90 mb limit exceeded for relative few items. Our data set could in theory contain several hundred items but it was failing for less than 40 items and this is an issue.
So Rich can up with the idea of adding a delay between each add to the bound collection - trickle the items, so after some experimentation we discovered the use of a call to the garbage collector when all the items have been added along with a delayed between each item reduced the memory usage to an acceptable level.
The initial implementation Rich came up with used a DispatcherTimer and I thought I could improve on this with Rx (reactive extensions) - this didn't work out (Rich - I won't try and be smart again). Simply the Rx added to much overhead and this caused the rendering to be less smooth than the DispatcherTimer implementation. So we went back to the DispatcherTimer.
We've provider the implementation in the WP7Contrib, there are currently 2 implementations:
Both classes implement the interface ITrickleToCollection<T> via the base class BaseTrickleToCollection<T>. It defines a set of methods that allow the starting, pausing, resuming and stopping of trickling as required. The start method defined the time delay between each add as well the source collection and the destination collection.
Both classes use a functional style via the Action method to notify the caller when an action occurs, e.g. when the trickling has started, stopped, item added etc. Shown below is the constructor signatures for both implementations:
Each class has a specific implementation for the DispatcherTimer, shown below is the implementation for the TrickleAllToCollection<T>, the interesting detail is the timer being shutdown once trickling all of the items has completed - no point leaving it running consuming device resources (memory & CPU).
Using these classes is simply, just instantiate an instance with the required Action methods:
And then start trickling to the bound destination collection:
That's pretty much it, as I said these are in the WP7Contrib and can be found in the Collections project. I've also created a demo application 'TrickleDemo' in the Spikes directory of the code base. This demo doesn't show any memory problems with data templates, it only demonstrates how to use this trickle to collection pattern.
Next time a post about how we do RESTful communication in WP7 using the HttpWebRequest & Reactive Extensions...
So we're developing an app which exhibits high memory usage, now obviously we want to reduce the memory consumption for the app so we trimmed down the data template for the list control, but we're still seeing the 90 mb limit exceeded for relative few items. Our data set could in theory contain several hundred items but it was failing for less than 40 items and this is an issue.
So Rich can up with the idea of adding a delay between each add to the bound collection - trickle the items, so after some experimentation we discovered the use of a call to the garbage collector when all the items have been added along with a delayed between each item reduced the memory usage to an acceptable level.
The initial implementation Rich came up with used a DispatcherTimer and I thought I could improve on this with Rx (reactive extensions) - this didn't work out (Rich - I won't try and be smart again). Simply the Rx added to much overhead and this caused the rendering to be less smooth than the DispatcherTimer implementation. So we went back to the DispatcherTimer.
We've provider the implementation in the WP7Contrib, there are currently 2 implementations:
- TrickleUniqueToCollection<T> - Only adds items not already in the collection,
- TrickleAllToCollection<T> - adds all items irrespective if they already exist in the collection.
Both classes implement the interface ITrickleToCollection<T> via the base class BaseTrickleToCollection<T>. It defines a set of methods that allow the starting, pausing, resuming and stopping of trickling as required. The start method defined the time delay between each add as well the source collection and the destination collection.
public interface ITrickleToCollection<T>
{
bool Pending { get; }
bool IsTrickling { get; }
void Start(int trickleDelay, IEnumerable<T> sourceCollection, IList<T> destinationCollection);
void Stop();
void Suspend();
void Resume();
}
Both classes use a functional style via the Action method to notify the caller when an action occurs, e.g. when the trickling has started, stopped, item added etc. Shown below is the constructor signatures for both implementations:
public sealed class TrickleUniqueToCollection<T> : BaseTrickleToCollection<T>
{
public TrickleUniqueToCollection(Action addedAction, Action completedAction)
: this (addedAction, () => {}, () => {}, () => {}, () => {}, completedAction)
public TrickleUniqueToCollection(Action addedAction, Action startedAction, Action stoppedAction,
Action suspendedAction, Action resumedAction, Action completedAction)
: base (addedAction, startedAction, stoppedAction, suspendedAction, resumedAction, completedAction)
}
public sealed class TrickleAllToCollection<T> : BaseTrickleToCollection<T>
{
public TrickleAllToCollection(Action addedAction, Action completedAction)
: this (addedAction, () => {}, () => {}, () => {}, () => {}, completedAction)
public TrickleUniqueToCollection(Action addedAction, Action startedAction, Action stoppedAction,
Action suspendedAction, Action resumedAction, Action completedAction)
: base (addedAction, startedAction, stoppedAction, suspendedAction, resumedAction, completedAction)
}
Each class has a specific implementation for the DispatcherTimer, shown below is the implementation for the TrickleAllToCollection<T>, the interesting detail is the timer being shutdown once trickling all of the items has completed - no point leaving it running consuming device resources (memory & CPU).
public TrickleAllToCollection(Action addedAction, Action startedAction, Action stoppedAction,
Action suspendedAction, Action resumedAction, Action completedAction)
: base (addedAction, startedAction, stoppedAction, suspendedAction, resumedAction, completedAction)
{
this.DispatcherTimer = new DispatcherTimer();
this.DispatcherTimer.Interval = this.StartupDelay;
this.DispatcherTimer.Tick += delegate
{
if (this.DispatcherTimer.IsEnabled)
{
if (this.Source.Count != 0)
{
var item = this.Source.Dequeue();
this.Destination.Add(item);
this.AddedAction();
if (this.Count == 0)
this.DispatcherTimer.Interval = this.Delay;
this.Count++;
}
else
{
this.DispatcherTimer.Stop();
this.CompletedAction();
}
}
};
}
Using these classes is simply, just instantiate an instance with the required Action methods:
this.trickler = new TrickleUniqueToCollection<Property>(() => this.UpdateTrickleStatus(TrickleStatus.Added),
() => this.UpdateTrickleStatus(TrickleStatus.Started),
() => this.UpdateTrickleStatus(TrickleStatus.Stopped),
() => this.UpdateTrickleStatus(TrickleStatus.Suspended),
() => this.UpdateTrickleStatus(TrickleStatus.Resumed),
() => this.UpdateTrickleStatus(TrickleStatus.Completed));
And then start trickling to the bound destination collection:
this.trickler.Start(432, result.Properties, this.aggregatedPropertiesView);
That's pretty much it, as I said these are in the WP7Contrib and can be found in the Collections project. I've also created a demo application 'TrickleDemo' in the Spikes directory of the code base. This demo doesn't show any memory problems with data templates, it only demonstrates how to use this trickle to collection pattern.
Next time a post about how we do RESTful communication in WP7 using the HttpWebRequest & Reactive Extensions...
Comments
Post a Comment