Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

external allocation too large for this process,Error inflating class, bitmap size exceeds VM budget

Posted my problem here too.

A beginner..I am doing a school map app.

First of all I have background with 4 buttons ( level3, level4, level5, level6).

I try and click all the buttons, normally after clicking 3 of it, the next one will crash.(each level button goes into a level map, whereby clicking the room image buttons, I will enter into the gallery-imageswitcher)

I have at least 5 ImageSwitchers in each level maps,so that about over 20 ImageSwitcher galleries overall.

I did all my stuffs using Drawable instead of Bitmap. I have tons and tons of images (because its a school map, show all the photos in my school, and all those backgrounds and ImageButton)

I did some research, and those who have problems are those using bitmap. Some of the ways that can be solved is system.gc();, and bitmap.recycle(not so sure how to use it though)

In my main background - starttour.java

 public class StartTour extends Activity {  
       private SoundManager mSoundManager;  
       /** Called when the activity is first created. */  
       @Override  
       public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.ttour);  

           mSoundManager = new SoundManager();  
           mSoundManager.initSounds(getBaseContext());  
           mSoundManager.addSound(1, R.raw.beep);  

           //play music  
           final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.jazz);  
             mPlayer.start();  

           final Button button = (Button) findViewById(R.id.back);  

           button.setOnClickListener(new Button.OnClickListener() {    

               // Implement the OnClickListener callback  
               public void onClick(View v) {  
                 // do something when the button is clicked           
                   startActivity(new Intent("android.com.Android"));   
                   mSoundManager.playSound(1);  
                   mPlayer.stop();  
               }  
           });  

               final Button button1 = (Button) findViewById(R.id.level3);  
               button1.setOnClickListener(new Button.OnClickListener() {  
                   public void onClick(View v) {  
                       // Perform action on click  
                        startActivity(new Intent("android.com.Lvl3"));  
                        mSoundManager.playSound(1);  
                        mPlayer.stop();  

                   }  
                });  

               final Button button2 = (Button) findViewById(R.id.level4);  
               button2.setOnClickListener(new Button.OnClickListener() {  
                   public void onClick(View v) {  
                       // Perform action on click  
                        startActivity(new Intent("android.com.Lvl4"));  
                        mSoundManager.playSound(1);  
                        mPlayer.stop();  

                   }  
                });              


               final Button button3 = (Button) findViewById(R.id.level5);  
               button3.setOnClickListener(new Button.OnClickListener() {  
                   public void onClick(View v) {  
                       // Perform action on click  
                        startActivity(new Intent("android.com.Lvl5"));  
                        mSoundManager.playSound(1);  
                        mPlayer.stop();  

                   }  
                });  

               final Button button4 = (Button) findViewById(R.id.level6);  
               button4.setOnClickListener(new Button.OnClickListener() {  
                   public void onClick(View v) {  
                       // Perform action on click  
                        startActivity(new Intent("android.com.Lvl6"));  
                        mSoundManager.playSound(1);  
                        mPlayer.stop();  

                   }  
                });  
       }  
   }





<ImageView android:id="@+id/ilevel4" android:layout_width="wrap_content"
                android:layout_height="fill_parent" android:src="@drawable/level4map"
                android:layout_gravity="center"/>

<Button
        android:id="@+id/m412button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="92.5px"
        android:layout_marginTop="95px"
        android:background="@drawable/m412_button" />    

<Button
        android:id="@+id/m411button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="92.5px"
        android:layout_marginTop="248.8px"
        android:background="@drawable/m411_button" />

<Button
        android:id="@+id/m410button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="92.5px"
        android:layout_marginTop="334.8px"
        android:background="@drawable/m410_button" />   


<Button
        android:id="@+id/m405button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6px"
        android:layout_marginTop="246.5px"
        android:background="@drawable/m405_button" />

<Button
        android:id="@+id/m406button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6px"
        android:layout_marginTop="335px"
        android:background="@drawable/m406_button" />  

<Button
        android:id="@+id/m419button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260px"
        android:layout_marginTop="59.5px"
        android:background="@drawable/m419_button" />      

