MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Getting Out Of Memory

Open nhjariwala opened this issue 11 years ago • 16 comments

I have fragment while changing orientation of fragment I got OOM.

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (mLogEnabled)
        Log.i(LOG_TAG, "OnSizeChanged()");

    if (w > 0 && h > 0 && w < 10000 && h < 10000) {
        // create a new bitmap with the new dimensions
        mDrawBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444); // Error Here
        // mDrawCanvas = new Canvas(mDrawBitmap);
        mViewPortHandler.setChartDimens(w, h);

        if (mLogEnabled)
            Log.i(LOG_TAG, "Setting chart dimens, width: " + w + ", height: " + h);
    }

    notifyDataSetChanged();

    super.onSizeChanged(w, h, oldw, oldh);
}

I have already retain the fragment but still it caused memory leaked. Thanks

nhjariwala avatar Apr 06 '15 12:04 nhjariwala

Have you try to recycle mDrawBitmap first?

MGaetan89 avatar Apr 06 '15 13:04 MGaetan89

Well I have added a call to recycle(). Tough I have never ever gotten OOM exception myself, no matter how often I change orientation.

Maybe something else in your code is leaking and the bitmap in the chart just gives it the last kick?

PhilJay avatar Apr 06 '15 16:04 PhilJay

@PhilJay recycle() not working still it crashing.

nhjariwala avatar Apr 07 '15 04:04 nhjariwala

@PhilJay and @MGaetan89 on more research I found that I got this error on above android L version. After commenting the the chart app works fine. Please have look in log

java.lang.OutOfMemoryError: Failed to allocate a 5688900 byte allocation with 1492788 free bytes and 1457KB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.Bitmap.nativeCreate(Native Method) at android.graphics.Bitmap.createBitmap(Bitmap.java:817) at android.graphics.Bitmap.createBitmap(Bitmap.java:794) at android.graphics.Bitmap.createBitmap(Bitmap.java:761) at com.github.mikephil.charting.charts.Chart.onSizeChanged(Chart.java:1341) at android.view.View.sizeChange(View.java:15703) at android.view.View.setFrame(View.java:15676) at android.view.View.layout(View.java:15592) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.widget.ScrollView.onLayout(ScrollView.java:1502) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget.FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)

I have tried several solution like:

  1. Bitmap.Config.ARGB_8888
  2. Bitmap.Config.RGB_565
  3. android:largeHeap="true"

nhjariwala avatar Apr 07 '15 06:04 nhjariwala

I get exact same error , android 4.4.2, galaxy s5. What might be reason. There is only 2 piecharts in activity:

java.lang.OutOfMemoryError at android.graphics.Bitmap.nativeCreate(Native Method) 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 com.github.mikephil.charting.charts.Chart.onSizeChanged(Chart.java:1518) at android.view.View.sizeChange(View.java:15710) at android.view.View.setFrame(View.java:15683) at android.view.View.layout(View.java:15591) at android.view.ViewGroup.layout(ViewGroup.java:4868) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) at android.view.View.layout(View.java:15595) at android.view.ViewGroup.layout(ViewGroup.java:4868) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.widget.ScrollView.onLayout(ScrollView.java:2035) at com.emilsjolander.components.StickyScrollViewItems.StickyScrollView.onLayout(StickyScrollView.java:158) at android.view.View.layout(View.java:15595) at android.view.ViewGroup.layout(ViewGroup.java:4868) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531) at android.widget.LinearLayout.onLayout(LinearLayout.java:1440) at android.view.View.layout(View.java:15595) at android.view.ViewGroup.layout(ViewGroup.java:4868) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:15595) at android.view.ViewGroup.layout(ViewGroup.java:4868) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:15595) at android.view.ViewGroup.layout(ViewGroup.java:4868) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:15595) at android.view.ViewGroup.layout(ViewGroup.java:4868) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531) at android.widget.LinearLayout.onLayout(LinearLayout.java:1440) at android.view.View.layout(View.java:15595) at android.view.ViewGroup.layout(ViewGroup.java:4868) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) at android.widget.FrameLayout.onLayout(FrameLayout.java:388) at android.view.View.layout(View.java:15595) at android.view.ViewGroup.layout(ViewGroup.java:4868) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2325) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2038) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1244) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6490) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803) at android.view.Choreographer.doCallbacks(Choreographer.java:603) at android.view.Choreographer.doFrame(Choreographer.java:573) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5586) at java.lang.reflect.Method.invokeNative(Native Method) 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(Native Method)

