2

I want Dipose cache in window phone 7. In my project I have download Images, after use I Dispose Image by:

Image.source = null;
Image = null;

but memory does not return to its original, for example:

After download I set source of image :

img.source = new bitmapImage(new uri("http://diadiem.com/image/123.jpg"),UriKind.Relative);

When next Page or LoadPage Again: I want clear item old item, and Replease memory in cache. Although I tried set image.source= null, and set control Image = null. But memory does not return to its original.

Please help me!

chue x
  • 18,573
  • 7
  • 56
  • 70
thongaduka
  • 627
  • 1
  • 8
  • 19
  • so do you mean 123.jpg on the server is a different value second time and still showing the original image? – Mo Valipour Aug 15 '11 at 08:16
  • http://diadiem.com/image/123.jpg , it's link temp ! i use link image http. When download , image save in cache, beacause when i Disconnect internet and load page (using the original link) image alway show ? i want dispose or Replease it in cache ? – thongaduka Aug 15 '11 at 08:19
  • Possible duplicate: http://stackoverflow.com/questions/7023879/how-does-one-release-cache-of-application-in-windows-phone-7 – Jamie Keeling Aug 15 '11 at 11:42

2 Answers2

6

To remove a downloaded image from the cache you need to assign it's source to a separate BitmapImage and set that to null before setting the image's source to null.

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

Don't ask me why, but it works.

chue x
  • 18,573
  • 7
  • 56
  • 70
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • Thank Matt Lacey ! I did it your way, but the memory can not return to its original condition. For example: Initially Memory App is 5.412 Mb, after I add an image to be 6612 Mb. I did it your way, but the memory is not reduced.after remove Image memory app is 5.516 ? I used GC.Collect () and GC.SuppressFinalize (image1), but the issue remains unresolved – thongaduka Aug 16 '11 at 01:52
  • @thongaduka If memory is going down after you remove the image then it's working. It's very likely that other things are going on in the app which will affect total memory consumption. I've used this technique to load and then unload hundreds of large (full screen size) images within an app and this stays under the memory limit and keeps memory at about the same level. – Matt Lacey Aug 16 '11 at 08:32
  • Thank you very much ! In my app , i use webservices and load data from services. when next Page i load data again , and i clear all itemControl before add new items to mylistview( stackPannel + scrollview). memory is going down but the memory can not return to its original condition. I tried to clean the memory, but my app can not work more than an hour ? You can help me ? Thank you very much – thongaduka Aug 17 '11 at 04:30
  • @thongaduka without access to your code I can only recommend that you use a profiling tool to ubderstad what's going on – Matt Lacey Aug 17 '11 at 08:02
1

What you can do to enforce system to load image always from the URL is to use the following inception:

img.source = new bitmapImage(new uri("http://diadiem.com/image/123.jpg?random=" + randomvar)...

Where randomvar is a random variable like GUID.NewGuid()

chue x
  • 18,573
  • 7
  • 56
  • 70
Mo Valipour
  • 13,286
  • 12
  • 61
  • 87
  • thank valipour ! But I want to know where the image is saved when I assign a URL to the image, and I want to destroy the image at the location automatically, so that my app is not running out of memory – thongaduka Aug 15 '11 at 09:53
  • I doubt you really need to do this. If you monitor metrics provided when you are debugging your app, you will notice that system clears memory very well shortly after your ui elements are disposed from the page. – Mo Valipour Aug 15 '11 at 10:46
  • This method works around caching of the image when it is downloaded. The question is about reclaiming memory from images after they've been downloaded. If you are downloading lots of images this is necessary to stay within the memory requirements/limitations of the platform. – Matt Lacey Aug 15 '11 at 11:48