How to solve OutOfMemoryError in android?

I prepared no.of drawable animations.when launching the first animation application will start.i there are two buttons (next and previous) with the same activity. When I click on the next button, I get an exception, for example,

java.lang.OutOfMemoryError: bitmap size exceeds VM budget 

My code

To get drawable animations using drawable,

 private void getAnimationForLetters(int mAnim) { // TODO Auto-generated method stub String anim = "capital_anim_" + new Integer(mAnim).toString(); final int resID = getApplicationContext().getResources().getIdentifier( anim, "drawable", "my-package-name"); mImgLetter.clearAnimation(); mImgLetter.setBackgroundResource(resID); mImgLetter.post(new Runnable() { public void run() { loadingAnimation = (AnimationDrawable) mImgLetter .getBackground(); loadingAnimation.start(); } }); } 

my next button code,

 case R.id.btnNext_: unbindDrawables(findViewById(R.id.imgLetter)); mLetterNum=mLetterNum+1; getAnimationForLetters(mLetterNum); 

my invind drawable method code,

 private void unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } } 

Finally, I got the java.lang.OutOfMemoryError: bitmap size exceeds VM budget exception java.lang.OutOfMemoryError: bitmap size exceeds VM budget The mImgLetter.setBackgroundResource(resID); exception is displayed here mImgLetter.setBackgroundResource(resID); Please help me.

I added the following code to clean up,

 loadingAnimation.stop(); for (int i = 0; i < loadingAnimation.getNumberOfFrames(); ++i){ Drawable frame = loadingAnimation.getFrame(i); if (frame instanceof BitmapDrawable) { ((BitmapDrawable)frame).getBitmap().recycle(); } frame.setCallback(null); } loadingAnimation.setCallback(null); 

It works fine only for the next or previous. First click on the next animation, moving to the second animation, the second time, if I click on the previous button, I get an exception, for example,

 Canvas: trying to use a recycled bitmap android.graphics.Bitmap 

please help me.

+4
source share
4 answers

for sure - the system knows when to collect garbage, so a call that does not help at all.

You really need to manage the memory of your applications. 290x330 may not seem like much, but if you use full RGBA, then 4 bytes per pixel, and your image will turn into 380K. If you have several, you will run out of memory. Most Android devices are not like PCs - they have fairly limited memory. Some have only 16M or 24M to run everything, including the OS and any other applications that the user can run at the same time.

You can try wrapping your program to load resources with try / catch

  While( ! success ) { try { // load resources } catch (OutOfMemoryError E) { // do something to use less memory and try again } } 
+3
source

This can happen if you use high resolution images for lower resolution mobile phones and memory. Use the files Drawable-hdpi, Drawable-mdpi, Drawable-ldpi and place the images with the appropriate resolution.

FYI .. basically you can also see this error in the emulator when heapSize is too smaller. Try increasing heap size with AVD Manager

This link can help you support multiple screen resolutions.

+2
source

Take a look at the link below for a detailed description of the lack of memory.

Android memory outage issue

For me, Thomas’s decision has passed in the past.

Hope this helps.

0
source

you can use this to free memory:

 system.gc(); 
-2
source

Source: https://habr.com/ru/post/1399970/


All Articles