curioustechizen / android-ago

An Android TextView that always displays an auto refreshing relative time span with respect to a reference time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RelativeTimeTextView and memory leaks

Winghin2517 opened this issue · comments

I think this library is causing some memory leaks - can you please fix?

I tested this with LeakCanary: https://github.com/square/leakcanary

And it keeps on detecting a memory leak here:

01-27 21:07:53.075 4217-4353/? D/LeakCanary: In com.example.simon.recyclerviewlistwithmongo:1.0:1.
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * com.example.simon.activities.PostDetailActivity has leaked:
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * GC ROOT android.view.ViewRootImpl$WindowInputEventReceiver.mMessageQueue
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * references android.os.MessageQueue.mMessages
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * references android.os.Message.callback
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * references com.github.curioustechizen.ago.RelativeTimeTextView$UpdateTimeRunnable.this$0
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * references com.github.curioustechizen.ago.RelativeTimeTextView.mContext
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * references android.support.v7.view.ContextThemeWrapper.mBase
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * leaks com.example.simon.activities.PostDetailActivity instance
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * Reference Key: f949ab86-5469-40cf-9f6b-bae54f1eedfa
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * Device: Genymotion generic Samsung Galaxy S2 - 4.1.1 - API 16 - 480x800 vbox86p
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * Android Version: 4.1.1 API: 16 LeakCanary: 1.3.1
01-27 21:07:53.075 4217-4353/? D/LeakCanary: * Durations: watch=5009ms, gc=116ms, heap dump=259ms, analysis=9831ms

Suggest fix: Please replace the context that is passed in RelativeTimeTextView.mContext with an ApplicationContext.

Thanks!

Thanks for this report. I'll take a look.

Hi,

Did you managed to fix this?

Hi @Winghin2517 I'm sorry I haven't had a chance to look at it yet. The problem seems to be that the UpdateTimeRunnable holds a reference to the View and hence to the Activity. I'm not sure if using an Application Context is the right way to fix it. Instead, I should be attaching and detaching the runnable in the appropriate callback methods.

Actually, I'm not sure about the point of this library - you actually don't need this library to tell relative time. Java already does it natively by using the method:http://stackoverflow.com/questions/25174921/time-ago-for-android-java

DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution)

Best to use this method as this library is leaking.

@Winghin2517 This library internally uses the DateUtils class that you mentioned. The README explains this clearly. But the reason to use this library is to automatically call the DateUtils method when the time changes.

For example,

  • suppose it is 10:05 am now. You set the relative time to 10:00 am. So, it shows "5 minutes ago".
  • After a minute, it becomes 10:06 am. But if you use regular DateUtils, you will still be showing "5 minutes ago", which is wrong. It should slow "6 minutes ago".

This library uses a Handler to automatically update the TextView whenever required so that the relative time displayed is always correct.

As to the memory leak, I haven't got time to fix it but contributions are appreciated!

Fixed in v1.3.2. Uploaded to maven central. Will be available .. .eventually ... hopefully.