title: onServiceDisconnected (crash or killed by oom)
body: <p>I am getting <code>onServiceDisconnected (crash or killed by oom)</code> error. The error occurs randomly. All that I am doing is using camera api2 to capture image and create base64 and load it using glide. There is no restriction on number of images, so if user click around 30 images and load those using glide then after sometime I am getting <code>onServiceDisconnected (crash or killed by oom)</code> error. Not sure where exactly the error is happening. When I am clicking a picture I am also putting <code>image bytes</code> in bytes array. This byte array is later on used to create an actual image and save it locally.</p>
  
  <p>So I am quite not sure where exactly I might be getting this error.  Any help would be really appreciated.</p>
  
  <p><strong>Edit:</strong></p>
  
  <pre><code>public class ImageAvailableListener : Java.Lang.Object, ImageReader.IOnImageAvailableListener
  {
      public ImageAvailableListener(CameraFragment fragment, File file)
      {
          if (fragment == null)
              throw new System.ArgumentNullException("fragment");
          if (file == null)
              throw new System.ArgumentNullException("file");
  
          owner = fragment;
          this.file = owner.mFile;
      }
  
      private readonly File file;
      private readonly CameraFragment owner;
  
      public void OnImageAvailable(ImageReader reader)
      {
          owner.mBackgroundHandler.Post(new AddImageToList(reader.AcquireNextImage(), owner, owner.mFile));
      }
  
      private class AddImageToList : Java.Lang.Object, IRunnable
      {
          private CameraFragment fragment;
          // The JPEG image
          private Image mImage;
  
          private File mFile;
          public AddImageToList(Image image, CameraFragment _fragment, File file)
          {
              if (image == null)
                  throw new System.ArgumentNullException("image");
  
              mImage = image;
              fragment = _fragment;
              mFile = file;
          }
  
          public void Run()
          {
              try
              {
                  ByteBuffer buffer = mImage.GetPlanes()[0].Buffer;
                  byte[] bytes = new byte[buffer.Remaining()];
                  buffer.Get(bytes);
                  string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                  string guid = Guid.NewGuid().ToString().ToUpper();
                  fragment.capturedPics.Add(new keyValue { imageBytes = bytes, imageName = guid });
                  fragment.capturedPicBase64.Add(new Data.Models.ViewModel.DocumentImageViewModel { imgName = string.Format("{0}", guid), base64 = base64String });
                  mImage.Close();
              }
              catch (System.Exception e)
              {
                  //e.PrintStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Adapter</strong></p>
  
  <pre><code>public class ViewDocumentAdapter : RecyclerView.Adapter
  {
      #region Variable Declarations
      public List&lt;object&gt; _items;
      private readonly Activity _context;
      public event EventHandler&lt;int&gt; ItemClick;
      #endregion
  
      public ViewDocumentAdapter(Activity activity, IEnumerable&lt;object&gt; images)
      {
          _items = images.ToList();
          _context = activity;
      }
  
      void OnClick(int position)
      {
          if (ItemClick != null)
          {
              ItemClick(this, position);
          }
      }
  
      public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
      {
          View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.imageThumbnailListItem, parent, false);
          DocumentViewHolder vh = new DocumentViewHolder(itemView, OnClick);
          return vh;
      }
  
      public override void OnViewRecycled(Java.Lang.Object holder)
      {
          DocumentViewHolder vh = holder as DocumentViewHolder;
          vh.imageView.SetImageDrawable(null);
          base.OnViewRecycled(holder);
      }
  
      public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
      {
          DocumentViewHolder vh = holder as DocumentViewHolder;
         if (_items[position].ToString() == "default")
          {
              vh.cardView.SetCardBackgroundColor(new Color(ContextCompat.GetColor(_context, Resource.Color.darkGray)));
              vh.imageView.SetScaleType(ImageView.ScaleType.Center);
              vh.imageView.SetBackgroundColor(new Color(ContextCompat.GetColor(_context, Resource.Color.darkGray)));
              vh.progress.SetBackgroundColor(new Color(ContextCompat.GetColor(_context, Resource.Color.darkGray)));
              vh.frame.SetPadding(0, 0, 0, 0);
  
              DrawableTransitionOptions options = new DrawableTransitionOptions();
              options.CrossFade();
              Glide.With(vh.imageView.Context)//_context)
                          .Load(Resource.Drawable.CameraWhite)
                          .Apply(new Com.Bumptech.Glide.Request.RequestOptions()
                              .Error(Resource.Drawable.ic_exclaimation)
                              .FitCenter()
                              .Override(120, 120)
                              .InvokeDiskCacheStrategy(DiskCacheStrategy.All))
                          .Transition(options)
                          .Listener(new GlideImagelistener(vh))
                          //.Apply(RequestOptions.CircleCropTransform())
                          .Into(vh.imageView);
          }
          else
          {
              vh.cardView.SetCardBackgroundColor(new Color(ContextCompat.GetColor(_context, Resource.Color.backgroundColor)));
              vh.imageView.SetScaleType(ImageView.ScaleType.FitXy);
              vh.imageView.SetBackgroundColor(new Color(ContextCompat.GetColor(_context, Resource.Color.backgroundColor)));
              vh.progress.SetBackgroundColor(new Color(ContextCompat.GetColor(_context, Resource.Color.backgroundColor)));
              vh.frame.SetPadding(15, 15, 15, 15);
  
              DrawableTransitionOptions options = new DrawableTransitionOptions();
              options.CrossFade();
              Glide.With(vh.imageView.Context)//_context)
                          .Load(_items[position].ToString())
                          .Apply(new Com.Bumptech.Glide.Request.RequestOptions()
                              .Error(Resource.Drawable.ic_exclaimation)
                              .FitCenter()
                              .Override(120, 120)
                              .InvokeDiskCacheStrategy(DiskCacheStrategy.All))
                          .Transition(options)
                          .Listener(new GlideImagelistener(vh))
                          //.Apply(RequestOptions.CircleCropTransform())
                          .Into(vh.imageView);
          }
      }
  
      public int getSelectItem(int position)
      {
          return position;
      }
      public override int ItemCount
      {
          get { return _items.Count; }
      }
  
      public class DocumentViewHolder : RecyclerView.ViewHolder
      {
          public ImageView imageView { get; set; }
          public ProgressBar progress { get; set; }
          public FrameLayout frame { get; set; }
          public CardView cardView { get; set; }
          // Get references to the views defined in the CardView layout.
          public DocumentViewHolder(View itemView, Action&lt;int&gt; listener) : base(itemView)
          {
              imageView = itemView.FindViewById&lt;ImageView&gt;(Resource.Id.grid_item_image);
              progress = itemView.FindViewById&lt;ProgressBar&gt;(Resource.Id.progress);
              frame = itemView.FindViewById&lt;FrameLayout&gt;(Resource.Id.frame);
              cardView = itemView.FindViewById&lt;CardView&gt;(Resource.Id.cardView);
              itemView.Click += (sender, e) =&gt; listener(base.Position);
          }
      }
  }
  
  internal class GlideImagelistener : Java.Lang.Object,IRequestListener
  {
      private DocumentViewHolder holder;
  
      public GlideImagelistener(DocumentViewHolder holder)
      {
          this.holder = holder;
          this.holder.progress.Visibility = ViewStates.Visible;
      }
  
      public bool OnLoadFailed(GlideException p0, Java.Lang.Object p1, ITarget p2, bool p3)
      {
          holder.progress.Visibility = ViewStates.Gone;
          return false;
      }
  
      public bool OnResourceReady(Java.Lang.Object p0, Java.Lang.Object p1, ITarget p2, DataSource p3, bool p4)
      {
          holder.progress.Visibility = ViewStates.Gone;
          return false;
      }
  }
  </code></pre>
  
  <p>When a image is clicked I am saving base64 of the image as well bytes in an array. The image is not yet saved to the mobile storage. The user is allowed to view list of images clicked and delete or add more images to the list (this list is loaded from base64 array). The user can save the images, at this point the actual image will be created in mobile storage and will be uploaded to server. Once it is synced to the sever, user can go back and edit ie. delete any image and or add more image. Here when loading the images it will coming from server ie by URL. And newly added images will be in base64.</p>
  
  <p>Not sure where exactly out of memory error occurs.</p>