Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

How to free memory used by Images in view content OutOfMemoryError

$
0
0

Hi, I'm using PCL and getting java OutOfMemoryError exceptions. The first time I create the view with a list of images, it works, but after coming back to the view and recreating the same list (OnAppearing), it fails. Why? Its the same list and the old content is overwritten, so it should be freed by GC.

The images are actually created in a child view: tabbedPage, NavigationPage, ScrollView, Grid, StackLayout, StackLayout, then Children.Add(Image).

I tried to simplify the example by reducing the sub-subViews, but still get the problem. I know I can reduce the issue by resizing the image (which I will do), but how do I free the images?
GC.Collect() and GC.Collect(1) doesn't make any difference.

===
Example:
public class FilesPage : ContentPage

    protected override void OnAppearing()
    {
        base.OnAppearing();
        AddContent();
    }

    protected void AddContent()
    {
        int idx = 0;
        var gridView = new Grid {};

    // makes no difference
        //if (Content != null)
        //{
        //  var oldView = (ScrollView)Content;
        //  Content = null;
        //  var oldSubView = (Grid)oldView.Content;
        //  foreach(StackLayout stk in oldSubView.Children)
        //  {
        //      stk.Children.Clear();
        //  }
        //  oldSubView.Children.Clear();
        //  GC.Collect();
        //  GC.Collect(1);
        //}

        Content = new ScrollView { Content = gridView };
        List<MediaModel> mediaList = MediaModel.LoadList();

        foreach (MediaModel mediaAttrs in mediaList)
        {
            AddOneFile(gridView, mediaAttrs, idx);
            idx++;
        }
    }

...
protected void AddOneFile(Grid view, MediaModel mediaAttrs, int idx)
{
Image omg = new Image {Source = ImageSource.FromFile(Path.Combine(App.storagePath, this.fileName))};
StackLayout stk = new StackLayout
{
Children = {
...
}
};
stk.Children.Insert(1, img);
view.Children.Add(stk, idx % 2, idx / 2);
}


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>