views:

99

answers:

3

Hi:

I'm developing an app for WP7 (VS2010 Express for Windows Phone RTM and WP Emulator), but now im facing a big problem related to memory usage. The app itself has like 12 views, and some of them are reused with different data. It's a newsreader, so the views are mostly listboxes (image thumbnail, stackpanels and textblocks).

The first view has a listbox with 30 items. This takes about 20MB of RAM, but as i navigate between views the current and peak RAM usage start to rise. Well the peak usage it's around 55MB which i think is OK, but the app has a gallery section in which after selecting a thumbnail it navigates to another view which displays a downloaded image (JPEG, 1131px × 1647px, ~486KB) but initially fit to the screen. Until here all good, but for scaling purposes i'm using Laurent Bugnion's Multitouch Behavior and the problem is that when i zoom in the image, the memory usage gets near 90MB (like 87MB last time i tested with the maximum scale size at 2.5).

Also after i navigate the views the current ram usage may permantly reach and stay at 35MB, which i beleive is due to the device caching some things.

So, as the title says, how can i avoid such huge ram usage?

Edit----
Also i'd like to ask if the fact that in my app one can navigate from any view to almost every other and that in between there's always a page transition animation (like the one for the phone list application template in the beta tools), may be contributing to the excessive memory usage.

A: 

Generally speaking you want to keep images as small as feasible (in dimensions and color depth, not necessarily file size). In order to display an image, the device must decompress it to an actual Bitmap, so in this case you're looking at a 1131x1647pixel image, let's say at 16bpp, means that you have a 3.7MB memory footprint for the image, not the 486k file size.

Depending on your zoom/rotate, there may be a second copy buffer, so you can effectively double that. It doesn't take long at that rate to get to 90MB. I'd certainly try either download smaller image files or try resizing them locally and then using the resized image.

ctacke
A: 

A couple of things to note. First, make sure that you are clearing out any lists or images that you are using on pages durring the onNavigatedFrom method. The way WP7 Deals with images is "interesting". One of the things I have found to help is to null out the background of any panorama controls when the page is not being displayed.

As for your specific issue, have you considered using a MultiScaleImage (Deep Zoom) to cut down on memory?

http://msdn.microsoft.com/en-us/library/system.windows.controls.multiscaleimage(VS.95).aspx

Barranger Ridler
do you mean something like mylistbox.itemsource=null; ?
Felipe Guajardo
or may it be better to set the whole datacontext to null?
Felipe Guajardo
I do it more on the ViewModel side and cache the data to Isolated storage when the page is no longer visible
Barranger Ridler
A: 

Well maybe i should look into deepzoom (but when i first read about it, i thought it was for using it with the same image at different sizes, like google maps in satellite view), but yesterday i solved it by using a webbrowser inside my view, so if before i had:

<Image x:Name="imgPlaceHolder" delay:LowProfileImageLoader.UriSource="{Binding Path=ActualImageSource}" MaxHeight="800"
               MaxWidth="480" >
            <interac:Interaction.Behaviors >
                <tbeh:MultiTouchBehavior x:Name="ImageMTB" IsScaleEnabled="True"
                                                 MinimumScale="0.4"
                                                 MaximumScale="2.5"
                                                 IsRotateEnabled="False"
                                                 IsDebugModeActive="False"
                                                 IsTranslateXEnabled="True"
                                                 IsTranslateYEnabled="True"

                                     >

                </tbeh:MultiTouchBehavior>
            </interac:Interaction.Behaviors>
        </Image>

I changed that to:

<phone:WebBrowser Source="{Binding Path=ActualImageSource}" x:Name="wbbigimage"  />

It uses less memory and takes care of the zoom with no problems.

I'll wait a bit for someone with a better solution before checking this one.

Felipe Guajardo
Well, i justo found a problem with this....The webBrowser control, captures the Manipulation events, so one can't do custom actions like firing a method. (like the one a was using to go to next and previous images).
Felipe Guajardo