13 Comments

Background

Weak references are an easy and effective way to keep your app’s memory usage in order. Wikipedia sums up the idea behind weak references very neatly:

a weak reference is a reference that does not protect the referenced object from collection by a garbage collector. An object referenced only by weak references is considered unreachable (or "weakly reachable") and so may be collected at any time.

Implementation

WP7’s framework contains a System.WeakReference –class which you can use in your application. But keep in mind that you can’t derive from this class because it’s constructor is marked with SecuritySafeCritical-attribute. To get around this limitation and to get a generic version as an added bonus, you can use the following class to implement the WeakReference in your app (I’m sorry but I have lost the original source of the following code):

  public class WeakReference<T>
  {
    public WeakReference(T target)
    {
      reference = new WeakReference(target);
    }

    public bool IsAlive
    {
      get
      {
        return reference.IsAlive;
      }
    }

    public T Target
    {
      get
      {
        return (T)reference.Target;
      }
      set
      {
        reference.Target = value;
      }
    }
  }

Usage

Using the class presented above is easy. Here’s an example of a code where we aren’tusing WeakReference yet:

        protected WriteableBitmap bitmapImage;
        public ImageSource ImageSource
        {
            get
            {
                if (bitmapImage != null)
                {
                    return bitmapImage;
                }

                if (ImageUri != null)
                    ThreadPool.QueueUserWorkItem(DownloadImage, ImageUri);

                return null;
            }
        }

Here’s an updated version where WeakReference is used:

        protected WeakReference<WriteableBitmap> bitmapImage;
        public ImageSource ImageSource
        {
            get
            {
                if (bitmapImage != null)
                {
                    if (bitmapImage.IsAlive)
                    {
                        return bitmapImage.Target;
                    }

                    Debug.WriteLine("Source has been garbage collected. Create new.");
                }

                if (ImageUri != null)
                    ThreadPool.QueueUserWorkItem(DownloadImage, ImageUri);

                return null;
            }
        }

Benefits

Without using the WeakReference-class, the memory usage of Teletext Finland app looks like this after a couple minutes:

image

With the updated version, the memory usage is drastically better:

image

Comments

Comment by XBOX One

XBOX One...

I don't have a clue...

Comment by Louboutin Soldes

Louboutin Soldes...

i would like to be able to i would like to show some gratitude to this specific. this allowed me to considerably using a undertaking i recently finished. at the beginning i actually tried just using simplexml to read a new 400mb xml document and also n...

Comment by doudoune homme moncler

doudoune homme moncler...

Truly, let alone. As if I had formed set up the LUNs accurately, the situation was along with ESXi not necessarily joining effectively from the GUI. I had fashioned to achieve this in order to bracket often the LUNs:...

Comment by FiFa 15 coins

FiFa 15 coins...

I'll take care of it....

Comment by Louboutin soldes

Louboutin soldes...

Once they took away often the playlist attribute it is not necessarily worthy of also $3. Anything at all beneficial on the site can be obtained for free....

Comment by air max 1 pas cher

air max 1 pas cher...

I looked at stumpleaudio and located some new excellent companies, yet Facto air max 1 pas cherrs . stick with Grooveshark for the time being. Individually I do believe it can superior to air max 1 pas cher one of the other out there....

Comment by nike femme solde

nike femme solde...

thanks a lot, what a weird place for your coders to hide that setting up. i got getting some problems with the Dreamhost hardware and i also weren't sure the reason why ends up that they can disable demonstrating undetectable data automatically, whils...

Comment by abercrombie and kent

abercrombie and kent...

Will we be able to stop focus on examined in the event our standing is invisible. I believe such as the data security continues to be frustrated due to this kind computer software =[ Are you aware how?? Thank you beforehand....

Comment by louboutin femme pas cher

louboutin femme pas cher...

Whoa! thnx dailymotion music in addition to related musician! which just about all the things i will need! Big thanks!...

Comment by gamma 11s

gamma 11s...

I have just about everything I could imagine i in addition acquired a pair of some other specialists give it a go. every thing moves fine soon you make an effort to gamma 11s install a suppose COMPUTER ITSELF. Only for kicks we perhaps tested out the i...

Comment by nike pas cher

nike pas cher...

You happen to nike pas cher be rightIbrahim: Search with Yahoo and google that moto nike pas cherrist: winxp_Graphic control set 945GMLit works okay to nike pas chergether with your LG ELECTRONICS R400 lapto nike pas cherp Please assist me to nike pas ...

Comment by timberland boots uk

timberland boots uk...

My favourite an example may be Songza.. really useful for me personally.. Duplicate Bell&Ross 46MM...