jackcar
jackcar

Reputation: 669

Out of Memory Picasso Lib

I'm developing an app that loads a lot of images through lib Picasso. When I'm navigating through the app, suddenly the app crashes, giving out of memory. I already tried to do somethings to avoid this kind of problem, but with no success.

If someone could help me, i would be glad.

Below the code:

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.codal.whatsee/com.codal.whatsee.fragmentactivity.ItemPostFragmentActivity}: android.view.InflateException: Binary 
XML file line #55: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2217)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.access$800(ActivityThread.java:145)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5141)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #55: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
at android.app.Activity.setContentView(Activity.java:1929)
at com.codal.whatsee.fragmentactivity.ItemPostFragmentActivity.onCreate(ItemPostFragmentActivity.java:150)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
... 23 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2208)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.ImageView.<init>(ImageView.java:129)
at android.widget.ImageView.<init>(ImageView.java:119)
at com.codal.whatsee.utils.SquareImageView.<init>(SquareImageView.java:13)
... 26 more

OBS: This problem happens not just in this class, but in any other class happens too.

Upvotes: 4

Views: 2258

Answers (4)

Bheem
Bheem

Reputation: 1

You may be getting OOM because of the following:

  1. You are doing some operation that continuously demands a lot of memory. Like Picasso Lib fetching large images/bitmaps which plays with bitmap operations

    • Picasso uses fit() or resize() methods while fetching the images. ReferenceLink
  2. Memory leaks: You didn’t made the previous objects you allocated eligible for Garbage Collection (GC)

    • Use memory analyzer tool for taking Dump check where object are getting leak.
  3. Dealing with large bitmaps. Scaling and bitmap operation take the memory from the heap.

I came across this link. It may help you : Link

Upvotes: 0

Codo
Codo

Reputation: 78855

Android 4.2 and 4.3 do not free the memory for a bitmap unless you finish the activity using the bitmap or recycle it (Bitmap.recycle()). See Android Bitmap Memory Analysis - Part 3. API Version Comparison for a detailed analysis. In Android 4.3 you need to additionally remove the image view from the view hierarchy.

So if you have an app where you can build up a deep navigation history and are using Picasso, you will run out of memory sooner or later because Picasso does not track references to bitmaps and therefore cannot call Bitmap.recycle() for them.

We had a similar issue and finally switched to Facebook's Fresco library, which goes at great length to track references. It is therefore more complex too use but it pays off.

Upvotes: 0

Faizan Tariq
Faizan Tariq

Reputation: 371

You can expect such exception when dealing with images and bitmap. In present case it appears that out of memory is due to loading of a large/big image which consume the available memory and creating issue.

Luckily picasso lib offers an option to scale down your image so that it may not consumed much memory and thus lower down the chances for getting out of memory exception.

See if this below code is helpful to you or not.

Picasso.with(context).load(file).resize(sizeToScaleDown, sizeToSacleDown).onlyScaleDown().into(imageView);

Note: onlyScaleDown() function only works if you have also used resize() function. Using onlyScaleDown() function without resize() function will cause you exception.

Upvotes: 0

user3811368
user3811368

Reputation: 183

This sounds a lot like your application is leaking memory and the image decoding is just the last operation that fills the heap. You should use the android DDMS tools and create a heap dump of your app after short use. If you see many Activities or Fragments in the dump, you should try to figure out which objects are still holding references to them.

Here is the related page from the developer site: https://developer.android.com/tools/debugging/debugging-memory.html

Upvotes: 1

Related Questions