java.lang.OutOfMemoryError when calling getDrawable() - java

I get the following out of memory error and I'm not sure why.
Failed to allocate a 1860508 byte allocation with 905740 free bytes and 884KB until OOM
java.lang.OutOfMemoryError: Failed to allocate a 1860508 byte allocation with 905740 free bytes and 884KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:837)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:656)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1037)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4056)
at android.content.res.Resources.loadDrawable(Resources.java:3929)
at android.content.res.Resources.getDrawable(Resources.java:1991)
at android.content.res.Resources.getDrawable(Resources.java:1973)
at android.graphics.drawable.LevelListDrawable.inflate(LevelListDrawable.java:128)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1192)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1086)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4045)
at android.content.res.Resources.loadDrawable(Resources.java:3929)
at android.content.res.Resources.getDrawable(Resources.java:1991)
at android.content.res.Resources.getDrawable(Resources.java:1973)
at android.content.Context.getDrawable(Context.java:409)
at com.MyCompany.MyApp.Login.updateWifiStatus(Login.java:942)
at com.MyCompany.MyApp.Login$6.run(Login.java:576)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6939)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
I am using a 5 level resource to indicate wifi status and here is the code I'm using
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
// Level of current connection
int rssi = wifiManager.getConnectionInfo().getRssi();
int level = WifiManager.calculateSignalLevel(rssi, 5);
Drawable db = getDrawable(R.drawable.stat_sys_wifi_signal);
db.setLevel(level);
wifiMenu.setIcon(db);
The images are all the same size (512x512).
In reference to other answers out there I have got largeheap in my manifest.
Any help would be greatly appreciated.

Use TinyPNG
TinyPNG uses smart lossy compression techniques to reduce the file
size of your PNG files. By selectively decreasing the number of colors
in the image, fewer bytes are required to store the data. The effect
is nearly invisible but it makes a very large difference in file size!
Compress all images using this link.
Reduce the size of all picture before use in your Android applications.
Make sure you read the document Loading Large Bitmaps Efficiently given by #mudit_sen.
Hope this may help you.

Related

android.os.TransactionTooLargeException: data parcel size NOUGAT ERROR

I am making an api call using RETROFIT, I got the JSON response from the server which is hardly 30kb in size. While the JSON parses the response into a complex Object, it is throwing this error.
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 1098852 bytes
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:161)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.os.TransactionTooLargeException: data parcel size 1098852 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(BinderProxy.java:510)
at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4524)
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
If I try to store the response in an Object class, and then get values via LinkedHashMap, I am able to fetch the data. But, it will be a tedious process.
Can anyone please help me to figure out what the issue here is and how to find a solution ?
According to my research this only happens to android devices with version >= Nougat. Below that, it works like a piece of cake.
I have also tried some of the solutions mentioned in:
--> onSaveInstanceState.clear() - but this does not work.
you can find the post here
This is the typical error when you put a large amount of data into Bundle of Android.
I would suggest you do it right. The large data has to persistent to disk (either database or files) and you only pass the reference of this data to other places to consume it.
Here you may find 30kb is small, but the actual configuration of size limit for Bundle may be various from different devices/vendors configurations.
The Bundle was meant to keep simple and small data only, don't over-abuse it.

java.lang.OutofMemoryError issues [duplicate]

This question already has an answer here:
java.lang.OutOfMemoryError: Failed to allocate a 31961100 byte allocation with 15257484 free bytes and 14MB until OOM
(1 answer)
Closed 6 years ago.
I'm developing a little game app for my collage project. The moment I'm at the point that my app runs smoothly for the first and second round it crashes after that along with this error java.lang.OutofMemoryError. I can't find the issue to this problem.
if (pic == null)
pic = BitmapFactory.decodeResource(context.getResources(), R.drawable.background);
scalepic = Bitmap.createScaledBitmap(pic, 720, 3840, false);
pic.recycle();
background = new Background(scalepic);
and this is the error:
java.lang.OutOfMemoryError: Failed to allocate a 117964812 byte allocation with 16777216 free bytes and 18MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:703)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:733)
at de.hs_kl.imst.gatav.runner.GameView.<init>(GameView.java:88)
at de.hs_kl.imst.gatav.runner.Start.onCreate(Start.java:39)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
if i erase the recycle, then the game crushes after the first attempt...
You can call System.gc(); method to call garbage collector as you want.

App crash on large string in TextView

