Louay Alakkad
Louay Alakkad

Reputation: 7408

how to optimize performance of longlistselector with images on windows phone?

I have a page with a pivot containing about 10 items, each pivot item contains a longlistselector with 30 items each.

Each longlistselector item contains an image.

When I'm browsing the page and I flip to the next pivot item, the app crashes at the 4th pivot item with a memory exception.

Is there anyway to unload undisplayed images in the longlistselector?

Upvotes: 2

Views: 1441

Answers (1)

Lee Gary
Lee Gary

Reputation: 2397

i suggest rethinking your app layout, 10 pivot items is a tad too many for normal user navigation, why not have a main page with a longlistselector of "categories" follow by a details page of 30 images.

But if you absolutely have to do it this way, take a look at microsoft's photohub source

I'm using it personally and have no problem loading hundreds of images in a panoramaItem.

Better still if you can afford the time, do a memory profile and look exactly where the bloat is, sometimes it might be some part of your application that is hogging on to the memory.

Last of all, wp itself caches the images automagically, but not everyone likes it this way (i'm sure you don't)

take a look at this:

Image Caching

This is an important one, and MSDN is currently fairly silent about it. If you were ever wondering why your image memory didn't get released after clearing the Source and removing the Image from the tree, you were most likely seeing Image caching in action. This is an intended performance optimization, to avoid (down)loading and decoding the same image over and over again. Instead we keep a cache in memory that we can easily and quickly reuse. This is not to confuse with the browser cache for downloaded files.

While this is a nice and free performance optimization, at times it can blow your memory unnecessarily, especially when you cycle through many images that you will never come back to. Their cache will use up memory for the lifetime of your app. The good news is that you can delete the cache when you decided that you no longer need it:

BitmapImage bitmapImage = image.Source as BitmapImage;
bitmapImage.UriSource = null;
image.Source = null;

Being smart about this can save you quite a bit of memory usage, which is a precious resource on a phone device. In the sample app, go to the "Caching" page and monitor the memory usage as you show/clear the image. Then check the box and try again. You will see a difference of ~3MB in the example case.

You can get the full article here

Upvotes: 2

Related Questions