Without implementing any SPen related SDK libraries, a memory leak occurs on an AppCompatActivity
extended class.
The following sequence of events typically triggers the memory leak:
Would there be any way to clear the reference to the mContext variable being held by the SpenGestureManager class? This should hopefully allow the Garbage Collector to clear the destroyed activity.
This issue was detected on the following device/Android version (Samsung Galaxy Note 3 Duos):
The same application was tested on a Samsung Galaxy Note 10.1 device, which does not appear to have this memory leak issue.
As you can see from LeakCanary's sources, that's a well-known memory leak.
SpenGestureManager
has a staticmContext
field that leaks a reference to the activity. Yes, a STATIC mContext field.
I think that can be resolved using reflection. This is a piece of code that should work:
// Perform an if-check to see whether this is a Samsung device
Class clazz = Class.forName("com.samsung.android.smartclip.SpenGestureManager");
Field mContext = clazz.getDeclaredField("mContext");
mContext.setAccessible(true);
mContext.set(null, null);