1 today when debugging Android program, found that even if the program exited, found that the memory also occupies about 15M. With the mat, after several GC operations, it is still 15. The intuition tells me that memory leaks should occur. Then use mat to view the memory Leak. The results surprised me and found that it was inputmethodmanager. This object has been referencing the context. That is, activity, which causes it to not release memory. Google found out later,
Here are some solutions that you would like to help someone in a similar situation:
@Overrideprotected void OnDestroy () { Super.ondestroy (); Fix for Memory leak:http://code.google.com/p/android/issues/detail?id=34731 Fixinputmethodmanager ();} private void Fixinputmethodmanager () { final Object imm = Getsystemservice (context.input_method_service); Final Reflector.typedobject Windowtoken = new Reflector.typedobject (GetWindow (). Getdecorview (). Getwindowtoken ( ), ibinder.class); Reflector.invokemethodexceptionsafe (IMM, "windowdismissed", Windowtoken); Final Reflector.typedobject View = new Reflector.typedobject (null, view.class); Reflector.invokemethodexceptionsafe (IMM, "Startgettingwindowfocus", view);}
is to call windowdismissed and Startgettingwindowfocus through reflection. Would have thought that this problem is done, who knows there is a more wonderful situation. I find that there is a more wonderful object: COM.LFLYTEK.SPEECH.A.A.A. The brain is a bit confused, what is this? Through Baidu This query, found that this is the Iflytek voice recognition SDK. Yes, the brain is really ignorant. But it's not right. This object is so long in life cycle. No, there must be something wrong. Later read through the source code:
Public Recognizerdialog (Context paramcontext, String paramstring) { super (paramcontext); THIS.A = new A (Paramcontext, paramstring); } public void Setlistener (Recognizerdialoglistener paramrecognizerdialoglistener) { ((a) THIS.A). A ( Paramrecognizerdialoglistener); }
This is the only place where the SDK can get my application, and the rest is set up with some information. Is this where the problem is? Yes, you got it right!
On the surface it seems that there is no problem, you see many of our application SDK is not all that way, it is normal AH. No way, although the code has been confused, but I have to find out the truth can only bite the bullet to read. Through the constant tracking of code, finally found the secret.
There is a very wonderful private class member in the COM.IFLYTEK.SPEECH.A.A.
private static A E=null
public static com.iflytek.speech.b B (Context paramcontext, String paramstring) { if (E = = null) e = new A (p Aramcontext, paramstring); return e; }
Aren't you deceptive? Why do you have to use static, this causes the life cycle of e and activity, it also caused a memory leak. (later, when looking at the confused Iflytek SDK code, found that it was in the acquisition device location information and application package name and related signature information, of course, it is understandable that, after all, they want to verify the application's package name and signature). This can be, this static is also private. By the thought of reflection.
The following code is posted on the reflection.
@Overridepublic void Finish () {com.iflytek.a.a.a=null;try {Class clazz=class.forname ("com.iflytek.speech.a.a"); Field Field=clazz.getdeclaredfield ("E"); if (field.isaccessible () ==false) {field.setaccessible (true);} Field.set (null, NULL);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Super.finish ();}
And then debugging, found that the problem finally solved.
It seems that later with the third-party SDK, you have to pay attention to the point, you can not use AH.
Android Memory leak subtotal