0

I got run time out of memory error, how can I resolve it and optimise my application performance.

When I am trying to perform any task regarding select images and video from gallery so at that time app getting hang and then crash and got out of memory issue.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • Read http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object – IntelliJ Amiya Apr 20 '17 at 10:43
  • 1. Use cache for selecting images and video. 2. Use `MediaStore` to get list of media files like MediaStore. – D.J Apr 20 '17 at 10:44
  • It sounds like you are getting into an infinite loop which allocates memory. Is there some code you can show? – Neil Apr 20 '17 at 10:44
  • i have already use **MediaStore** and for images showing i used Glide library but still getting error when user select 7 to 8 images and images size is more then 4 to 5 MBs – Shailendrasinh Gohil Apr 20 '17 at 10:45
  • Go have a look at this developer docs article. You don't want to use largeheap="true" as a fix because it simply is not. You should try solve your memory issue. https://developer.android.com/topic/performance/memory.html – the-ginger-geek Apr 20 '17 at 10:50

2 Answers2

2

You can code like this

 BitmapFactory.Options options = new Options();
options.inSampleSize = 32;`
//img = BitmapFactory.decodeFile(imageids[position], options);
Bitmap theImage = BitmapFactory.decodeStream(imageStream,null, options);
Bitmap img=theImage.copy(Bitmap.Config.RGB_565,true);
theImage.recycle();
theImage = null;
System.gc();
//ivlogdp.setImageBitmap(img);
Runtime.getRuntime().gc();

`

Harsh Singhal
  • 567
  • 4
  • 12
1

You should avoid playing with bitmaps otherwise you have to face OutOfMemory exceptions.

1. Set large heap in Android Manifest. Add following attribute to application tag.

android:largeHeap="true"

2. Try image libraries Picasso or Glide to avoid out of memory issues & better performance. These libraries handle out of memory.

Replace path with local folder path or server url

Glide.with (context).load (path).asBitmap().into(imageView);
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • largeHeap also true in my Manifeast and also tryed Glide and Picasso but problem is there i have functionality of user can select max 10 images but when user select huge resolution images at that time getting issue – Shailendrasinh Gohil Apr 20 '17 at 10:48
  • some people told me that Glide mange own cache so thats why so get error out of memory if you remove cache issue will be resolve but if i remove cache then images slightly blinks when i set next time in image view – Shailendrasinh Gohil Apr 20 '17 at 10:50