Hi Guys,

I am working with out of memory problem and it is bashing my head :)
In my project each activity has a instance of a imagehandler, this class
loads images from memory. It caches the latest 5 bitmaps and should dispose
the others and this ssems to work as the memory uses stabelizes. The
following three methods are in the imagehandler

 public Bitmap DecodeFile(string _path)
        {

            if (ImageCacheImages.ContainsKey(_path))
            {
                return ImageCacheImages[_path];
            }
            Bitmap newImage = null;
            try
            {
                newImage = BitmapFactory.DecodeFile(_path, _options);
            }
            catch (Exception)
            {

                if (this.RecycleImages())
                {
                    return this.DecodeFile(_path); 
                }
                else
                {
                    return null;
                } 
               
            }


            ImageCacheImages.Add(_path, newImage);
            _keys.Add(_path);

            if (ImageCacheImages.Count > 5)
            {
                this.ReleaseImage(0);
            }

            return newImage;

        }

        /// <summary>
        /// Method recycles each  bitmap for better gc
        /// </summary>
        public bool RecycleImages()
        {
            if (ImageCacheImages.Count>0)
            {
                for (int i = 0; i < ImageCacheImages.Count; i++)
                {
                    KeyValuePair<string, Bitmap> item =
ImageCacheImages.ElementAt(i);
                    Bitmap img = item.Value;
                    img.Dispose();
                    img = null;
                }

                ImageCacheImages.Clear();
                GC.Collect();
                return true;
            }
            GC.Collect();
            return false;

        }

        public bool ReleaseImage(int itemIndex)
        {
            if (_keys.Count > itemIndex)
            {
                string key = _keys[itemIndex];

                Bitmap img = ImageCacheImages[key];
                ImageCacheImages.Remove(key);
                _keys.RemoveAt(itemIndex);
                img.Dispose();
                img = null;
                GC.Collect();
                return true;
            }
            GC.Collect();
            return false;

        }

When finishing a activity I call the method public bool RecycleImages() on
the imagehandler to release memory. This is in the ondestroy method of the
activity:

   ImageHandler.RecycleImages();
            ImageHandler.Dispose();
            base.OnDestroy();

However it still seems to be leaking. The bitmaps are used in a custom
imageview where the bitmap is set the following way:
 var image = _activityContext.ImageHandler.DecodeFile(_path);
                   
                    if (image!=null)
                    {
                        SetImageBitmap(image);
                        this.Invalidate();    
                    }

Do you guys have any suggestion if the handling of bitmaps is correct or
what can I improve?

Regards

Bjarke



--
View this message in context: 
http://mono-for-android.1047100.n5.nabble.com/Memory-not-release-tp5711882.html
Sent from the Mono for Android mailing list archive at Nabble.com.
_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to