2

I am having a horrible time with the automatic image caching by WP7 platform.

I made a very simple app (I mean real simple). They have 2 images already added in solution as content of pix dimension 1280 x 2000 Here is XAML

<Grid x:Name="LayoutRoot" Background="Transparent" ManipulationCompleted="ImageHolder_ManipulationCompleted">
    <Image x:Name="ImageHolder" />
    <TextBlock x:Name="MemoryUsage" />
</Grid>

My .cs

   ImageHolder.Source = null;
        if (i % 2 == 0)
            ImageHolder.Source = new BitmapImage(new Uri("image002.jpg", UriKind.Relative));
        else
            ImageHolder.Source = new BitmapImage(new Uri("image001.jpg", UriKind.Relative));
        i++;

   MemoryUsage.Text = "Device Total Memory = " + (long)DeviceExtendedProperties.GetValue("DeviceTotalMemory") / (1024 * 1024)
            + "\nCurrent Memory Usage = " + (long)DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage") / (1024 * 1024)
            + "\nPeak Memory Usage = " + (long)DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage") / (1024 * 1024);

Memory usage is very high is equal to the 2 images in raw bitmap size, though there should only be one such instance. Please help, I am in dire need.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Master Chief
  • 2,520
  • 19
  • 28

1 Answers1

1

First of all resize your image to the correct size. There is no point to have such high resolution if the screen doesn't support it. Also make sure the build action for the image is "Content" otherwise all your images will be loaded to memory at start up. You might still see high memory usage because there is no garantee GC will dispose the image immediately but sooner or later it will.

Andras Csehi
  • 4,305
  • 1
  • 28
  • 36
  • actually this was my test app, I created another test app which basically does the same thing, but cycles with many images like 30-40. But that test app crahes after sometime. I will resize the images in final build of my main app, I am concerned because if this is not rectified it will creep into main app, where problem may not apppear instantly because, images are small but will eventually run out of memory. Already tested in 256MB emulator. – Master Chief Nov 17 '12 at 11:24
  • If you change the images frequently you can try to call GC manually after you change the picture. Just call GC.Collect(); Make sure you resize the images to 100% ratio, that will save you tons of memory and also increase the overall performance as there won't be any need to resize at runtime. – Andras Csehi Nov 17 '12 at 11:30
  • yeah tried that, but it doesn't guarantee that GC will kick-in, and in my case it doesn't. – Master Chief Nov 17 '12 at 11:41
  • And actually this test app is result of another test app. Actually as it turns out image caching is WP7's problem and there's a work around, but still I am facing caching some other thing.Check this out http://stackoverflow.com/questions/13355496/cannot-find-the-memory-leak I created this question because there were not many reponse, so I am trying to drill down to problem. If you could help me there... – Master Chief Nov 17 '12 at 11:44