2

I'm using 150 DPI for my images. In my opinion, they really aren't that big and it's quite a large project. How can I increase virtual memory in Android app?

Here is what goes on in the console:

W/art: Throwing OutOfMemoryError "Failed to allocate a 409534333 byte allocation with 9927994 free bytes and 177MB until OOM"
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: app.com.example.android.brainboost, PID: 3817
                  java.lang.OutOfMemoryError: Failed to allocate a 409534333 byte allocation with 9927994 free bytes and 177MB until OOM
                      at com.android.tools.fd.runtime.FileManager.readRawBytes(FileManager.java:831)
                      at com.android.tools.fd.runtime.FileManager.checkInbox(FileManager.java:249)
                      at com.android.tools.fd.runtime.BootstrapApplication.createResources(BootstrapApplication.java:126)
                      at com.android.tools.fd.runtime.BootstrapApplication.attachBaseContext(BootstrapApplication.java:235)
                      at android.app.Application.attach(Application.java:187)
                      at android.app.Instrumentation.newApplication(Instrumentation.java:1020)
                      at android.app.Instrumentation.newApplication(Instrumentation.java:1004)
                      at android.app.LoadedApk.makeApplication(LoadedApk.java:573)
                      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4749)
                      at android.app.ActivityThread.access$1600(ActivityThread.java:163)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5491)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Here is my code:

package app.com.example.android.brainboost;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;

public class delta extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delta);
    }

    MediaPlayer mp3;
    public void level_eight(View view){

        mp3 = MediaPlayer.create(this, R.raw.d_zero_five);

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    mp3.pause();
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    mp3.start();
                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    mp3.pause();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

    public void level_seven(View view){

        mp3 = MediaPlayer.create(this, R.raw.d_one);

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    mp3.pause();
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    mp3.start();
                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    mp3.pause();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

    public void level_six(View view){

        mp3 = MediaPlayer.create(this, R.raw.d_one_five);

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    mp3.pause();
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    mp3.start();
                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    mp3.pause();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

    public void level_five(View view){

        mp3 = MediaPlayer.create(this, R.raw.d_two);

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    mp3.pause();
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    mp3.start();
                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    mp3.pause();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

    public void level_four(View view){

        mp3 = MediaPlayer.create(this, R.raw.d_two_five);

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    mp3.pause();
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    mp3.start();
                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    mp3.pause();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

    public void level_three(View view){

        mp3 = MediaPlayer.create(this, R.raw.d_three);

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    mp3.pause();
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    mp3.start();
                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    mp3.pause();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

    public void level_two(View view){

        mp3 = MediaPlayer.create(this, R.raw.d_three_five);

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    mp3.pause();
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    mp3.start();
                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    mp3.pause();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

    public void level_one(View view){

        mp3 = MediaPlayer.create(this, R.raw.d_four);

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    mp3.pause();
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    mp3.start();
                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    mp3.pause();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        };
        TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if(mgr != null) {
            mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_delta"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="app.com.example.android.brainboost.delta"
    android:background="#FFFFFF">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:srcCompat="@drawable/dlv_8"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/lv8"
        android:onClick="level_eight" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:srcCompat="@drawable/dlv_7"
        android:layout_below="@+id/lv8"
        android:layout_centerHorizontal="true"
        android:id="@+id/lv7"
        android:onClick="level_seven" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:srcCompat="@drawable/dlv_6"
        android:id="@+id/lv6"
        android:layout_below="@+id/lv7"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="level_six" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:srcCompat="@drawable/dlv_5"
        android:layout_below="@+id/lv6"
        android:layout_centerHorizontal="true"
        android:id="@+id/lv5"
        android:onClick="level_five" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:srcCompat="@drawable/dlv_4"
        android:layout_below="@+id/lv5"
        android:layout_centerHorizontal="true"
        android:id="@+id/lv4"
        android:onClick="level_four" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:srcCompat="@drawable/dlv_3"
        android:id="@+id/lv3"
        android:layout_below="@+id/lv4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="level_three" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:srcCompat="@drawable/dlv_2"
        android:id="@+id/lv2"
        android:layout_below="@+id/lv3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="level_two" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:srcCompat="@drawable/dlv_1"
        android:id="@+id/lv1"
        android:onClick="level_one"
        android:layout_below="@+id/lv2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        app:srcCompat="@drawable/logo"
        android:id="@+id/logo"
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
okunato
  • 63
  • 1
  • 8
  • You say "Failed to allocate a 409,534,333 byte", this is not big ? You show an android logcat but only talk about android studio VM. Please make it clearer what you want. – Jon Goodwin Jan 13 '17 at 00:25

2 Answers2

2

The only way to increase the memory used by the application is setting android:largeHeap="true" in the application tag of the Manifest. Obviously setting this has some disadvantages which you can read here.

If you get a OOM (OutOfMemory) error, 99% it's your fault in handling app memory. If you need to read a big file (maybe because you need to parse content) don't load it enterily in the RAM, but read it at fixed block of bytes at a time.

Without seeing your code or without knowing your intention we can't be more usefull

To speed up up Android Studio you can increase the RAM dedicated to the JVM. Read here for more infos here and here

Community
  • 1
  • 1
MatPag
  • 41,742
  • 14
  • 105
  • 114
  • Do you have any guides on how to properly display an image? I just drag the images into the imageview – okunato Jan 13 '17 at 00:43
  • add some codes to your question, and i will take a look at what you are doing wrong with your image (maybe the problem it's simply your image, which could be too big to be displayed in an ImageVIew) – MatPag Jan 13 '17 at 00:45
0

I fixed the problem by changing the images from "match-parent" to their proper dp.

okunato
  • 63
  • 1
  • 8