I was recently encountered by a terryfying windows phone 7 memory problem, i was creating an app in which images were downloaded from the internet. On first screen there were around 16 images of sizes upto 300KB max. But eventually at the end of rendering the memory taken up by the app was 80 to 90 MB. This was surprisingly aweful for me, as Microsoft will refuse to certify your app if you exceed the memory limits.  Here’s the relevant section of the certification guidelines.

5.2.5  Memory Consumption      An application must not exceed 90 MB of RAM usage, except on devices that have more than 256 MB of memory. You can use the DeviceExtendedProperties class to query the amount of memory that is available on the device and modify the application behavior at runtime to take advantage of additional memory.

What to do, What to do, thoughts… The dilema was that on the very next page, i was going to download even larger images, and that racks up the memory to 270+ MB.. and eventually “Out of memory” exception. Other big problem is that when wp7 page navigate to other page, it keeps the previous page’s memory intact, I definately need to wipe out memory when it moves to next page.

I tried with GC.Collect() making null every Webclient object, even the control which had images i set that to null but the problem persisted. I was using a BitmapImage object, i set the object to null as well and GC.Collect, but that did’nt do it either.

Then i came to some heaven blog and found a solution that, setting the BitmapObject to null would’nt do it. But  if i set the source of the bitmap Object to null, that will sort things out.

bitmapImage.ImageSource =null;
This cleared the image cached into Bitmap Objects.. and the app was memory happy again. I hope this helps all the guys who are banging there heads on windows phone 7.