写真を一定量読み込むとメモリ不足になってしまうのですが
VolleyのImageLoaderを用いてネットからListViewへ写真をロードしています。
キャッシュはLruCacheとImageLoader#ImageCasheを組み合わせています。
ImageLoaderなどはシングルトンです。
確認はエミュレータのNexus5(API22)で行っています。
写真のサイズは縦120dpになるようにしていて、画面上に1枚と2枚目が少し見える程度です。
ListViewは複数あり、それぞれをreplaceして切り替えています。
8ページ目まではスクロールしなければ普通にページ遷移できるのですが、9ページ目でメモリ不足と言われます。
また、前のページでスクロールして3枚以上の写真を見ていると、きちんと見られるページ数が減っていきます。
キャッシュはRuntime.getRuntime().maxMemory()/1024
の1/8をmaxSizeとしているはずなのですが、メモリ使用量を見るとページを開くたびにみるみる消費されていて指定したサイズを超えているように見えます。
原因は何が考えられるでしょうか?
キャッシュ用クラスは以下の通りです。
public static class ImageLruCache implements ImageLoader.ImageCache {
private LruCache<String, Bitmap> mMemoryCache;
public ImageLruCache() {
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount() / 1024;
}
};
}
@Override
public Bitmap getBitmap(String url) {
System.out.println(mMemoryCache.toString());
System.out.println("maxMemory:" + Runtime.getRuntime().maxMemory() / 1024);
System.out.println("totalMemory:" + Runtime.getRuntime().totalMemory() / 1024);
System.out.println("freeMemory:" + Runtime.getRuntime().freeMemory() / 1024);
System.out.println("useMemory:" + ((Runtime.getRuntime().totalMemory() / 1024) - (Runtime.getRuntime().freeMemory() / 1024)));
return mMemoryCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
mMemoryCache.put(url, bitmap);
}
}
ログは以下の通りです。
(一部情報を伏せています。)
07-21 15:50:46.811 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=0,hitRate=0%] 07-21 15:50:46.811 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:46.812 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:5890 07-21 15:50:46.812 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:457 07-21 15:50:46.812 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:5433 07-21 15:50:46.884 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=1,hitRate=0%] 07-21 15:50:46.884 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:46.884 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:11699 07-21 15:50:46.884 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:0 07-21 15:50:46.884 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:11699 07-21 15:50:49.794 2178-2235/jp.co.sakaguchi W/chromium﹕ [WARNING:jni_string.cc(28)] ConvertJavaStringToUTF8 called with null string. 07-21 15:50:49.855 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=2,hitRate=0%] 07-21 15:50:49.855 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:49.855 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:16096 07-21 15:50:49.855 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:3456 07-21 15:50:49.855 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:12641 07-21 15:50:49.931 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=3,hitRate=0%] 07-21 15:50:49.932 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:49.932 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:19161 07-21 15:50:49.932 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:0 07-21 15:50:49.932 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:19161 07-21 15:50:51.413 2178-2237/jp.co.sakaguchi W/chromium﹕ [WARNING:jni_string.cc(28)] ConvertJavaStringToUTF8 called with null string. 07-21 15:50:51.474 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=4,hitRate=0%] 07-21 15:50:51.474 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:51.474 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:23461 07-21 15:50:51.474 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:3425 07-21 15:50:51.474 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:20037 07-21 15:50:51.531 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=5,hitRate=0%] 07-21 15:50:51.531 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:51.531 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:26971 07-21 15:50:51.531 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:0 07-21 15:50:51.531 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:26972 07-21 15:50:51.567 2178-2190/jp.co.sakaguchi W/art﹕ Suspending all threads took: 8.262ms 07-21 15:50:51.702 2178-2185/jp.co.sakaguchi W/art﹕ Suspending all threads took: 7.901ms 07-21 15:50:52.038 2178-2190/jp.co.sakaguchi I/art﹕ Background sticky concurrent mark sweep GC freed 401(10KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 29MB/29MB, paused 8.106ms total 29.733ms 07-21 15:50:52.791 2178-2238/jp.co.sakaguchi W/chromium﹕ [WARNING:jni_string.cc(28)] ConvertJavaStringToUTF8 called with null string. 07-21 15:50:52.872 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=6,hitRate=0%] 07-21 15:50:52.872 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536F 07-21 15:50:52.872 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:30026 07-21 15:50:52.872 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:3424 07-21 15:50:52.872 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:26602 07-21 15:50:52.913 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=7,hitRate=0%] 07-21 15:50:52.913 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:52.913 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:33528 07-21 15:50:52.913 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:0 07-21 15:50:52.913 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:33528 07-21 15:50:53.012 2178-2190/jp.co.sakaguchi I/art﹕ Background partial concurrent mark sweep GC freed 1375(113KB) AllocSpace objects, 12(6MB) LOS objects, 13% free, 25MB/29MB, paused 30.173ms total 56.540ms 07-21 15:50:54.171 2178-2235/jp.co.sakaguchi W/chromium﹕ [WARNING:jni_string.cc(28)] ConvertJavaStringToUTF8 called with null string. 07-21 15:50:54.282 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=8,hitRate=0%] 07-21 15:50:54.282 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:54.282 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:36989 07-21 15:50:54.282 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:3424 07-21 15:50:54.282 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:33565 07-21 15:50:54.322 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=9,hitRate=0%] 07-21 15:50:54.322 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:54.322 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:40491 07-21 15:50:54.322 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:0 07-21 15:50:54.322 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:40492 07-21 15:50:55.554 2178-2236/jp.co.sakaguchi W/chromium﹕ [WARNING:jni_string.cc(28)] ConvertJavaStringToUTF8 called with null string. 07-21 15:50:55.609 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=10,hitRate=0%] 07-21 15:50:55.609 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:55.609 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:43947 07-21 15:50:55.609 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:3424 07-21 15:50:55.609 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:40523 07-21 15:50:55.674 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=11,hitRate=0%] 07-21 15:50:55.674 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:55.674 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:46991 07-21 15:50:55.674 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:0 07-21 15:50:55.674 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:46991 07-21 15:50:56.135 2178-2190/jp.co.sakaguchi I/art﹕ Background partial concurrent mark sweep GC freed 156(7KB) AllocSpace objects, 3(4MB) LOS objects, 7% free, 46MB/50MB, paused 7.580ms total 22.873ms 07-21 15:50:57.087 2178-2237/jp.co.sakaguchi W/chromium﹕ [WARNING:jni_string.cc(28)] ConvertJavaStringToUTF8 called with null string. 07-21 15:50:57.123 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=12,hitRate=0%] 07-21 15:50:57.123 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:57.123 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:50904 07-21 15:50:57.123 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:3463 07-21 15:50:57.123 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:47441 07-21 15:50:57.166 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=13,hitRate=0%] 07-21 15:50:57.166 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:57.166 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:53948 07-21 15:50:57.166 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:0 07-21 15:50:57.169 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:53949 07-21 15:50:58.239 2178-2238/jp.co.sakaguchi W/chromium﹕ [WARNING:jni_string.cc(28)] ConvertJavaStringToUTF8 called with null string. 07-21 15:50:58.279 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=14,hitRate=0%] 07-21 15:50:58.279 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:58.280 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:57869 07-21 15:50:58.280 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:3424 07-21 15:50:58.280 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:54445 07-21 15:50:58.330 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=15,hitRate=0%] 07-21 15:50:58.331 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:58.331 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:61381 07-21 15:50:58.331 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:0 07-21 15:50:58.331 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:61381 07-21 15:50:58.689 2178-2190/jp.co.sakaguchi I/art﹕ Background partial concurrent mark sweep GC freed 222(11KB) AllocSpace objects, 0(0B) LOS objects, 6% free, 58MB/62MB, paused 9.673ms total 29.542ms 07-21 15:50:58.724 2178-2236/jp.co.sakaguchi I/art﹕ Alloc sticky concurrent mark sweep GC freed 477(14KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 62MB/62MB, paused 1.237ms total 9.936ms 07-21 15:50:58.741 2178-2236/jp.co.sakaguchi I/art﹕ Alloc partial concurrent mark sweep GC freed 246(12KB) AllocSpace objects, 3(4MB) LOS objects, 6% free, 58MB/62MB, paused 2.806ms total 12.968ms 07-21 15:50:59.643 2178-2235/jp.co.sakaguchi W/chromium﹕ [WARNING:jni_string.cc(28)] ConvertJavaStringToUTF8 called with null string. 07-21 15:50:59.721 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=16,hitRate=0%] 07-21 15:50:59.721 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:59.722 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:65222 07-21 15:50:59.722 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:3424 07-21 15:50:59.722 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:61798 07-21 15:50:59.767 2178-2190/jp.co.sakaguchi I/art﹕ Background sticky concurrent mark sweep GC freed 5577(228KB) AllocSpace objects, 6(225KB) LOS objects, 0% free, 63MB/63MB, paused 6.175ms total 16.146ms 07-21 15:50:59.768 2178-2178/jp.co.sakaguchi I/art﹕ WaitForGcToComplete blocked for 14.318ms for cause Alloc 07-21 15:50:59.780 2178-2190/jp.co.sakaguchi W/art﹕ Suspending all threads took: 6.845ms 07-21 15:50:59.785 2178-2178/jp.co.sakaguchi I/art﹕ Alloc partial concurrent mark sweep GC freed 763(73KB) AllocSpace objects, 13(7MB) LOS objects, 6% free, 56MB/60MB, paused 2.870ms total 14.182ms 07-21 15:50:59.798 2178-2178/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=17,hitRate=0%] 07-21 15:50:59.798 2178-2178/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-21 15:50:59.798 2178-2178/jp.co.sakaguchi I/System.out﹕ totalMemory:62100 07-21 15:50:59.798 2178-2178/jp.co.sakaguchi I/System.out﹕ freeMemory:1208 07-21 15:50:59.798 2178-2178/jp.co.sakaguchi I/System.out﹕ useMemory:60892 07-21 15:51:00.229 2178-2237/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 64MB to 64MB 07-21 15:51:00.229 2178-2237/jp.co.sakaguchi I/art﹕ Alloc partial concurrent mark sweep GC freed 596(32KB) AllocSpace objects, 1(297KB) LOS objects, 5% free, 60MB/64MB, paused 1.129ms total 42.822ms 07-21 15:51:00.276 2178-2190/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.280 2178-2237/jp.co.sakaguchi I/art﹕ WaitForGcToComplete blocked for 12.467ms for cause Alloc 07-21 15:51:00.299 2178-2237/jp.co.sakaguchi I/art﹕ Alloc sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 1.154ms total 5.816ms 07-21 15:51:00.316 2178-2237/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.316 2178-2237/jp.co.sakaguchi I/art﹕ Alloc partial concurrent mark sweep GC freed 12(416B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 1.152ms total 15.903ms 07-21 15:51:00.339 2178-2237/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.339 2178-2237/jp.co.sakaguchi I/art﹕ Alloc concurrent mark sweep GC freed 8(12KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 938us total 16.672ms 07-21 15:51:00.341 2178-2237/jp.co.sakaguchi I/art﹕ Forcing collection of SoftReferences for 1708KB allocation 07-21 15:51:00.355 2178-2237/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.355 2178-2237/jp.co.sakaguchi I/art﹕ Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 1.218ms total 13.334ms 07-21 15:51:00.359 2178-2237/jp.co.sakaguchi E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 1749612 byte allocation with 344768 free bytes and 336KB until OOM" 07-21 15:51:00.373 2178-2237/jp.co.sakaguchi I/art﹕ Alloc sticky concurrent mark sweep GC freed 5(368B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 435us total 13.655ms 07-21 15:51:00.390 2178-2237/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.391 2178-2237/jp.co.sakaguchi I/art﹕ Alloc partial concurrent mark sweep GC freed 10(336B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 1.863ms total 10.959ms 07-21 15:51:00.409 2178-2237/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.409 2178-2237/jp.co.sakaguchi I/art﹕ Alloc concurrent mark sweep GC freed 5(224B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 1.622ms total 13.938ms 07-21 15:51:00.412 2178-2237/jp.co.sakaguchi I/art﹕ Forcing collection of SoftReferences for 1708KB allocation 07-21 15:51:00.428 2178-2237/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.428 2178-2237/jp.co.sakaguchi I/art﹕ Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 1.221ms total 13.563ms 07-21 15:51:00.430 2178-2237/jp.co.sakaguchi E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 1749612 byte allocation with 344384 free bytes and 336KB until OOM" 07-21 15:51:00.431 2178-2237/jp.co.sakaguchi E/Volley﹕ [160] ImageRequest.parseNetworkResponse: Caught OOM for 304176 byte image, url=画像取得先URL 07-21 15:51:00.448 2178-2238/jp.co.sakaguchi I/art﹕ Alloc sticky concurrent mark sweep GC freed 74(5KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 1.452ms total 15.147ms 07-21 15:51:00.463 2178-2238/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.465 2178-2238/jp.co.sakaguchi I/art﹕ Alloc partial concurrent mark sweep GC freed 68(2928B) AllocSpace objects, 1(297KB) LOS objects, 0% free, 63MB/64MB, paused 1.662ms total 11.116ms 07-21 15:51:00.483 2178-2238/jp.co.sakaguchi I/art﹕ Alloc concurrent mark sweep GC freed 11(432B) AllocSpace objects, 1(3MB) LOS objects, 6% free, 59MB/63MB, paused 1.161ms total 12.194ms 07-21 15:51:00.513 2178-2238/jp.co.sakaguchi I/art﹕ Alloc sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/63MB, paused 1.429ms total 7.145ms 07-21 15:51:00.539 2178-2238/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.539 2178-2238/jp.co.sakaguchi I/art﹕ Alloc partial concurrent mark sweep GC freed 10(336B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 1.755ms total 18.936ms 07-21 15:51:00.550 2178-2178/jp.co.sakaguchi E/Event﹕ Could not dispatch event: class jp.co.sakaguchi.model.UserEvent to subscribing class class jp.co.sakaguchi.MainActivity java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at jp.co.sakaguchi.MainActivity.onEventAlert(MainActivity.java:212) at jp.co.sakaguchi.MainActivity.onEvent(MainActivity.java:123) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at de.greenrobot.event.EventBus.invokeSubscriber(EventBus.java:498) at de.greenrobot.event.EventBus.postToSubscription(EventBus.java:429) at de.greenrobot.event.EventBus.postSingleEventForEventType(EventBus.java:410) at de.greenrobot.event.EventBus.postSingleEvent(EventBus.java:383) at de.greenrobot.event.EventBus.post(EventBus.java:263) at jp.co.sakaguchi.model.AppController.onErrorResponse(AppController.java:134) at jp.co.sakaguchi.view.PhotoView$1.onErrorResponse(PhotoView.java:65) at com.android.volley.toolbox.ImageLoader$4.run(ImageLoader.java:476) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 07-21 15:51:00.551 2178-2178/jp.co.sakaguchi D/Event﹕ No subscribers registered for event class de.greenrobot.event.SubscriberExceptionEvent 07-21 15:51:00.574 2178-2238/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.574 2178-2238/jp.co.sakaguchi I/art﹕ Alloc concurrent mark sweep GC freed 335(35KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 1.732ms total 31.029ms 07-21 15:51:00.579 2178-2238/jp.co.sakaguchi I/art﹕ Forcing collection of SoftReferences for 1708KB allocation 07-21 15:51:00.621 2178-2238/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.621 2178-2238/jp.co.sakaguchi I/art﹕ Alloc concurrent mark sweep GC freed 75(3KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 2.898ms total 40.899ms 07-21 15:51:00.626 2178-2238/jp.co.sakaguchi E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 1749612 byte allocation with 627616 free bytes and 612KB until OOM" 07-21 15:51:00.642 2178-2238/jp.co.sakaguchi I/art﹕ Alloc sticky concurrent mark sweep GC freed 5(368B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 1.190ms total 8.460ms 07-21 15:51:00.675 2178-2238/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.675 2178-2238/jp.co.sakaguchi I/art﹕ Alloc partial concurrent mark sweep GC freed 27(1504B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 3.525ms total 29.061ms 07-21 15:51:00.731 2178-2238/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.735 2178-2238/jp.co.sakaguchi I/art﹕ Alloc concurrent mark sweep GC freed 7(352B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 2.951ms total 36.983ms 07-21 15:51:00.745 2178-2238/jp.co.sakaguchi I/art﹕ Forcing collection of SoftReferences for 1708KB allocation 07-21 15:51:00.783 2178-2238/jp.co.sakaguchi I/art﹕ Clamp target GC heap from 67MB to 64MB 07-21 15:51:00.783 2178-2238/jp.co.sakaguchi I/art﹕ Alloc concurrent mark sweep GC freed 10(352B) AllocSpace objects, 0(0B) LOS objects, 0% free, 63MB/64MB, paused 3.985ms total 29.294ms 07-21 15:51:00.785 2178-2238/jp.co.sakaguchi E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 1749612 byte allocation with 628496 free bytes and 613KB until OOM" 07-21 15:51:00.793 2178-2238/jp.co.sakaguchi E/Volley﹕ [161] ImageRequest.parseNetworkResponse: Caught OOM for 304176 byte image, url=画像取得先URL 07-21 15:51:00.918 2178-2178/jp.co.sakaguchi E/Event﹕ Could not dispatch event: class jp.co.sakaguchi.model.UserEvent to subscribing class class jp.co.sakaguchi.MainActivity java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at jp.co.sakaguchi.MainActivity.onEventAlert(MainActivity.java:212) at jp.co.sakaguchi.MainActivity.onEvent(MainActivity.java:123) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at de.greenrobot.event.EventBus.invokeSubscriber(EventBus.java:498) at de.greenrobot.event.EventBus.postToSubscription(EventBus.java:429) at de.greenrobot.event.EventBus.postSingleEventForEventType(EventBus.java:410) at de.greenrobot.event.EventBus.postSingleEvent(EventBus.java:383) at de.greenrobot.event.EventBus.post(EventBus.java:263) at jp.co.sakaguchi.model.AppController.onErrorResponse(AppController.java:134) at jp.co.sakaguchi.view.PhotoView$1.onErrorResponse(PhotoView.java:65) at com.android.volley.toolbox.ImageLoader$4.run(ImageLoader.java:476) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 07-21 15:51:00.918 2178-2178/jp.co.sakaguchi D/Event﹕ No subscribers registered for event class de.greenrobot.event.SubscriberExceptionEvent
追記
ListViewはページによりますが最高で1ページに50枚の写真があります。
そこで、1ページ目を1枚目から順に表示させて反対に50枚目から順に表示させるというのを試してみました。
すると、折り返し2枚分はhitしたようです。
LruCache[maxSize=8192,hits=2,misses=50,hitRate=3%]
これは2枚だけキャッシュに存在したということでしょうか?
また、使用量の差がImageLoaderで取得したBitmapのサイズを違うように見えるのですが、
その分はImageViewなどのサイズ分ということになるのでしょうか?
以下のログはその時の一部抜粋です。
07-22 11:48:55.546 3316-3316/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-22 11:48:55.546 3316-3316/jp.co.sakaguchi I/System.out﹕ totalMemory:29048 07-22 11:48:55.546 3316-3316/jp.co.sakaguchi I/System.out﹕ freeMemory:1988 07-22 11:48:55.546 3316-3316/jp.co.sakaguchi I/System.out﹕ useMemory:27061 07-22 11:48:55.963 3316-3316/jp.co.sakaguchi I/System.out﹕ size:1708 07-22 11:48:59.496 3316-3316/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=0,misses=50,hitRate=0%] 07-22 11:48:59.496 3316-3316/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-22 11:48:59.496 3316-3316/jp.co.sakaguchi I/System.out﹕ totalMemory:28711 07-22 11:48:59.496 3316-3316/jp.co.sakaguchi I/System.out﹕ freeMemory:1988 07-22 11:48:59.496 3316-3316/jp.co.sakaguchi I/System.out﹕ useMemory:26724 07-22 11:48:59.499 3316-3316/jp.co.sakaguchi I/System.out﹕ size:1708 07-22 11:49:01.793 3316-3316/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=1,misses=50,hitRate=1%] 07-22 11:49:01.793 3316-3316/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-22 11:49:01.793 3316-3316/jp.co.sakaguchi I/System.out﹕ totalMemory:28476 07-22 11:49:01.793 3316-3316/jp.co.sakaguchi I/System.out﹕ freeMemory:4091 07-22 11:49:01.793 3316-3316/jp.co.sakaguchi I/System.out﹕ useMemory:24385 07-22 11:49:01.800 3316-3316/jp.co.sakaguchi I/System.out﹕ size:1708 07-22 11:49:03.896 3316-3316/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=2,misses=50,hitRate=3%] 07-22 11:49:03.896 3316-3316/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-22 11:49:03.896 3316-3316/jp.co.sakaguchi I/System.out﹕ totalMemory:28476 07-22 11:49:03.896 3316-3316/jp.co.sakaguchi I/System.out﹕ freeMemory:1984 07-22 11:49:03.896 3316-3316/jp.co.sakaguchi I/System.out﹕ useMemory:26492 07-22 11:49:04.305 3316-3316/jp.co.sakaguchi I/System.out﹕ size:1708 07-22 11:49:06.383 3316-3316/jp.co.sakaguchi I/System.out﹕ LruCache[maxSize=8192,hits=2,misses=51,hitRate=3%] 07-22 11:49:06.383 3316-3316/jp.co.sakaguchi I/System.out﹕ maxMemory:65536 07-22 11:49:06.383 3316-3316/jp.co.sakaguchi I/System.out﹕ totalMemory:25336 07-22 11:49:06.384 3316-3316/jp.co.sakaguchi I/System.out﹕ freeMemory:1988 07-22 11:49:06.384 3316-3316/jp.co.sakaguchi I/System.out﹕ useMemory:23348 07-22 11:49:06.490 3316-3328/jp.co.sakaguchi I/art﹕ Background partial concurrent mark sweep GC freed 187(7KB) AllocSpace objects, 2(1983KB) LOS objects, 13% free, 24MB/28MB, paused 14.302ms total 55.078ms 07-22 11:49:06.676 3316-3316/jp.co.sakaguchi I/System.out﹕ size:1708
また、キャッシュ容量に入る入らないは別として一切キャッシュしないというのを試してみたいのですが、putBitmapでは何もしない、getBitmapではnullを返す、といったことをすれば良いのでしょうか?
その状況で試してもmissesなどが増えないこと以外は状況が変わりませんでした。
追記2
@Override
protected void entryRemoved(boolean evicted, String key,
Bitmap oldValue, Bitmap newValue) {
if (!oldValue.isRecycled()) {
oldValue.recycle();
oldValue = null;
}
}
このように古いBitmapの開放処理を書いたところ、
9ページ目の1枚目ではなく16ページ目の1枚目を表示しようとするまでメモリ不足になりませんでした。
メモリ開放は手動で行わなければならないのですね。
これは2枚だけキャッシュに存在したということでしょうか?
そうなります。
やっとLruCache#toStringした時の項目が理解できました。
ありがとうございます。
RuntimeのメソッドでDalvikヒープの状態を知ることができます
私にはその方法がよくわかりませんでした。
試しにApplication用クラスのonCreateでRuntime.getRuntime().traceMethodCalls(true);
と書いてみるとjava.lang.UnsupportedOperationException
が発生しました。
どうすれば知ることができるのでしょうか?
アプリはAndroid4.4以降対応で、普段はAndroid Studio付属(?)のエミュレータのNexus5(API 22)を使用しています。
大量の写真のキャッシュにメモリキャッシュはあまり有効じゃないとのことでディスクキャッシュを試そうと思ったのですが、その方法がよくわかりません。
ImageLoaderでディスクキャッシュを使用しようと思ったらどのようにすれば良いのでしょうか?
追記3
ディスクキャッシュについてはVolleyですでに行われているようですが、デフォルトが5MBらしいので10MB・100MB・1GBに変更して試したものの、どれも改善は見られませんでした。
ヒープダンプを見たところ、やはりbyte[]が一番メモリを消費していることがわかりました。
他にはListView用のAdapterクラスがページの数だけメモリに残っていることなどもわかりました。
Adapterクラスがページの数だけ残っているのはページをBackStackに追加しているからでしょうか?
今はいっそVolleyをやめて他を使うなりライブラリ自体を使わないことも検討しています。