<Button
        android:id="@+id/m420button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260px"
        android:layout_marginTop="168.5px"
        android:background="@drawable/m420_button" />   

<Button
        android:id="@+id/m421button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260px"
        android:layout_marginTop="240.5px"
        android:background="@drawable/m421_button" />

<Button
        android:id="@+id/m422button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="260px"
        android:layout_marginTop="335px"
        android:background="@drawable/m422_button" />              
</RelativeLayout>

In my level 4.xml

     <ImageView android:id="@+id/ilevel4" android:layout_width="wrap_content"  
                    android:layout_height="fill_parent" android:src="@drawable/level4map"  
                    android:layout_gravity="center"/>  

   <Button  
           android:id="@+id/m412button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="92.5px"  
           android:layout_marginTop="95px"  
           android:background="@drawable/m412_button" />      

   <Button  
           android:id="@+id/m411button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="92.5px"  
           android:layout_marginTop="248.8px"  
           android:background="@drawable/m411_button" />  

   <Button  
           android:id="@+id/m410button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="92.5px"  
           android:layout_marginTop="334.8px"  
           android:background="@drawable/m410_button" />     


   <Button  
           android:id="@+id/m405button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="6px"  
           android:layout_marginTop="246.5px"  
           android:background="@drawable/m405_button" />  

   <Button  
           android:id="@+id/m406button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="6px"  
           android:layout_marginTop="335px"  
           android:background="@drawable/m406_button" />    

   <Button  
           android:id="@+id/m419button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="260px"  
           android:layout_marginTop="59.5px"  
           android:background="@drawable/m419_button" />        

   <Button  
           android:id="@+id/m420button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="260px"  
           android:layout_marginTop="168.5px"  
           android:background="@drawable/m420_button" />     

   <Button  
           android:id="@+id/m421button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="260px"  
           android:layout_marginTop="240.5px"  
           android:background="@drawable/m421_button" />  

   <Button  
           android:id="@+id/m422button"  
           android:layout_width="wrap_content"  
           android:layout_height="wrap_content"  
           android:layout_marginLeft="260px"  
           android:layout_marginTop="335px"  
           android:background="@drawable/m422_button" />                   
   </RelativeLayout>

In level4.java

  public class Level4 extends Activity {  
        /** Called when the activity is first created. */  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.tlevel4);  

            final Button button1 = (Button) findViewById(R.id.m405button);  
            button1.setOnClickListener(new OnClickListener() {  
               public void onClick(View v) {  
                       // Perform action on click  
                    startActivity(new Intent("android.com.M405"));  
               }  
           });  

           final Button button2 = (Button) findViewById(R.id.m406button);  
           button2.setOnClickListener(new OnClickListener() {  
               public void onClick(View v) {  
                       // Perform action on click  
                    startActivity(new Intent("android.com.M406"));  
               }  
           });  
           }  
         }

In logcat

11-03 12:35:55.194: ERROR/dalvikvm-heap(708): 483406-byte external allocation too large for this process.
11-03 12:35:55.194: ERROR/(708): VM won't let us allocate 483406 bytes
11-03 12:35:55.204: DEBUG/AndroidRuntime(708): Shutting down VM
11-03 12:35:55.213: WARN/dalvikvm(708): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
11-03 12:35:55.213: ERROR/AndroidRuntime(708): Uncaught handler: thread main exiting due to uncaught exception
11-03 12:35:55.254: ERROR/AndroidRuntime(708): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.com/android.com.Level3}: android.view.InflateException: Binary XML file line #7: Error inflating class java.lang.reflect.Constructor
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.os.Looper.loop(Looper.java:123)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.main(ActivityThread.java:3948)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Method.invoke(Method.java:521)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at dalvik.system.NativeStart.main(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class java.lang.reflect.Constructor
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.createView(LayoutInflater.java:512)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:562)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.Activity.setContentView(Activity.java:1626)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.com.Level3.onCreate(Level3.java:19)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): ... 11 more
11-03 12:35:55.254: ERROR/AndroidRuntime(708): Caused by: java.lang.reflect.InvocationTargetException
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.widget.ImageView.<init>(ImageView.java:103)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Constructor.constructNative(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.view.LayoutInflater.createView(LayoutInflater.java:499)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): ... 22 more
11-03 12:35:55.254: ERROR/AndroidRuntime(708): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:363)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:212)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:663)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.content.res.Resources.loadDrawable(Resources.java:1637)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.content.res.TypedArray.getDrawable(TypedArray.java:548)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): at android.widget.ImageView.<init>(ImageView.java:113)
11-03 12:35:55.254: ERROR/AndroidRuntime(708): ... 26 more

