Windows Support Number

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Monday, 31 January 2011

WP7: Know your data

Posted on 06:01 by Unknown
If you've been developing Windows Phone 7 apps you'll be aware of the '90 MB' memory limit guideline detailed in the application certification requirements, if not it says:

5.2.5 Memory Consumption An application must not exceed 90 MB of RAM usage, except on devices that have more than 256 MB of memory. You can use the DeviceExtendedProperties class to query the amount of memory that is available on the device and modify the application behavior at runtime to take advantage of additional memory. For more information, see the DeviceExtendedProperties class in MSDN.

I've heard of apps exceeding this limit and still being approved for the app store, but that is not the point of this post, the point is as a developer working on a device you should have a cursory knowledge of the size of your data. The reason is simple because when you exceed the 90 Mb limit (and in all probability you will) you'll be able to identify where your memory problems are quicker and more efficiently.

Rich & I are building a couple of applications for the platform as well as getting WP7Contrib up & running and we've run into issues with the memory limit. We know we are because we're using the excellent memory diagnostics helper from Peter Torr. The problem is we're exceeding the limit with what appeared to be a very limited amount of data (a results set of 40 items). Now we've a good idea of where the problem is - XAML, but we're unable to say with any confidence this was the real cause of our problems. After all the application involves calling back end services, getting the data back, transforming the data, caching the data as well as pushing it into observable collections and binding to the UI. So whilst Rich had a look at the XAML, data templates and ways to lazy load items and images I concentrated on analyzing our data model.

How was I going analyze the memory usage of our data model?

A quick search highlighted the issue there doesn't appear to a memory profiler out there for WP7 - I could be wrong! So next I thought why not and try measure the 'size of' the model at runtime, but how do you measure the size of a reference type? Simply you can't - 'sizeOf' is for value types only and 'Marshal.SizeOf' only shows the unmanaged size after marshalling, not the actual size in memory.

I then thought I don't want an exact figure just an indicative idea of the size, I could use serialization - serialise the model to a byte array and display the length, not perfect but okay.

The following are steps I used to to measure memory size of our model.

1. Create an observable collection to hold the instance of the model,
2. Serialise the empty observable collection and get the length of the array,
2. Get current application memory usage via the DeviceExtendedProperties methods,
3. Make request to back end service and result to observable collection,
4. Serialise the updated observable collection and get the length of the array,
5. Calculate the average size of a serialized collection item by dividing the length by the number of items in the collection,
6. Call GC.Collect - clear down any freed memory (at least request a sweep),
7. Get current application memory usage again and calculate delta.

Note: I didn't bind the observable collection to the UI, I purely wanted to measure the size of the model when contained in an observable collection.

I then took the results of these steps and displayed them on a simple UI.



The import information to take from the screenshot:

Number of items in the collection = 378
Serialized size of collection = 346,538 Bytes
Average size of serialized item = 916 Bytes

The current memory usage = 12,865,536 Bytes

You can also see the output from Peter Torr's memory diagnostic helper at the right hand side of the screen - 11,512 KBytes.

So from these figures I am able to deduce the model isn't causing our application to directly exceed the 90 MB limit and we've gained a greater understanding our data model - we now know our data model.

Note: We would never try and display so many items on a device, it's just a test...
Read More
Posted in Performance, WP7, WP7Contrib, WP7Dev | No comments

Sunday, 30 January 2011

WP7Contrib: Thread safe ObservableCollection<T>

Posted on 06:22 by Unknown
Continuing with the introduction of WP7Contrib concepts, patterns & services from my previous post I thought I would explain why we have chosen to implement our own ObservableCollection.

As with the desktop version of Silverlight the idea of a 'blocking call' on the UI thread is discouraged, to point where if you do block the UI thread on a WP7 device your application could be automatically torn down - bad user experience. So to get round the issue the Silverlight framework makes use of the standard .Net 'async pattern' - IAsyncResult interface. Examples of this are the HttpWebRequest class and it's counterpart HttpWebResponse, they allow you to execute web requests without blocking the UI thread.

The problem with the ObservableCollection<T> supplied by the framework is it's not thread safe for any methods. For example, when you have a control bound to the collection and it's enumerating over the collection and another thread modifies the collection you'll receive the exception - "Collection was modified; enumeration operation may not execute.". It could be said this is an oversight of the implementation but I wouldn't go that far, the idea of copying the internal list for every call to GetEnumerator could be an expensive time & memory operation.

We required a thread safe version of an ObservableCollection<T> so that meant rolling our own. The first thing we defined where the interfaces we needed, the obvious ones being the INotifyCollectionChanged & INotifyPropertyChanged.

/// <summary>
/// Marker interface for the observable collection.
/// </summary>
public interface IObservableCollection
{
}

