Cannot find the memory leak

后端 未结 4 1579
栀梦
栀梦 2020-12-01 19:03

I have been working on a WP7 app, it\'s image gallery app, with basic zooming and flick gestures implemented.

For test purposes I compiled the app with offline image

相关标签:
4条回答
  • 2020-12-01 19:37

    Try this approach: Image downloader with auto memory cleaning. It is advanced KooKiZ's sample, which supports visualization. Sample project is here: https://simca.codeplex.com/

    0 讨论(0)
  • 2020-12-01 19:42

    I have the same kind of app, with the next/previous picture buttons. And I had exactly the same memory leak, which has driven me mad.

    I still haven't been able to find the root cause, but I've managed to bypass it with a ugly hack. When displaying the next picture, I force the old image source to load an invalid picture, thus freeing the memory. I don't understand why removing all references and calling the garbage collector isn't enough, there must be another reference kept internally somewhere.

    Anyway, here is the hack:

    private void DisposeImage(BitmapImage image)
    {
        if (image != null)
        {
            try
            {
                using (var ms = new MemoryStream(new byte[] { 0x0 }))
                {
                    image.SetSource(ms);
                }
            }
            catch (Exception)
            {
            }
        }
    }
    

    You can call it for instance in your RefreshImage method:

    private void RefreshImage()
    {
        BitmapImage image = ImageHolder.Source as BitmapImage;
        ImageHolder.Source = null;
    
        DisposeImage(image);
    
        ImageHolder.Source = new BitmapImage(new Uri("000\\image" + (pageNum + 1).ToString("D3") + ".jpg", UriKind.Relative));
        RefreshTextData();
    }
    

    Kinda ashamed to use that, but at least it seems to work.

    0 讨论(0)
  • 2020-12-01 19:47

    After many trial runs and debugging sessions I found that this caching of images is not performed (or not performed as aggressively) when images are residing in the app's IsolatedStorage.

    Thing is I was using the images which were a part of the xap file, included as content. I did this because I just wanted to test my image viewer. But this would not be the case when my app would finish. App was really designed to store images in Isolated storage and display them.

    So I setup the necessary code and voila, Images are now getting garbage collected, even though they still got cached. See image below(see how many times Garbage Collector gets called). This is a solution to a not so trivial question, that's why no one else faced problem of this kind.

    I believe that when WP7 silverlight finds that images is not from Isolated storage it assumes image is from some remote URI and decides to cache it anyway. And that's where silverlight image caching problem kicks in. As another answer confirms this doesn't happens in WP8.enter image description here

    0 讨论(0)
  • 2020-12-01 19:48

    I have tried your code sample, but in Windows Phone 8 environment and I couldn't reproduce the leak. The only difference is that I've used my own images.

    The current memory usage stayed at 13MB for my 512 WVGA Emulator and the Peak stayed at 14MB. I have pushed the "next button" around 20 times.

    Also have you tried using Bindings for ImageHolder instead of setting the Source manually?

    (btw, visually I don't see any possible memory leaks in your codebehind).

    (Also check this article http://blogs.windows.com/windows_phone/b/wpdev/archive/2012/02/01/memory-profiling-for-application-performance.aspx )

    0 讨论(0)
提交回复
热议问题