If I click any of the buttons first and enter into the levelmap, it wil crash while im looking at the imageswitcher pictures.

In logcat

11-03 09:17:34.308: INFO/ActivityManager(564): Starting activity: Intent { action=android.com.M615 comp={android.com/android.com.M615_gallery} }
11-03 09:17:34.638: ERROR/dalvikvm-heap(712): 212800-byte external allocation too large for this process.
11-03 09:17:34.638: ERROR/(712): VM won't let us allocate 212800 bytes
11-03 09:17:34.638: DEBUG/AndroidRuntime(712): Shutting down VM
11-03 09:17:34.648: WARN/dalvikvm(712): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
11-03 09:17:34.648: ERROR/AndroidRuntime(712): Uncaught handler: thread main exiting due to uncaught exception
11-03 09:17:34.677: ERROR/AndroidRuntime(712): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:363)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:212)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:663)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.content.res.Resources.loadDrawable(Resources.java:1637)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.content.res.Resources.getDrawable(Resources.java:535)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.ImageView.resolveUri(ImageView.java:482)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.ImageView.setImageResource(ImageView.java:268)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.com.M615_gallery$AddImgAdp.getView(M615_gallery.java:71)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.makeAndAddView(Gallery.java:754)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.fillToGalleryRight(Gallery.java:703)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.layout(Gallery.java:634)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.Gallery.onLayout(Gallery.java:339)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.View.layout(View.java:6133)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.ViewRoot.performTraversals(ViewRoot.java:929)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.view.ViewRoot.handleMessage(ViewRoot.java:1482)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.os.Looper.loop(Looper.java:123)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at android.app.ActivityThread.main(ActivityThread.java:3948)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at java.lang.reflect.Method.invoke(Method.java:521)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-03 09:17:34.677: ERROR/AndroidRuntime(712): at dalvik.system.NativeStart.main(Native Method)

really really appreciate if someone can help..thanks

PS: I have tons of images in the app. I have at-least 5 ImageSwitcher gallerys in each level, so that kinda adds up to over 20 imageswitchers in my app.

like image 585
Choo Yan Avatar asked Nov 06 '22 07:11

Choo Yan


1 Answers

I've got to do some guesswork here, because your naming and line numbers don't really match up.

You have this line, for example:

startActivity(new Intent("android.com.Lvl4"));

But your activity is named "android.com.Level4". You're inflating layout "tlevel4" when you call it "level4.xml". Also, the error you pasted is for Level3. Maybe they're similar, but the line numbers matter, so it would have been better to post the same error & class.

I'm not trying to be picky, those details just make a big difference when trying to decipher code you aren't familiar with.

Anyway, in this case I think it's pretty clear what's going on.

Error 1 & 2 Both of these errors are out of memory exceptions. In both, you're asking the OS to give you memory for loading some large resource, and the OS is simply telling you you've had enough. If you find it's happening consistently, it's almost certain that you're leaking memory somewhere. You mention using drawables extensively. You want to be really careful what you do with those, because when you add a drawable to a view it retains a reference to the view... which has a reference to the Context. I don't see anything glaringly obvious in the code you posted, but I'd bet you have some sort of Context leak. Read this article and see if anything they talk about rings a bell.

Also, I notice your package name is android.com (ie android.com.Lvl3). Maybe you just edited this for posting, but it should really start with com, ie com.yourProjectName.Lvl3.

Oh, and please don't do this:

startActivity(new Intent("android.com.Android"));

Just call this.finish();

like image 93
DougW Avatar answered Nov 15 '22 10:11

DougW