Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to avoid OutOfMemoryError trouble in Android

How to avoid OutOfMemoryError trouble in Android

- ExoPlayer
- Bitmap
- Glide
- Tools (Leak Canary, Stetho, Takt)

Daichi Furiya (Wasabeef)

October 14, 2016
Tweet

More Decks by Daichi Furiya (Wasabeef)

Other Decks in Programming

Transcript

  1. 10-14 00:38:00.683 13057-13057/tv.abema E/AndroidRuntime: FATAL EXCEPTION: main Process: tv.abema.debug, PID:

    13057 java.lang.OutOfMemoryError: THIS IS FAKE at tv.abema.components.activity.MainActivity.onCreate(MainActivity.java:366) at android.app.Activity.performCreate(Activity.java:6664) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
  2. Java HeapSize Model getMemoryClass getLargeMemoryClass Nexus 5X 192MB 512MB Xperia

    Z1 (SOL23) 192MB 512MB Galaxy Nexus 96MB 256MB Nexus 7 (2013) 192MB 512MB Nexus 7 (2012) 64MB 384MB Nexus S 48MB 128MB
  3. Distribution (AbemaTV) Version Codename Distribution Android 6.0 Marshmallow 28.38% Android

    5.0 Lollipop 22.72% Android 4.4 KitKat 20.09% Android 5.1 Lollipop 13.60% Android 4.2 Jelly Bean 9.60% Android 4.1 Jelly Bean 4.29% Android 4.3 Jelly Bean 0.79% Android 7.0 Nougat 0.52% Android 7.1 Nougat 0.00% Android 4.0.3 - 4.0.4 Ice Cream Sandwich 0.00% 4.1 4.2 5.1 4.4 5.0 6.0
  4. Distribution (AbemaTV) Model Brand Distribution Xperia Z3 (SO-01G) Sony 2.81%

    Xperia Z3 (SOL26) Sony 2.40% Xperia Z5 (SOV32) Sony 2.39% Xperia Z5 (SO-01H) Sony 2.10% Z3 Compact (SO-02G) Sony 1.96% Qua tab 01 (KYT31) 京セラ 1.47% Xperia Z4 (SOV31) Sony 1.47% Galaxy S5 (SC-04F) Samsung 1.45% Nexus 7 (2013) Google 1.45% Xperia A (SO-04E) Sony Ericsson 1.37% その他 その他 81.13% その他 Xperia A (SO-04E) Nexus 7 (flo) Galaxy S5 (SC-04F) Xperia Z4 (SOV31) Qua tab 01 (KYT31) Z3 Compact (SO-02G) Xperia Z5 (SO-01H) Xperia Z5 (SOV32) Xperia Z3 (SOL26) Xperia Z3 (SO-01G)
  5. 1080p (bandwidth = 4200000) 720p (bandwidth = 2200000) 480p (bandwidth

    = 1400000) 360p (bandwidth = 900000) 240p (bandwidth = 300000) Video Resolution
  6. Video Resolution 解像度 Wi-Fi Mobile xxxhdpi 1080p 360p 560 dpi

    1080p 360p xxhdpi 720p 360p 400 dpi 720p 360p xhdp 480p 240p hdp 360p 240p mdp 240p 240p
  7. Supported Image Formats Format Encoder Decoder Details File Type JPEG

    ◯ ◯ Base Progressive .jpg GIF ◯ .gif PNG ◯ ◯ .png BMP ◯ .bmp WebP ◯ 4.+ ◯ 4.+ .webp
  8. inSampleSize public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int

    reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; }
  9. inSampleSize public static Bitmap decodeSampledBitmapFromResource( Resources res, int resId, int

    reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); }
  10. GlideModule public class GlideModule extends OkHttpGlideModule { @Inject OkHttpClient client;

    @Override public void applyOptions(Context context, GlideBuilder builder) { builder.setMemoryCache(new LruResourceCache(getMaxCacheSize(context))) .setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); } private int getMaxCacheSize(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); return (activityManager.getMemoryClass() * 1024 * 1024) / 4; } }
  11. GlideModule public class GlideModule extends OkHttpGlideModule { @Inject OkHttpClient client;

    @Override public void applyOptions(Context context, GlideBuilder builder) { builder.setMemoryCache(new LruResourceCache(getMaxCacheSize(context))) .setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); } private int getMaxCacheSize(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); return (activityManager.getMemoryClass() * 1024 * 1024) / 4; } }
  12. Thank you. twitter.com/wasabeef_jp wasabeef.jp github.com/wasabeef Photo by Japan Travel And

    Tourism Association is licensed under CC BY 4.0 International