MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

MoveViewJob.pool is memory leak

Open ChuuMong opened this issue 9 years ago • 14 comments

MoveVIewJab.java

in a

private static ObjectPool<MoveViewJob> pool;

occurs memory leck.

ChuuMong avatar Sep 07 '16 03:09 ChuuMong

Leak Canary detected the same problem in my app. Here is an export from the leak canary report:

In de.example.base.prod:TODO:1.

  • de.example.main.gui.processdetails.ProcessDetailsActivity has leaked:
  • GC ROOT static com.github.mikephil.charting.jobs.MoveViewJob.pool
  • references com.github.mikephil.charting.utils.ObjectPool.objects
  • references array java.lang.Object[].[37]
  • references com.github.mikephil.charting.jobs.MoveViewJob.view
  • references com.github.mikephil.charting.charts.LineChart.mContext
  • leaks de.example.main.gui.processdetails.ProcessDetailsActivity instance

I hope this helps to fix the issue.

BaN4NaJ0e avatar Oct 14 '16 15:10 BaN4NaJ0e

Leak Canary detects the same memory leak in my app. It occurs since I updated the library from version v2.2.5 to v3.0.1:

* de.myapp.device.DeviceActivity has leaked:
* GC ROOT static com.github.mikephil.charting.jobs.MoveViewJob.pool
* references com.github.mikephil.charting.utils.ObjectPool.objects
* references array java.lang.Object[].[2]
* references com.github.mikephil.charting.jobs.MoveViewJob.view
* references com.github.mikephil.charting.charts.LineChart.mContext
* leaks de.myapp.device.DeviceActivity instance

Alkisum avatar Dec 02 '16 15:12 Alkisum

I got this leak when I called LineChart.centerViewTo(). It spawns a MoveViewJob that is added to the static pool member mentioned in the original post. This job contains a reference to the (chart)view and because of how my event handlers were tied up caused my activity to leak.

Hacky fix for my scenario is to call MoveViewJob.getInstance(null, 0, 0, null, null); on the fragment's onPause(). (The fragment that contains the chart view.)

This call nullifies the view reference on the pool object, and makes the view garbage collectable.

Diederikjh avatar Dec 05 '16 13:12 Diederikjh

Same here. MoveViewJob.getInstance(null, 0f, 0f, null, null) called in activity onDestroy fixed the leak. Any official solution for this?

recoverrelax avatar Dec 15 '16 11:12 recoverrelax

any news on this? at least an official workaround until it is fixed. Thanks in advanced

recoverrelax avatar Jan 26 '17 12:01 recoverrelax

i write MoveViewJob.getInstance(null, 0f, 0f, null, null) in activity onDestroy,and it also leak , i don't know how to fix it?

FengQing123 avatar May 04 '18 03:05 FengQing123

I can confirm that the animation pools were not properly created. I'm working through a massive refactor/ documentation update now and I believe I've fixed the issue already, but the commit is still a bit into the future. I noticed a few cases already where these objects were NEVER recycled, despite that being the intention of the ObjectPool class, to allow these animations to be easily reusable.

Specifically, I believe this line is the cause of the problem, Android Studio even reports a warning on it:

Line 49

this.recycleInstance(this);

The problem is that it uses this.recycleInstance(this) instead of what ZoomJob uses, a plain static call to recycleInstance(this)

I can also confirm that these objects aren't super expensive, so perhaps gutting the ObjectPool and just creating these objects locally could solve the problem, as they are guaranteed to be garbage collected if only being created in local scopes and not permanently stored with activities or other objects. But I'll leave that option up for debate.

almic avatar May 05 '18 16:05 almic

any body find the solution? I have same problem, I put barchart in recyclerview and always move to item zero, it's work but cause memory leak. I treat using

                float[] pts = {-1f, 0f};
                Transformer transformer = chart.getTransformer(YAxis.AxisDependency.LEFT);
                ViewPortHandler viewPortHandler = chart.getViewPortHandler();
                transformer.pointValuesToPixel(pts);
                viewPortHandler.centerViewPort(pts, chart);

when I scroll barchart it's delay 3, 4 seconds to scroll to zero.

haiithust avatar Jun 29 '18 10:06 haiithust

call MoveViewJob.getInstance(null, 0f, 0f, null, null) onDestory doesn't work in my Activity,anyone solve this problem?

po1arbear avatar Aug 01 '18 06:08 po1arbear

If you use RecyclerView you can write


     @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        MoveViewJob.getInstance(null, 0, 0, null, null);
    }

goraga1 avatar Oct 20 '18 22:10 goraga1

Call on the onDestroy() for activity Call on the onDestroyView() for fragment

private fun fixChartMemoryLeaks() {
        // Fix https://github.com/PhilJay/MPAndroidChart/issues/2238
        val moveViewJobPoll = MoveViewJob::class.java.getDeclaredField("pool")
        moveViewJobPoll.isAccessible = true
        moveViewJobPoll.set(null, ObjectPool.create(2, MoveViewJob(null, 0f, 0f, null, null)))

        // the same issue with ZoomJob
        val zoomViewJobPoll = ZoomJob::class.java.getDeclaredField("pool")
        zoomViewJobPoll.isAccessible = true
        zoomViewJobPoll.set(null, ObjectPool.create(2, ZoomJob(null, 0f, 0f, 0f, 0f, null, null, null)))
}

ZaydelEduard avatar Feb 18 '19 16:02 ZaydelEduard

在 MoveViewJob 类中加以下代码

public static void recycleInstance(MoveViewJob instance) { instance.mViewPortHandler = null; instance.mTrans = null; instance.view = null; pool.recycle(instance); }

pengxinaglin avatar Jan 08 '20 09:01 pengxinaglin

这样只能修改源码了

HolenZhou avatar Aug 01 '22 02:08 HolenZhou