title: Memory Leak on ListView (due to ByteArrayToImageConverter)
body: <p>I m using ByteArrayToImageConverter to convert byte array i m getting from SQL server databse , the converter as below</p>
  
  <pre><code> public class ByteArrayToImageConverter : IValueConverter
      {
          public ImageSource retSource;
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
             // ImageSource retSource = null;
              if (value != null)
              {
                  byte[] imageAsBytes = (byte[])value;
                  // byte[] decodedByteArray = System.Convert.FromBase64String(Encoding.UTF8.GetString(imageAsBytes, 0, imageAsBytes.Length));
                  // var stream = new MemoryStream(decodedByteArray);
                  retSource = ImageSource.FromStream(() =&gt; new MemoryStream(imageAsBytes));
              }
              return retSource;
          }
  
  
          public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
          {
  
              //return null;
              throw new NotImplementedException();
          }
  </code></pre>
  
  <p>My view Model as below </p>
  
  <pre><code> public class ResturentItems    {
          public long ID { get; set; }
          public string Category { get; set; }
          public string Name { get; set; }
          public string Code { get; set; }
          public string Description { get; set; }
          public decimal Price { get; set; }
          public bool isservicecharge { get; set; }
          public decimal CostPrice { get; set; }
          public string Date { get; set; }      
          public byte[] Image { get; set; }    }
  
      public class ResturentList
      {
          public List&lt;ResturentItems&gt; Resturent { get; set; }
      }
  </code></pre>
  
  <p>conenet page code behind </p>
  
  <pre><code>namespace TestProject.Views.DetailViews
  {
      [XamlCompilation(XamlCompilationOptions.Compile)]
      public partial class FoodTabPage : ContentPage
      {
          private List&lt;ResturentItems&gt; _foodList;
          public FoodTabPage(string categoryName, List&lt;ResturentItems&gt;foodList)
          {
              InitializeComponent();
              this.BackgroundImage = "background.png";
  
              // createEventsList();           
              Title = categoryName;            
              mealsListView.ItemsSource = foodList;
              _foodList = foodList;
  
          }
  
          private void Search(object sender, EventArgs e)
          {
              stackLayoutShowHide.IsVisible = !stackLayoutShowHide.IsVisible;
          }
  
          private void searchChanged(object sender, TextChangedEventArgs e)
          {
  
              if (string.IsNullOrWhiteSpace(e.NewTextValue))
                  mealsListView.ItemsSource = _foodList;
              else
                  mealsListView.ItemsSource = _foodList.Where(i =&gt; i.Name.ToLower().Contains(e.NewTextValue.ToLower()));
          }
  
  
          private void listviewContacts_ItemSelected(object sender, SelectedItemChangedEventArgs e)
          {
              var itemSelectedData = e.SelectedItem as ResturentItems;
              Navigation.PushAsync(new JsonDetailsPage(itemSelectedData.ID, itemSelectedData.Image, itemSelectedData.Name, itemSelectedData.Code, itemSelectedData.Description, itemSelectedData.Price, itemSelectedData.isservicecharge, itemSelectedData.CostPrice));
              //this.Content = null;
              //this.BindingContext = null;
              //GC.Collect(1);
          }
  
  
          protected override void OnDisappearing()
          {
              // ImagesStackLayout.Children.Clear();
  
              GC.Collect();
             // mealsListView.BindingContext = null;
             // this.Content = null;
              //mealsListView.ItemsSource = null;
              //this.BindingContext = null;
              //GC.Collect(1);
          }
  
      }
  
  
  } 
  </code></pre>
  
  <p>XAML of conetent page </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
               x:Class="TestProject.Views.DetailViews.FoodTabPage"
               xmlns:local ="clr-namespace:TestProject.Data"&gt;   
      &lt;ContentPage.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;local:ByteArrayToImageConverter x:Key="severityTypeImageConvertertwo"/&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/ContentPage.Resources&gt;
      &lt;ContentPage.ToolbarItems&gt;
          &lt;ToolbarItem Text="Search" Clicked="Search"/&gt;
      &lt;/ContentPage.ToolbarItems&gt;
      &lt;ContentPage.Content&gt;       
              &lt;StackLayout&gt;
                  &lt;StackLayout x:Name="stackLayoutShowHide" IsVisible="False"&gt;
                      &lt;Entry Placeholder="Your text to search" TextChanged="searchChanged" TextColor="Gray" BackgroundColor="White"/&gt;
                  &lt;/StackLayout&gt;
                  &lt;ListView x:Name="mealsListView" HasUnevenRows="True" ItemSelected="listviewContacts_ItemSelected" CachingStrategy="RecycleElement" &gt;
                  &lt;ListView.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;ViewCell&gt;
                              &lt;StackLayout  Orientation="Vertical" Padding="5"&gt;
                                  &lt;StackLayout Orientation="Horizontal"  BackgroundColor="Ivory" Opacity="0.9"&gt;
                                      &lt;Image Source="{Binding Image,Converter={StaticResource severityTypeImageConvertertwo}}" HeightRequest="120" WidthRequest="120"/&gt;
                                      &lt;StackLayout Orientation="Vertical"&gt;
                                          &lt;Label Text="{Binding Name}" FontSize="Medium" TextColor="Gray" FontAttributes="Bold"/&gt;
                                          &lt;BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" HorizontalOptions="FillAndExpand" /&gt;
                                          &lt;Label Text="{Binding Description}" FontSize="Micro" TextColor="Gray" FontAttributes="Bold"/&gt;
                                          &lt;StackLayout Orientation="Horizontal" VerticalOptions="Start" HorizontalOptions="Start"&gt;
                                              &lt;Label Text="$" FontSize="Micro" VerticalOptions="Start" HorizontalOptions="Start" TextColor="Gray" FontAttributes="Bold"/&gt;
                                              &lt;Label Text="{Binding Price}" FontSize="Micro" TextColor="Gray" FontAttributes="Bold"/&gt;
                                          &lt;/StackLayout&gt;
                                      &lt;/StackLayout&gt;
                                      &lt;Image Source="arrowtwo.png" BackgroundColor="Transparent" WidthRequest="25" Margin="0,10,10,0"/&gt;
                                  &lt;/StackLayout&gt;
                              &lt;/StackLayout&gt;
                          &lt;/ViewCell&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/ListView.ItemTemplate&gt;
              &lt;/ListView&gt;
          &lt;/StackLayout&gt;
      &lt;/ContentPage.Content&gt;
  &lt;/ContentPage&gt;
  </code></pre>
  
  <p>i m getting the Content page data through a tab page with API call, as below </p>
  
  <pre><code>public partial class FoodTabbedPage : TabbedPage
      {
          // List&lt;ResturentItems&gt; ResturentList = new List&lt;ResturentItems&gt;();
          List&lt;ResturentItems&gt; ResturentList;
          //SearchBar searchBar;
          //Label resultsLabel;
  
          public FoodTabbedPage()
          {
  
            //  List&lt;ResturentItems&gt; ResturentList = new List&lt;ResturentItems&gt;();
              InitializeComponent();
              this.BackgroundImage = "Resources/drawable/background.png";
              this.BackgroundColor = Color.Maroon;
              Children.Add(new TabLoader());
  
  
              //View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("Images/bg.png"));
              //this.ParentView = "background.png";
              // this.View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromFile("your-background.png"));
              //ActivityIndicator ciclenow = new ActivityIndicator();
              //ciclenow.Color = Color.Green;
              //ciclenow.IsRunning = true;
              ActivityIndicator ai = new ActivityIndicator
              {
                  Color = Color.Black,
                  IsRunning = true,
              };
  
  
              this.IsBusy = true;
              GetJSON();
              //Children.Remove(new JsonDesertNew());
              //var stack = new StackLayout
              //{
              //    Spacing = 20,
              //    Children = {
              //             new Label {Text = "Hello World!"},
              //             new Button {Text = "Click Me!"}
              //                }
              //};
  
              //ToolbarItem cartItem = new ToolbarItem();            
              //cartItem.Order = ToolbarItemOrder.Primary;
              //cartItem.Text = "serch";
              //cartItem.Command = new Command(() =&gt; Navigation.PushAsync(new SearchItemsPage()));
              //ToolbarItems.Add(cartItem);
  
              ToolbarItem serch = new ToolbarItem();
              serch.Order = ToolbarItemOrder.Secondary;
              serch.Text = "SERCH MORE";
              serch.Command = new Command(() =&gt; Navigation.PushAsync(new SearchItemsPage()));
              ToolbarItems.Add(serch);
  
          }
  
  
  
  
          public async void GetJSON()
          {
  
              var client = new System.Net.Http.HttpClient();               
                  var response = await client.GetAsync("http://192.168.43.51/TabPageAll.php");
                  string contactsJson = response.Content.ReadAsStringAsync().Result;
                  if (response.IsSuccessStatusCode)
                  {
                      var list = JsonConvert.DeserializeObject&lt;ResturentList&gt;(contactsJson);
                      ResturentList = new List&lt;ResturentItems&gt;(list.Resturent);
                      List&lt;string&gt; allCategories = ResturentList.Select(x =&gt; x.Category).ToList();
                      allCategories = allCategories.Distinct().ToList();
                      foreach (var category in allCategories)
                      {
                      //Children.Remove(Children.First());
                      Children.Add(new FoodTabPage(category, ResturentList.Where(x =&gt; x.Category == category).ToList()));
                      // Children.Remove(Children.First());
  
                      }
                  Children.Remove(Children.First());
                  }
                  else
                  {
                      var textReader = new JsonTextReader(new StringReader(contactsJson));
                      dynamic responseJson = new JsonSerializer().Deserialize(textReader);
                      contactsJson = "Deserialized JSON error message: " + responseJson.Message;
                      await DisplayAlert("fail", "no Network Is Available.", "Ok");
                  }
  
  
          }
  </code></pre>
  
  <p>my problem when i show all the images in the xaml list view ( bind with my model) i m having memeory leaks , every time i open the list view the memory is growing gradually , Whast is the solution to reduce the memeor leaking issue with the converter and list view ( by the way i m using <code>CachingStrategy="RecycleElement"</code> but it dosent reduce the memory leak. please if anyone across this type of issue please let me know.
  <a href="https://i.stack.imgur.com/rY75G.png" rel="nofollow noreferrer">ProfilerImage</a>
  <a href="https://i.stack.imgur.com/wtp7u.jpg" rel="nofollow noreferrer">ProfilerImage2</a></p>
  
  <p>Thank you inadvance for your support. 
  Pan</p>