/// <summary>
/// Generic interface for observable collection
/// </summary>
/// <typeparam name="T">The type being used for binding.
/// </typeparam>
public interface IObservableCollection<T> : IList<T>, IObservableCollection, INotifyCollectionChanged, INotifyPropertyChanged
{
/// <summary>
/// Adds a range of items to the observable collection.
/// </summary>
/// <param name="items">
/// The items to be added to the observable collection.
/// </param>
void AddRange(IEnumerable<T> items);
}

The implementation of these interfaces are the ObservableCollection we developed.

We use the composition principle to store the items of collection internally in a List<T> implementation.

/// <summary>
/// Thread safe observable collection
/// </summary>
/// <typeparam name="T">The type being used for binding.
/// </typeparam>
public sealed class ObservableCollection<T> : BaseCollectionModel, IObservableCollection<T>
{
/// <summary>
/// The internal collection.
/// </summary>
private readonly IList<T> collection = new List<T>();

...
...
...
}

Synchronisation to all methods which could modify or enumerate the items is done using a Monitor critical section exposed via the 'lock' method.

Add Method:

/// <summary>
/// Adds an item to the collection, raises a RaiseCollectionChanged event with a NotifyCollectionChangedAction.Add
/// parameter.
/// </summary>
/// <param name="item">
/// The item being added.
/// </param>
public void Add(T item)
{
int index;

lock (this.sync)
{
this.collection.Add(item);
index = this.collection.IndexOf(item);
}

this.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
}

Remove Method:

/// <summary>
/// Removes an item from the collection. If the item does not exist in the collection the false is returned.
/// </summary>
/// <param name="item">
/// The item to be removed from the collection
/// </param>
/// <returns>
/// Returns the result of removing an item from the collection, if the item existed it returns true else false.
/// </returns>
public bool Remove(T item)
{
int index;
bool result;

lock (this.sync)
{
index = this.collection.IndexOf(item);
if (index == -1)
{
return false;
}
result = this.collection.Remove(item);
}

if (result)
{
this.RaiseCollectionChanged(NotifyCollectionChangedAction.Remove, item, index);
}

return result;
}

Clear Method:

/// <summary>
/// Clears the contents of the collection, raises a RaiseCollectionChanged event with a NotifyCollectionChangedAction.Reset
/// </summary>
public void Clear()
{
lock (this.sync)
{
this.collection.Clear();
}

this.RaiseCollectionChanged(NotifyCollectionChangedAction.Reset);
}

You will notice from the above code we don't fire the events to notify observers the collection has changed from inside the critical section - the observers are notified in a sequential manner and therefore doing this inside of the critical section would mean holding the lock for a longer period and we are trying to reduce this time to a minimum.

I mentioned above a way to get around the problem of enumerating the collection whilst another thread makes modifications, we do make use of this pattern, a copy is made for each call to the either of the GetEnumerator methods. We took the view that the collection would never contain 'to many' items that making a copy of the list would be an issue - we don't envisage have 1000's of items, what would be the point on a WP7 device.

The last major issue to consider when implementing the collection was - do we want to support the firing the events on the UI thread via the 'Dispatcher.BeginInvoke' method. We took the view this is not the responibility of the collection, it's the responsibility of the host application to make sure they are on the correct thread (if required). The responsibility of collection is to maintain and update items and notify observers, not to manage any constraints of the UI threading model - Single Responsibility Principle.

The full implementation can be found in the WP7Contrib.Collections project on CodePlex.

Read More
Posted in WP7 WP7Contrib CodePlex ObservableCollection | No comments

Monday, 24 January 2011

WP7Contrib: Network Connectivity Push Model

Posted on 15:23 by Unknown
To start my WP7Contrib blogging contribution I thought I started with something small, easy and relatively quick to explain. If you're familiar with the platform you can get the current network type using the API - NetworkInterface.NetworkInterfaceType. This is a simple synchronous call which returns what MSDN describes as 'the type of the network servicing Internet requests. This is a prime example of a 'pull model' to obtain data.
We've turned this model on its head, and produced a 'push model' using the MS Reactive Extensions for .Net.

What we did was use an observable sequence which returns values at a specified frequency using the interval method.

this.networkObserver = Observable.Interval(TimeSpan.FromMilliseconds(frequency))
.Select(DetermineCurrentStatus)
.Subscribe(this.statusSubject);

So for every interval we execute the Select and the returned value from DetermineCurrentStatus is pushed to the statusSubject. This is a behavior subject and this is specifically designed to store a value that changes over time.
We then expose this via an instance of the interface IObservable and use the DistinctUntilChanged to return the last distinct value.

public IObservable Status()
{
return this.statusSubject.DistinctUntilChanged();
}