jemshit avatar May 19 '15 12:05 jemshit

Are there any news regarding this issue ?? i am facing it also and it does torture me! Thanks!

ChrisMpitzios avatar Jun 18 '15 15:06 ChrisMpitzios

use System.gc(); System.gc(); in oncreate

jemshit avatar Jun 18 '15 15:06 jemshit

Having this issue as well, it's very random in nature, always occurs when leaving the fragment/activity with the PieChart (after they are destroyed).

ootpapps avatar Jun 19 '15 05:06 ootpapps

I see increasing number of byte arrays in native memory. A assume that bitmap (as createBitmap() use native not heap) with graph is not cleaned in onPause. So if I enter several times into fragment with graphs I receive OOM exception.

adammach avatar Jun 24 '15 10:06 adammach

I researched using System.gc(); System.gc(); there's no guarantee it will release memory...

Getting this error a lot from users now, have to fix something... It also occurs to me specifically when switching back and forth from 4-5 fragments displaying a PieChart with about 5 segments per chart.

Please take a look dev, or can someone else propose a fix? I will be looking today myself.

ootpapps avatar Jun 24 '15 15:06 ootpapps

Same error using ViewPager with ~10 fragments , upon loading the last few it crashes with: java.lang.OutOfMemoryError: Failed to allocate a 4227276 byte allocation with 2371664 free bytes and 2MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.Bitmap.nativeCreate(Native Method) at android.graphics.Bitmap.createBitmap(Bitmap.java:939) at android.graphics.Bitmap.createBitmap(Bitmap.java:912) at android.graphics.Bitmap.createBitmap(Bitmap.java:879)

My chart is just shorten copy-paste of the LineChart examples(i was testing how it looks).Anyone tried to track leak using leakcanary ?

UPDATE: In onPause() i did chart=null; and for now problem is gone , tho memory usage is quite big.

Dokuzov avatar Jul 30 '15 07:07 Dokuzov

@Dokuzov Is this still helping you, to put chart = null in onPause?

jaredsburrows avatar Jan 20 '16 07:01 jaredsburrows

I no longer work on the app that was using the chart , but i recall that in onDestroyView() i was checking for if the chart is null and set it to null if not . In onPause() i was empying the data lines , to enforce re-loading in onResume(). Chunk of code i was using(not sure how good but it wasn't crashing):

@Override
public void onResume() {
    //Draws the chart with latest data (in case of updates)
    drawChart(chart, lineData, secondLineData);
    super.onResume();
}

@Override
public void onPause() {
    // Enforcing to reload the data in onResume()
    lineData = null;
    secondLineData = null;

    // chart = null;
    super.onPause();
}

@Override
public void onDestroyView() {
    if (chart != null) chart = null;
    super.onDestroyView();
}

Dokuzov avatar Jan 20 '16 08:01 Dokuzov

@Dokuzov Thanks for sharing!

jaredsburrows avatar Jan 20 '16 08:01 jaredsburrows

What was the exact finalized solution of this issue?

naveedahmad99 avatar Jun 10 '16 04:06 naveedahmad99

09-14 18:43:19.441 1597-1610/? W/android.os.Debug: failed to get memory consumption info: -1 can anybody help me to resolve this issue please?

amit055 avatar Sep 14 '18 13:09 amit055