about Clear Memory For Windows Phone

Jul 23, 2012 at 7:39 AM
Edited Jul 23, 2012 at 7:40 AM

For Windows Phone 

 Create gif pictures

  AnimatedImage GifAnimatedImage = new AnimatedImage();
            ExtendedImage source = new ExtendedImage();
            source.UriSource = new Uri("http://ww4.sinaimg.cn/large/891a0f27jw1dv61g8ivx8g.gif", UriKind.Absolute);
            GifAnimatedImage.Source = source;
            GifGrid.Children.Clear();
            GifGrid.Children.Add(GifAnimatedImage);

//clear 
  if (source != null)
            {
                for (int i = 0; i < source.Frames.Count; i++)
                {
                    using (Stream s = new MemoryStream(source.Frames[i].Pixels))
                    {
                        s.Dispose();
                        s.Close();
                    };
                    source.Frames[i] = null;
                }
                //foreach (ImageFrame item in source.Frames)
                //{

                //}
                source.Frames.Clear();
                source.UriSource = null;
                source = null;
                //using (Stream s = new MemoryStream(source.Pixels))
                //{
                //    source.Frames.Clear();
                //    source.UriSource = null;
                //    source = null;
                //    s.Dispose();
                //    s.Close();
                //};
            }
            if (GifAnimatedImage != null)
            {
                (GifAnimatedImage.Source as ExtendedImage).Frames.Clear();
                GifAnimatedImage.Source = null;
                GifAnimatedImage = null;
            }

            GifGrid.Children.Clear();
            GC.Collect();

i can't clear memroy ,why? please help me.
Jul 27, 2012 at 4:50 PM

because it's a bug. Assigning to the UriSource only works initially. If you set it to null, no memory is freed. If you set it to another Uri it results in memory leak because frames are appended to the memory already allocated.