The implementation can be found in the NetworkMonitor class, in the WP7Contrib.Communications assembly.

I have shown an extract of a quick example application called 'Network Monitor Demo' below (the code can be found here.). I've done this in the "code behind" for simplicity so there are no distractions of view models or DI etc.
In this example I display the last type and the type history overtime, see screenshot below - click 'Start' to see the NetworkMonitor in action.





public partial class MainPage : PhoneApplicationPage
{
private readonly INetworkService networkService;
private readonly IObservableCollection<TypeHistory> history = new ObservableCollection<TypeHistory>();
private IDisposable statusObserver;
// Constructor
public MainPage()
{
InitializeComponent();
this.networkService = new NetworkMonitor(10000);
this.typeHistory.ItemsSource = this.history;
}
private void start_Click(object sender, RoutedEventArgs e)
{
if (this.statusObserver != null)
return;

this.statusObserver = this.networkService.Status()
.ObserveOnDispatcher()
.Subscribe(type =>
{
this.lastType.Text = type.ToString();
this.history.Add(new TypeHistory(type.ToString(), DateTime.Now));
});
}
private void stop_Click(object sender, RoutedEventArgs e)
{
if (this.statusObserver == null)
return;

this.statusObserver.Dispose();
this.statusObserver = null;
}
}

public class TypeHistory
{
public string Type { get; private set; }
public DateTime Time { get; private set; }
public TypeHistory(string type, DateTime time)
{
this.Type = type;
this.Time = time;
}

public override string ToString()
{
return string.Format("{0} - {1}", this.Time.ToUniversalTime(), this.Type);
}
}

And that pretty much covers our push model for network connectivity, thanks for getting this far :)
If you're wondering how I did the screenshot from the phone check out Laurent Bugnion post on the subject.

Read More
Posted in | No comments

Sunday, 23 January 2011

WP7Contrib...

Posted on 10:20 by Unknown
Just wanted to announce the go live of WP7Contrib - a community effort based around the MS Window Phone 7 Platform.

Rich Griffin and I have spent the last couple of months bring together code snippets and patterns into a cohensive contribution library, we're currently developing an app using the framework and will be giving more details and examples over the next couple of weeks.
Read More
Posted in WP7 WP7Contrib CodePlex | No comments
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

Popular Posts

  • Unit testing Rx methods Timeout & Retry with moq
    Earlier this week I was trying to unit test an asynchronous service (Foo) which used another asynchronous service (Bar) internally and ran i...
  • Understanding RefCount in Reactive Extensions
    A couple of weeks ago  @LordHanson  & I ran into an issue converting a stateless async service exposed as an Rx cold observable to a  co...
  • StructureMap: ILifecycle
    The other day I wanted to control the scope of a service inside a web based app with semantics which didn't fit either 'HttpContextS...
  • MVVM anti-pattern: Injecting the IoC container into a View Model
    This is another anti-pattern I've seen a lot recently, the dynamic use of the IoC container inside a view model to resolve child view mo...
  • How many pins can Bing Maps handle in a WP7 app - part 1
    part2 -  http://awkwardcoder.blogspot.com/2011/10/how-many-pins-can-bing-maps-handle-in.html part3 -  http://awkwardcoder.blogspot.com/2011/...
  • Bad developers love 'The Daily WTF'
    When 'The Daily WTF' started up back in 2003/2004 it was a great laugh looking at shocking code other developers wrote, but after a ...
  • Using CompositeDisposable in base classes
    To help make an object eligible for collection by the GC (garbage collector) one would implement the IDisposable interface. Executing the di...
  • Implementing a busy indicator using a visual overlay in MVVM
    This is a technique we use at work to lock the UI whilst some long running process is happening - preventing the user clicking on stuff whil...
  • Daily Dilbert Service - the most important service I've ever written...
    NuGet package available here ... First off a big shout to  @hamish  &  @leeoades  on this one - I'm just blogging about it. At work ...
  • Comparing performance of .Net 4.5 to .Net 4.0 for WPF
    Currently I'm working on a .Net 4.0 WPF app and we've had some discussion about moving to .Net 4.5, we don't get to make the dec...

