我在我的应用程序中使用Picasso库最新版本2.4.0来下载和缓存图像.大约有25-30张图像,每张300KB-400KB.我认为这不是一件大事或沉重的事情.
即使应用程序运行正常,我的logcat中的内存分配也没有了.谁能解释为什么会这样?
在GridView适配器中加载图像的代码:
Picasso.with(mContext).load(getUrl()).placeholder(R.drawable.placeholder) .into(viewholder.image);
这是我的Logcat输出:
I/dalvikvm-heap(11142): Grow heap (frag case) to 53.860MB for 2720016-byte allocation I/dalvikvm-heap(11142): Forcing collection of SoftReferences for 3265936-byte allocation E/dalvikvm-heap(11142): Out of memory on a 3265936-byte allocation. I/dalvikvm(11142): "Picasso-/images/posters/34.71.jpg" prio=5 tid=18 RUNNABLE I/dalvikvm(11142): | group="main" sCount=0 dsCount=0 obj=0x4283f248 self=0x60a47830 I/dalvikvm(11142): | sysTid=11196 nice=10 sched=0/0 cgrp=apps/bg_non_interactive I/dalvikvm(11142): | state=R schedstat=( 2070202497 1858185620 3947 ) utm=172 stm=35 core=3 I/dalvikvm(11142): at android.graphics.Bitmap.nativeCreate(Native Method) I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:726) I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:703) I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:636) I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.transformResult(BitmapHunter.I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:168) I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:111) I/dalvikvm(11142): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390I/dalvikvm(11142): at java.util.concurrent.FutureTask.run(FutureTask.java:234) I/dalvikvm(11142): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.I/dalvikvm(11142): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.I/dalvikvm(11142): at java.lang.Thread.run(Thread.java:841) I/dalvikvm(11142): at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:408) I/dalvikvm-heap(11142): Forcing collection of SoftReferences for 3265936-byte allocation E/dalvikvm-heap(11142): Out of memory on a 3265936-byte allocation. I/dalvikvm(11142): "Picasso-/images/posters/34.71.jpg" prio=5 tid=17 RUNNABLE I/dalvikvm(11142): | group="main" sCount=0 dsCount=0 obj=0x42841b88 self=0x5ec91f90 I/dalvikvm(11142): | sysTid=11183 nice=10 sched=0/0 cgrp=apps/bg_non_interactive I/dalvikvm(11142): | state=R schedstat=( 2050467088 1713164574 3713 ) utm=172 stm=32 core=3 I/dalvikvm(11142): at android.graphics.Bitmap.nativeCreate(Native Method) I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:726) I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:703) I/dalvikvm(11142): at android.graphics.Bitmap.createBitmap(Bitmap.java:636) I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.transformResult(BitmapHunter.I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:168) I/dalvikvm(11142): at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:111) I/dalvikvm(11142): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390I/dalvikvm(11142): at java.util.concurrent.FutureTask.run(FutureTask.java:234) I/dalvikvm(11142): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.I/dalvikvm(11142): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.I/dalvikvm(11142): at java.lang.Thread.run(Thread.java:841) I/dalvikvm(11142): at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:408)
CommonsWare.. 26
您的原始代码正在下载完整图像,将完整图像加载到内存中,然后让Android缩小图像以适合您的图像ImageView
.
在这种情况下,你不需要内存中的完整图像 - 你需要一些更接近它的东西ImageView
.
fit()
在毕加索处理这个问题.它使用inSampleSize
on BitmapFactory.Options
来对图像进行下采样,因为它被加载到内存中,为你提供大小相当的东西ImageView
,让Android从那里扩展.这将显着减少每个图像的内存占用量,特别是取决于ImageView
您使用的大小.