0

How to reduce the memory usage in my C# Windows Phone apps?

Some instances:

1). To use the method: LockScreen.GetImageUri()

I can either add using Windows.Phone.System.UserProfile; in the top of the cs file. or add the prefix Windows.Phone.System.UserProfile. in front of it, so that's Windows.Phone.System.UserProfile.LockScreen.GetImageUri()

Which one will use less memory?

2). Consider the scope of the variables, will it release memory more frequently if I break my method into multiple pieces, and run them one by one?

e.g. I need to render some images use WriteableBitmap, each might consume 1MB memory, if I have 10 or more images to render, it might exceed the memory limit soon.

Will it help if I render them in different methods?

3). Which is the better choice: Static or Non-Static?

It seems the static object will persist in the memory whenever the app is "alive" or "running", however, to use a non-static method we need to create an instance of it, which will consume the memory each time we do so (isn't it?).

ADD: If I create an instance of a class object, can I "Dispose" it anyway?

A special case: To use the IsolatedStorageSettings.ApplicationSettings;

I can either use it like:

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
if (!settings.Contains("IconSet"))
{
    settings["IconSet"] = "Set1";
}

or I can also use

if (!IsolatedStorageSettings.ApplicationSettings.Contains("IconSet"))
{
    IsolatedStorageSettings.ApplicationSettings["IconSet"] = "Set1";
}

Any difference? (regarding to the memory usage)

4). Deployment.Current.Dispatcher.BeginInvoke(() =>{})

Will this method release the memory it used at all? Or do I need any special method to release the memory manually? Such as EndInvoke()?

Max Meng
  • 147
  • 10

1 Answers1

0
  1. No difference.

  2. Objects like images that use large amounts of memory usually implement IDispose. To ensure that memory is released when it is no longer needed you should be calling dispose on objects when you no longer need the memory. Personally I find an easier way to manage this, is rather than explicitly calling Dispose on an object, wrap it in a using statement e.g.

    using(Image myImage = new Image(Myfile)) { // render myImage }

  3. People will say declare everything static as it is quicker. Personally I think when you try and declare everything static you're losing all the benefits of .NET as an OO language. Unless performance is absolutely critical, I discourage excessive use of static as in most instances the benefits are negligible and the compromises to your code structure are considerable.

  4. EndInvoke() does not release memory

To answer Max Meng...

There is more than one Image class in the .NET framework. e.g.

http://msdn.microsoft.com/en-us/library/system.drawing.image(v=vs.110).aspx

WPF controls do not implement IDispose. Controls are owned by their parent controls. To dispose memory associated with the control call close on the page when you are done with it.

What is the correct way to dispose of a WPF window?

If IDispose is not implemented in a .NET Framework class that implies either there are no unmanaged or substantial resources associated with the class or alternatively that some other component is responsible for managing its resources.

Community
  • 1
  • 1
Mick
  • 6,527
  • 4
  • 52
  • 67
  • To add to this, remember that the GC can't reclaim objects if you still refer to them. If you have some instance variable holding onto transient objects, make sure to null them out when they're not being used so that the garbage collector can reclaim them. – antiduh Dec 30 '13 at 04:37
  • @Mick, will try the using block, thanks for that. Any idea about the memory usage of the BeginInvoke method? – Max Meng Dec 30 '13 at 05:17
  • @antiduh, it seems the GC only run periodically‎, when my app will only run the task in less then 5 seconds, it seems I cannot count on it? – Max Meng Dec 30 '13 at 05:22
  • The gc should run also when there is memory pressure. – antiduh Dec 30 '13 at 05:24
  • @Mick, forgive my ignorance, it seems the using block doesn't work for the Image object? It says `'System.Windows.Controls.Image': type used in a using statement must be implicitly convertible to 'System.IDisposable'` What am I missing? – Max Meng Dec 30 '13 at 07:58
  • The answer to this comment was a little longer than I could fit, so I've answered above – Mick Dec 31 '13 at 05:01