I am trying to display 4 long pieces of text (about 250 chars long each) inside a TextView separated by a few images on android. This is inside a fragment which is placed in a scrollView on the Host activity to make it scrollable.
But it throws
java.lang.OutOfMemoryError
I was surprised to see that the app went out of memory by this because the only thing it has to do is display 250 characters and apply word wrap to it. So after googling it I set android:largeHeap to true to see what would happen. The application does load the text but it uses a lot of CPU resources and makes the UI come to a snail phase.
So I am wondering: what I am doing wrong here?
Thanks for reading.
Here is the complete crash:
java.lang.OutOfMemoryError: Failed to allocate a 69120012 byte
allocation with 8388608 free bytes and 10MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at
android.content.res.Resources.loadDrawableForCookie(Resources.java:2730)
at android.content.res.Resources.loadDrawable(Resources.java:2635)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.widget.ImageView.(ImageView.java:152)
at android.widget.ImageView.(ImageView.java:140)
at
android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:57)
at
android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:53)
at
android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at
android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:980)
at
android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1039)
at
android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at
android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at
android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at
nl.evhb.evhbmijnopbrengst.FragmentCoTwoInformation.onCreateView(FragmentCoTwoInformation.java:25)
at
android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at
android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I am sorry but after looking into the log again I realized that I imported the images on full 1080p resolution which is too much to process for android.graphics.BitmapFactory and that is why it goes out of memory.
I made the images to a mobile friendly size and it worked fine.
OutOfMemoryErrorenter code here error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space.
Try it...
<application
.......................
............
android:largeHeap="true"
>

Card.io java.lang.OutOfMemoryError

Sometimes after the card is captured the app will crash.
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Bitmap.java)
at android.graphics.Bitmap.createBitmap(Bitmap.java:924)
at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
at android.graphics.Bitmap.createBitmap(Bitmap.java:868)
at io.card.payment.ah.a()
at io.card.payment.CardIOActivity.a()
at io.card.payment.CardScanner.onPreviewFrame()
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1016)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(NativeStart.java)
It happens every time i capture on the Galaxy S5.
Here are the parameters which I'm using to start scan intent:
Intent scanIntent = new Intent(CardInfoActivity.this, CardIOActivity.class);
scanIntent.putExtra(CardIOActivity.EXTRA_APP_TOKEN, Constants.CARD_IO_TOKEN);
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, true);
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, true);
scanIntent.putExtra(CardIOActivity.EXTRA_SUPPRESS_CONFIRMATION, true);
scanIntent.putExtra(CardIOActivity.EXTRA_SUPPRESS_MANUAL_ENTRY, true);
startActivityForResult(scanIntent, MY_SCAN_REQUEST_CODE);
Maybe anyone knows how to solve this problem?
Android bitmaps keep the image in native memory, not on the Java heap. If you allocate a bunch of images quickly this can happen, since cleanup only happens when the Java GC runs, which is tied to the Java heap. Ensuring you call bitmap.recycle() when you're done with images you've been working with can help.

java.lang.RuntimeException: Could not read input channel file descriptors from parcel

