0

So, I've been creating my project and been using the emulator from android studio to run and test my application. Worked perfectly, but when testing on my android phone I have encountered a error:

java.lang.OutOfMemoryError: Failed to allocate a 51321612 byte allocation with 16767424 free bytes 
    and 37MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:700)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:535)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1179)
    at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:770)
    at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:621)
    at android.content.res.Resources.loadDrawable(Resources.java:1727)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:945)
    at android.widget.ImageView.<init>(ImageView.java:157)
    at android.widget.ImageView.<init>(ImageView.java:145)
    at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
    at androidx.appcompat.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
    at androidx.appcompat.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:187) 
    at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:107)
    at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1407)
    at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1457)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:776)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:873
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:873)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:873
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1011)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:427)

I am trying to fix this but need some help. Here is my design code:

   <androidx.cardview.widget.CardView
    android:layout_margin="10dp"
    app:cardCornerRadius="50dp"
    app:cardElevation="0dp"
    android:layout_width="300dp"
    android:layout_height="125dp">

    <ImageView
     android:id="@+id/FullBody"
     tools:ignore="ContentDescription"
     android:src="@drawable/full"
     android:scaleType="centerCrop"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     > </ImageView>

Here is the version of android studio that i am using:

    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
    applicationId "com.example.gymtastic"
    minSdkVersion 17
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 
    multiDexEnabled true;

The phone that i use has a android version of 7.0 which is equivalent to 24Sdk. If more information is required, will provide.

Ace B
  • 19
  • 6
  • Its always good to provide as much context as possible. For example here you are specifying an Android phone but you do not specify what version (Platform) this is just an example of how you can add some helpful context. See here https://stackoverflow.com/help/how-to-ask – Nigel Savage Jan 27 '20 at 19:39
  • Thank you, will do provide. – Ace B Jan 28 '20 at 11:10
  • I can only guess, but this can be a result of heavy or big image you are loading in `ImageView` are you using any lib like `Glide` or something to load image? can I also know size and res of the image? – Rahul Gaur Jan 28 '20 at 11:12
  • I am using a Card View widget that I have found on the internet 'implementation 'androidx.cardview:cardview:1.0.0' and the image res is 1188 x 675 and 96.0 kB – Ace B Jan 30 '20 at 16:18

2 Answers2

0

Usually, this error is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.

You can request for more heap size using this

android:largeHeap="true"

Just put this line in the application element of your Android.Manifest file like this

<application
android:largeHeap="true"
.
.
.>
  • I have implemented this change but did no effect as it crashes when moving onto the screen with the images. – Ace B Jan 30 '20 at 16:18
0

To allocate a specific amount of heap memory to your project: Open the file located at:

C:\Program Files\Android\Android Studio\bin\studio.exe.vmoptions

Change this content to:

-Xms128m
-Xmx4096m
-XX:MaxPermSize=1024m
-XX:ReservedCodeCacheSize=200m
-XX:+UseCompressedOops

"

Xmx specifies the maximum memory allocation pool for a Java Virtual Machine (JVM), while Xms specifies the initial memory allocation pool. Your JVM will be started with Xms amount of memory and will be able to use a maximum of Xmx amount of memory.

In Android Studio 2.0, you can also create/edit this file by accessing "Edit Custom VM Options" from the Help menu.

Answer by Wesley on Feb 26 '15 Android Studio - How to increase Allocated Heap Size

Mart Wienk
  • 361
  • 3
  • 5
  • Is the location found within the user android studio file or within the Program Files – Ace B Jan 30 '20 at 16:19
  • Within the Program Files. I saw that the path in my initial answer was incorrect, so I changed it to the right path this time. – Mart Wienk Jan 31 '20 at 13:36