Krøllebølle Krøllebølle - 1 year ago 260
Android Question

Picasso: out of memory

I have a

presenting several images using
. After scrolling some time up and down the application runs out of memory with messages like this:

E/dalvikvm-heap﹕ Out of memory on a 3053072-byte allocation.
I/dalvikvm﹕ "Picasso-/wp-content/uploads/2013/12/DSC_0972Small.jpg" prio=5 tid=19 RUNNABLE
I/dalvikvm﹕ | group="main" sCount=0 dsCount=0 obj=0x42822a50 self=0x59898998
I/dalvikvm﹕ | sysTid=25347 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=1500612752
I/dalvikvm﹕ | state=R schedstat=( 10373925093 843291977 45448 ) utm=880 stm=157 core=3
I/dalvikvm﹕ at Method)
I/dalvikvm﹕ at
I/dalvikvm﹕ at com.squareup.picasso.BitmapHunter.decodeStream(
I/dalvikvm﹕ at com.squareup.picasso.BitmapHunter.hunt(
I/dalvikvm﹕ at
I/dalvikvm﹕ at java.util.concurrent.Executors$
I/dalvikvm﹕ at
I/dalvikvm﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(
I/dalvikvm﹕ at java.util.concurrent.ThreadPoolExecutor$
I/dalvikvm﹕ at
I/dalvikvm﹕ at com.squareup.picasso.Utils$
I/dalvikvm﹕ [ 08-10 18:48:35.519 25218:25347 D/skia ]
--- decoder->decode returned false

The things I note while debugging:

  1. When installing the app on a phone or a virtual device, the images are loaded over the network, which is how it is meant to be. This is seen by the red triangle in the upper left corner of the image.

  2. When scrolling so that the images are reloaded, they are fetched from the disk. This is seen by the blue triangle in the upper left corner of the image.

  3. When scrolling some more, some of the images are loaded from memory, as seen by a green triangle in the upper left corner.

  4. After scrolling some more, the out of memory exception occurs and the loading stops. Only the placeholder image is shown on the images that are not currently kept in memory, while those in memory are shown properly with a green triangle.

Here is a sample image. It is quite large, but I use
to reduce the memory footprint in the app.

So my questions are:

  • Shouldn't the images be reloaded from disk when the memory cache
    is full?

  • Are the images just too large? How much memory can I
    expect an, let's say 0.5 MB image, to consume when decoded?

  • Is there anything wrong/unusual in my code below?

Setting up the static Picasso instance when creating the

private void setupPicasso()
Cache diskCache = new Cache(getDir("foo", Context.MODE_PRIVATE), 100000000);
OkHttpClient okHttpClient = new OkHttpClient();

Picasso picasso = new Picasso.Builder(this)
.memoryCache(new LruCache(100000000)) // Maybe something fishy here?
.downloader(new OkHttpDownloader(okHttpClient))

picasso.setIndicatorsEnabled(true); // For debugging


Using the static Picasso instance in my

public void onBindViewHolder(RecipeViewHolder recipeViewHolder, int position)
.into(recipeViewHolder.recipeImage); // recipeImage is an ImageView

// More...

in the XML file:



It seems that scrolling the
continuously makes the memory allocation increase indefinitely. I made a test
stripped down to match the official documentation, using a single image for 200
s with an
, but the problem persists. Most of the images are loaded from memory (green) and scrolling is smooth, but about every tenth
loads the image from disk (blue). When the image is loaded from disk, a memory allocation is performed, thereby increasing the allocation on the heap and thus the heap itself.

I tried removing my own setup of the global
instance and using the default instead, but the problems are the same.

I did a check with the Android Device Monitor, see the image below. This is for a Galaxy S3. Each of the allocations done when an image is loaded from disk can be seen to the right under "Allocation count per size". The size is slightly different for each allocation of the image, which is also weird. Pressing "Cause GB" makes the rightmost allocation of 4.7 MB go away.

Android Device Monitor

The behavior is the same for virtual devices. The image below shows it for a Nexus 5 AVD. Also here, the largest allocation (the 10.6 MB one) goes away when pressing "Cause GB".

Android Device Monitor

Additionally, here are images of the memory allocation locations and the threads from the Android Device Monitor. The reoccurring allocations are done in the
threads while the one removed with
Cause GB
is done on the main thread.

Allocation Tracker

Answer Source

I'm not sure fit() works with android:adjustViewBounds="true". According to some of the past issues it seems to be problematic.

A few recommendations:

  • Set a fixed size for the ImageView
  • User a GlobalLayoutListener to get the ImageView's size once it is calculated and after this call Picasso adding the resize() method
  • Give Glide a try - its default configuration results in a lower footprint than Picasso (it stores the resized imaged rather than the original and uses RGB565)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download