Getting Out Of Memory
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
Have you try to recycle mDrawBitmap first?
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 recycle() not working still it crashing.
@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:
- Bitmap.Config.ARGB_8888
- Bitmap.Config.RGB_565
- android:largeHeap="true"
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)
Are there any news regarding this issue ?? i am facing it also and it does torture me! Thanks!
use System.gc(); System.gc(); in oncreate
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).
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.
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.
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 Is this still helping you, to put chart = null in onPause?
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 Thanks for sharing!
What was the exact finalized solution of this issue?
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?