I am using crashlytics to track my app's crash. There is one bug which is quite hard to figure out. The stack trace from crashlytics is as below:
java.lang.RuntimeException: Could not read input channel file descriptors from parcel.
at android.view.InputChannel.nativeReadFromParcel(InputChannel.java)
at android.view.InputChannel.readFromParcel(InputChannel.java:148)
at android.view.InputChannel$1.createFromParcel(InputChannel.java:39)
at android.view.InputChannel$1.createFromParcel(InputChannel.java:36)
at com.android.internal.view.InputBindResult.<init>(InputBindResult.java:62)
at com.android.internal.view.InputBindResult$1.createFromParcel(InputBindResult.java:102)
at com.android.internal.view.InputBindResult$1.createFromParcel(InputBindResult.java:99)
at com.android.internal.view.IInputMethodManager$Stub$Proxy.windowGainedFocus(IInputMethodManager.java:851)
at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1292)
at android.view.inputmethod.InputMethodManager.onWindowFocus(InputMethodManager.java:1518)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3550)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(NativeStart.java)
I know there is one similar question about this here. But there is a bit different. And as statistics from crashlytics, the crash happens mainly in SAMSUNG android phone.
I am new to android and don't know why the crash happened and how to fix this kind of crash.
Any suggestion would be much appreciated.
Think it's a very wide area and there could be a lot of situations which can trigger this system level exception. But maybe this example of how it was fixed in a particular project can help someone.
I experienced a similar exception:
"Could not read input channel file descriptors from parcel" on Samsung phone:
java.lang.RuntimeException: Could not read input channel file descriptors from parcel.
at android.view.InputChannel.nativeReadFromParcel(Native Method)
at android.view.InputChannel.readFromParcel(InputChannel.java:148)
at android.view.IWindowSession$Stub$Proxy.addToDisplay(IWindowSession.java:690)
at android.view.ViewRootImpl.setView(ViewRootImpl.java:525)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.widget.Toast$TN.handleShow(Toast.java:402)
at android.widget.Toast$TN$1.run(Toast.java:310)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
It happened in a big old project which I got for maintenance and this floating bug occurred only after several hours. I spent quite some time on it and also read some related answers on SO regarding it and had no clue except it's a system level bug of Android, and there should be some extra data or duplicates of objects or big objects in memory etc:
https://code.google.com/p/android/issues/detail?id=32470
The last thing I could think about was SoundPool. It's not used a lot in the project - there are not more than 10 different sounds played from time to time. But it was the root cause! Sometimes there were floating exceptions from SoundPool "unable to load sample (null)". And it helped to realize that SoundPool was used in a wrong way:
public void play(int rscId) {
...
final int soundId = soundPool.load(mContext, rscId, 1);
...
soundPool.play(soundId, volume, volume, 5, 0, 1f);
So new id was generated and sound resource was reloaded each time application called play sound method! And after certain amount of time some non related exceptions started to occur until application crashed with the "Could not read input channel file descriptors from parcel" exception.
It's interesting that one of those non related exceptions was: "ExceptionHandled in unable to open database file (code 14)":
ExceptionHandled in unable to open database file (code 14)
android.database.sqlite.SQLiteCantOpenDatabaseException:
unable to open database file (code 14)
at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow
(Native Method)
at android.database.sqlite.SQLiteConnection.executeForCursorWindow
(SQLiteConnection.java:845)
And of course it has nothing to do neither with database nor with toasts/parcels. The fix for that particular situation was very easy: just preload all sounds as it's suggested in Android documentation:
http://developer.android.com/reference/android/media/SoundPool.html
"The loading logic iterates through the list of sounds calling the appropriate SoundPool.load() function. This should typically be done early in the process to allow time for decompressing the audio to raw PCM format before they are needed for playback.
Once the sounds are loaded and play has started, the application can trigger sounds by calling SoundPool.play()."
So I moved soundPool.load() out from play() method and the exception :
"Could not read input channel file descriptors from parcel" has gone as well as the exception "unable to open database file (code 14)".
public void play(int soundId) {
...
soundPool.play(soundId, volume, volume, 5, 0, 1f);
And soundPool.release(); soundPool = null should be called as well when it's not needed anymore. And maybe it also can have an effect on such exceptions, see details here
Could not read input channel file descriptors from parcel
Most probably it's not the exact situation for the original question but hope it can give some information to dig further. I.e. looking for some additional exceptions, swallowed exceptions, or wrong files/data handling.
I am looking for the answer for a long time such as the others facing at this exception.
As I see it, this crash can also be triggered by the unexceptional TextView or EditText which works with the InputMethodManager:
java.lang.RuntimeException: Could not read input channel file descriptors from parcel.
at android.view.InputChannel.nativeReadFromParcel(Native Method)
at android.view.InputChannel.readFromParcel(InputChannel.java:148)
at android.view.InputChannel$1.createFromParcel(InputChannel.java:39)
at android.view.InputChannel$1.createFromParcel(InputChannel.java:36)
at com.android.internal.view.InputBindResult.<init>(InputBindResult.java:68)
at com.android.internal.view.InputBindResult$1.createFromParcel(InputBindResult.java:112)
at com.android.internal.view.InputBindResult$1.createFromParcel(InputBindResult.java:109)
at com.android.internal.view.IInputMethodManager$Stub$Proxy.startInput(IInputMethodManager.java:697)
at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1407)
at android.view.inputmethod.InputMethodManager.checkFocus(InputMethodManager.java:1518)
at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:1286)
at android.widget.TextView.setText(TextView.java:4718)
at android.widget.TextView.setText(TextView.java:4656)
at android.widget.TextView.append(TextView.java:4330)
at android.widget.TextView.append(TextView.java:4320)
...
When TextView appending text, it will make text to Editable, and start InputMethodManager. But at this time, something goes wrong in the InputMethod process, and it leads to fail to create InputBindResult which reading error from the parcel.
In this case, the workaround is very simple: DO NOT call append on textview directly, use StringBuilder or SpannableStringBuilder to build text and call TextView.setText(text) instead!
Previous users have commented that this can be a hard bug to track down. I caused this by building a custom alert dialog (containing multiple TextView objects) inside of a while loop vice an if statement. The application crashed before displaying the dialog box(es).
I have meet this problem recently.
java.lang.RuntimeException: Could not read input channel file descr
iptors from parcel.
I search the log find following info:
01-01 09:07:52.164 5162 6777 W zygote64: ashmem_create_region failed
for 'indirect ref table': Too many open files
The problem is that I create HandlerThread repeatedly, but don't do it's quit() method, at last result in fd leaked.
ViewRootImpl$ViewRootHandler means you have view.post or view.postDelay.
and in the post method maybe requestFocus or Keyboard

Categories