Categories

  • .Net
  • .Net 4.5
  • Abstractions
  • Advertising
  • Agile
  • Agile Courage
  • AOP
  • Async
  • automated testing
  • Azure
  • Azure IIS RESTful development
  • BDD
  • Bing Maps
  • Bounded Context
  • C#
  • C# 5.0
  • Caching
  • Chocolatey
  • CLoud
  • CodePlex
  • Coding
  • Coding Building CI Testing
  • Coding C#
  • coding C# IoC StructureMap
  • Coding Functional-Programming
  • Coding REST Knowledge
  • Coding Services
  • Coding TDD Refactoring Agile
  • Command
  • continuous testing
  • coupling
  • CultureInfo
  • DAL
  • databases
  • DDD
  • DDD Coaching
  • DDD Domain Events Auditing nHibernate
  • DDD Entities Value Objects
  • Debugging
  • Design Patterns
  • Design Patterns Databases Auditing
  • Developement
  • Development
  • Development Coding
  • Development Process
  • Development unit testing
  • Development VS 2011
  • Diagnostics
  • Disposable
  • Exceptions
  • FINDaPAD
  • FindaPad Property Rental Windows Phone 7 Mobile Devices
  • Fun Coding Duct-Tape
  • Hotfixes
  • integration testing
  • IoC
  • jasmine
  • javascript
  • Jobs Development
  • LINQ
  • marketplace
  • Mobile Devices
  • Mocking
  • MSDN Coding
  • MSpec
  • Multilingual
  • MVC
  • MVVM
  • nCrunch
  • nHbiernate Repository Pattern Criteria
  • nHibernate Auditing Design Fluent
  • nHibnerate Entities Events Listeners
  • node.js
  • nodes.js
  • Nokia
  • NoSQL RavenDB Azure Development
  • Observations
  • OO
  • ORM
  • Performance
  • Portable Class Library
  • Portable Library
  • PostSharp
  • Process
  • Rants
  • RavenDB IIS 7.5 Development
  • Reactive
  • Reactive Extension
  • Reactive Extensions
  • ReadOnlyCollections
  • Resharper
  • REST Distributed-Systems
  • REST HTTP
  • rest web
  • RESTful
  • Rx
  • Serialization
  • Silverlight
  • Silverlight Installation
  • Task
  • TDD
  • TDD IoC DI
  • TDD Mocking
  • TDD Team Observation
  • Telerik
  • testing
  • threading
  • TPL
  • UI
  • Undo-Redo
  • unit testing
  • ViewModels
  • VS 2012
  • wcf
  • web api
  • Web Services
  • web services mobile devices data
  • WebAPI
  • Windows
  • Windows 8
  • windows phone
  • Windows Phone 7
  • WP7
  • WP7 Bing Maps Development Network HTTP
  • WP7 Bing Maps Development UK Crime
  • WP7 Bing Maps Development UK Crime Clustering
  • WP7 Bing Maps Development UK Polygons Clustering Performance
  • WP7 cryptography bouncy castle
  • WP7 Cultures C#
  • WP7 feedback development app store
  • WP7 Javascript web browser
  • WP7 MSBuild
  • WP7 ORM Databases performance
  • WP7 Serialisation
  • WP7 SilverlightSerializer C#
  • WP7 sqlite performance development
  • WP7 WP7Contrib Bing Maps Development
  • WP7 WP7Contrib Bing Maps Polygon Development
  • WP7 WP7Contrib CodePlex
  • WP7 WP7Contrib CodePlex Bing Maps Development
  • WP7 WP7Contrib CodePlex ObservableCollection
  • WP7 WP7Contrib ILMerge .Net
  • WP7 WP7Contrib Phone Maps
  • WP7 WP7Contrib SilverlightSerializer C#
  • WP7Contrib
  • WP7Contrib Bing Maps WP7
  • WP7Contrib WP7 Geo-Location development C#
  • WP7Contrib WP7 HTTP Compression
  • WP7Contrib WP7 Url Development Rx
  • WP7Dev
  • WPF
  • WPF Cultures
  • WuApi
  • XAML

Blog Archive

  • ►  2013 (16)
    • ►  November (5)
    • ►  September (3)
    • ►  August (1)
    • ►  July (1)
    • ►  June (3)
    • ►  May (2)
    • ►  January (1)
  • ►  2012 (44)
    • ►  November (2)
    • ►  October (8)
    • ►  September (5)
    • ►  August (2)
    • ►  July (4)
    • ►  June (3)
    • ►  May (1)
    • ►  April (2)
    • ►  March (13)
    • ►  February (4)
  • ▼  2011 (52)
    • ►  December (3)
    • ►  November (5)
    • ►  October (7)
    • ►  September (7)
    • ►  August (11)
    • ►  July (4)
    • ►  May (2)
    • ►  April (1)
    • ►  March (5)
    • ►  February (3)
    • ▼  January (4)
      • WP7: Know your data
      • WP7Contrib: Thread safe ObservableCollection<T>
      • WP7Contrib: Network Connectivity Push Model
      • WP7Contrib...
  • ►  2010 (1)
    • ►  August (1)
  • ►  2009 (32)
    • ►  December (3)
    • ►  November (7)
    • ►  October (6)
    • ►  September (11)
    • ►  April (1)
    • ►  March (4)
Powered by Blogger.

About Me

Unknown
View my complete profile