0

I have an app that works perfectly when run on my phone device. However, it crashes when run on the emulator.


  2021-01-11 06:58:24.719 19783-19783/com.example.myapplication E/RecyclerView: No adapter attached; skipping layout
2021-01-11 06:58:35.827 19783-19783/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 19783
    java.lang.OutOfMemoryError: Failed to allocate a 599752036 byte allocation with 4194304 free bytes and 199MB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155)
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
        at android.content.res.Resources.loadDrawable(Resources.java:858)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
        at android.widget.ImageView.<init>(ImageView.java:162)
        at android.widget.ImageView.<init>(ImageView.java:150)
        at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:74)
        at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:69)
        at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:199)
        at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:119)
        at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1551)
        at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1602)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
  at com.example.myapplication.Fragments.Friends_List$DisplayAllFriends$adapter$1.onCreateViewHolder(Friends_List.kt:495)

The problem is pointing to my friends list xml file but everything looks ok and runs fine on my actual phone..

kotalina
  • 7
  • 3
  • "java.lang.OutOfMemoryError: Failed to allocate a 599752036 byte " ... that would be 599MB. Not all PCs are configured with a large RAM and swapspace. Use the "top" command to check swapspace usage. – Gyro Gearloose Jan 11 '21 at 10:01
  • try with increasing ram size maybe it is low https://stackoverflow.com/a/40068396/7216511 – PJain Jan 11 '21 at 12:47
  • If you're trying to load a 599MB bitmap you're going to have problems! Especially if you're loading it from resources. Why do you have such a big image, what is it? – cactustictacs Jan 11 '21 at 15:19
  • @cactustictacs I have a lot of the same icons on 3 XML files repeated... I have "if" statements that make them either visible or invisible. I guess I could maybe use Picasso instead? – kotalina Jan 12 '21 at 06:02
  • @kotalina one of your ``ImageView``s in that layout is loading a **huge** ``drawable`` - possibly something like a 4000x4000 pixel image. The problem is the system is trying to decode that image into a bitmap, which requires 599MB of memory (that is *a lot* for one image). I'm guessing you have an image that's supposed to be small on the screen, but the file you're using is extremely big. You need to resize your resources to the appropriate size for how they'll be displayed, ideally at different densities: https://developer.android.com/training/multiscreen/screendensities#TaskProvideAltBmp – cactustictacs Jan 12 '21 at 15:52
  • 1
    @cactustictacs thank you so much. I resized the images and I'm no longer getting the error :) can you leave your response as an 'Answer' so I can mark it as solved? thanks – kotalina Jan 13 '21 at 06:13
  • @kotalina no probs, done! Glad you got it sorted out – cactustictacs Jan 13 '21 at 15:10

2 Answers2

1

edit Menifest android:hardwareAccelerated="false" , android:largeHeap="true":

<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">

Link: Solution

  • After updating my manifest I'm still getting the same error... – kotalina Jan 11 '21 at 07:27
  • It sounds like you are either caching too many files in RAM or instantiating the same bitmaps and videos over and over until you run out of memory. You should not write your own image loading and caching code on Android in 2016 unless you have unusual requirements. Use one of the libraries that have solved this problem. See Picasso v/s Imageloader v/s Fresco vs Glide for more guidance. – Jay Kundaliya Jan 11 '21 at 08:26
0

Answered in the comments, but the issue is that it's trying to allocate memory for a 599MB bitmap, which is very huge! It's probably a very large image - 4000 x 4000 x 32 bits gets you into the ballpark. It might not be that large in filesize (JPG and PNG can be very efficient) but they unpack into a bitmap, and that memory use depends on the dimensions of the image.

You probably shouldn't be using images that size anyway (or if you are, you might need to look into more efficient ways of displaying them) - but from what it sounds like, you have a list of stuff, and the images should be fairly small. So you definitely don't want to be using huge ones!

Really, you need to work out how large the images should be on the screen (in dp, same as every other View is measured in, so it's easy to compare) and then resize them to the appropriate size in pixels. Here's a description of the different densities (with dp to pixel conversions - ideally you'll have a version for each density, resized perfectly and optimised (e.g. you might want to apply sharpening on the smaller ones)

It's not just about optimising the images for display either - constantly scaling large images will make your app run worse, and including resources that are way bigger than you actually need will bloat the APK / App Bundle size. So it's definitely good practice to have images that are exactly what you need!

cactustictacs
  • 17,935
  • 2
  • 14
  • 25