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"
>
Related
My application is facing random crashes for real users. Unfortunately I can not reproduce it. Has any one has more insight on the issue, or have a solution for the crash? The stack trace is as follows.
java.lang.IllegalArgumentException: path must be convex
at android.graphics.Outline.setConvexPath(Outline.java:284)
at android.graphics.drawable.GradientDrawable.getOutline(GradientDrawable.java:1745)
at android.view.ViewOutlineProvider$1.getOutline(ViewOutlineProvider.java:38)
at android.view.View.rebuildOutline(View.java:17362)
at android.view.View.invalidateDrawable(View.java:22675)
at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:457)
at android.graphics.drawable.Drawable.setVisible(Drawable.java:866)
at android.view.View.onVisibilityAggregated(View.java:13928)
at android.view.View.dispatchDetachedFromWindow(View.java:20062)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3995)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3987)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:3987)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:5588)
at android.view.ViewGroup.removeViewAt(ViewGroup.java:5535)
at android.support.v7.widget.RecyclerView$5.removeViewAt(RecyclerView.java:877)
at android.support.v7.widget.ChildHelper.removeViewAt(ChildHelper.java:168)
at android.support.v7.widget.RecyclerView$LayoutManager.removeViewAt(RecyclerView.java:8374)
at android.support.v7.widget.RecyclerView$LayoutManager.removeAndRecycleViewAt(RecyclerView.java:8647)
at android.support.v7.widget.LinearLayoutManager.recycleChildren(LinearLayoutManager.java:1369)
at android.support.v7.widget.LinearLayoutManager.recycleViewsFromEnd(LinearLayoutManager.java:1449)
at android.support.v7.widget.LinearLayoutManager.recycleByLayoutState(LinearLayoutManager.java:1482)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1508)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1331)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1075)
at android.support.v7.widget.RecyclerView.scrollStep(RecyclerView.java:1832)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5067)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:693)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
I found this stackoverflow post but it is not useful in my case. The issue appears to be happening while drawing RecyclerView. I use custom background for RecyclerView items which I suspect might be the reason.
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.
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.
I've taken a code from a tutorial about a spinning wheel.
I've adjusted the code to work from my application but it's crashing for some reason and I can't figure out why.
I'll really appreciate any kind of help.
The logcat information is :
com.truthordareyakir.android.truthordare E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.truthordareyakir.android.truthordare, PID: 2404
java.lang.OutOfMemoryError: Failed to allocate a 37748748 byte allocation with 4194304 free bytes and 27MB 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.BitmapFactory.decodeResource(BitmapFactory.java:467)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:497)
at com.truthordareyakir.android.truthordare.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Sounds to me like your animated image is too large. You have have separate sized images for each drawable type? hdpi, mdpi, xhdpi, etc...
Use the Android Asset Studio to scale images if needed.
Have you tried to initialise the byte with a lower value ?
Sometimes the Memory of your phone can't process such high va. lues.
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.