我用的是Android Studio2.3,app的目标和编译SDK版本25,最小SDK版本14。有一个BaseApplication继承Application,相应的还有BaseActivity, BaseFragment.
给app添加分享到微信的功能,申请了appid,看文档上面说的要在程序入口出Activity的onCreate回调注册,但是我是需要在一个fragment中调用,如果提前注册,岂不是无法获取已注册的IWXAPI对象? 所以我把注册也放在了fragment里面了,然后就出现了内存泄露?
https://open.weixin.qq.com/cg...
泄露是这样说的:
In cn.tellyouwhat.checkinsystem:0.4.0Alpha:20170409.
cn.tellyouwhat.checkinsystem.activities.MainActivity has leaked:
GC ROOT static com.tencent.mm.opensdk.openapi.WXApiImplV10.activityCb
references com.tencent.mm.opensdk.openapi.WXApiImplV10$ActivityLifecycleCb.context
leaks cn.tellyouwhat.checkinsystem.activities.MainActivity instance
Retaining: 6.6 MB.
Reference Key: a0b25eec-cdb2-44be-8131-730c8cf50517
Device: Xiaomi Xiaomi Mi-4c mk_libra
Android Version: 6.0.1 API: 23 LeakCanary: 1.5 00f37f5
Durations: watch=5033ms, gc=177ms, heap dump=2449ms, analysis=318591ms
Details:
Class com.tencent.mm.opensdk.openapi.WXApiImplV10
| static $staticOverhead = byte[24]@853491713 (0x32df4001)
| static wxappPayEntryClassname = null
| static activityCb = com.tencent.mm.opensdk.openapi.WXApiImplV10$ActivityLifecycleCb@854930048 (0x32f53280)
| static TAG = java.lang.String@851479856 (0x32c08d30)
Instance of com.tencent.mm.opensdk.openapi.WXApiImplV10$ActivityLifecycleCb
| static $staticOverhead = byte[16]@853143553 (0x32d9f001)
| static DELAYED = 800
| static TAG = java.lang.String@851487200 (0x32c0a9e0)
| context = cn.tellyouwhat.checkinsystem.activities.MainActivity@852043216 (0x32c925d0)
| handler = android.os.Handler@854930080 (0x32f532a0)
| isForeground = true
| onPausedRunnable = com.tencent.mm.opensdk.openapi.WXApiImplV10$ActivityLifecycleCb$1@854921600 (0x32f51180)
| onResumedRunnable = com.tencent.mm.opensdk.openapi.WXApiImplV10$ActivityLifecycleCb$2@854921616 (0x32f51190)
| shadow$_klass_ = com.tencent.mm.opensdk.openapi.WXApiImplV10$ActivityLifecycleCb
| shadow$_monitor_ = 0
Instance of cn.tellyouwhat.checkinsystem.activities.MainActivity
| static $staticOverhead = byte[16]@852410369 (0x32cec001)
| static serialVersionUID = 3661784752173624772
| static $change = null
| TAG = java.lang.String@851833328 (0x32c5f1f0)
| mOnNavigationItemSelectedListener = cn.tellyouwhat.checkinsystem.activities.MainActivity$1@854922080 (0x32f51360)
| mAlertDialog = null
| mHelper = com.github.anzewei.parallaxbacklayout.ParallaxBackActivityHelper@854922096 (0x32f51370)
| mDelegate = android.support.v7.app.AppCompatDelegateImplV23@853970128 (0x32e68cd0)
| mEatKeyUpEvent = false
| mResources = null
| mThemeId = 2131427511
| mCreated = true
| mFragments = android.support.v4.app.FragmentController@854922112 (0x32f51380)
| mHandler = android.support.v4.app.FragmentActivity$1@854932384 (0x32f53ba0)
| mNextCandidateRequestIndex = 0
| mOptionsMenuInvalidated = false
| mPendingFragmentActivityResults = android.support.v4.util.SparseArrayCompat@854932416 (0x32f53bc0)
| mReallyStopped = true
| mRequestedPermissionsFromFragment = false
| mResumed = false
| mRetaining = false
| mStopped = true
| mStartedActivityFromFragment = false
| mStartedIntentSenderFromFragment = false
| mExtraDataMap = android.support.v4.util.SimpleArrayMap@854932448 (0x32f53be0)
| mActionBar = null
| mActionModeTypeStarting = 0
| mActivityInfo = android.content.pm.ActivityInfo@854885376 (0x32f48400)
| mActivityTransitionState = android.app.ActivityTransitionState@854926720 (0x32f52580)
| mApplication = cn.tellyouwhat.checkinsystem.bases.BaseApplication@854718880 (0x32f1f9a0)
| mCalled = true
| mChangeCanvasToTranslucent = false
| mChangingConfigurations = false
| mComponent = android.content.ComponentName@854922128 (0x32f51390)
| mConfigChangeFlags = 0
| mCurrentConfig = android.content.res.Configuration@854060272 (0x32e7ecf0)
| mDecor = null
| mDefaultKeyMode = 0
| mDefaultKeySsb = null
| mDestroyed = true
| mDoReportFullyDrawn = false
| mEmbeddedID = null
| mEnableDefaultActionBarUp = false
| mEnterTransitionListener = android.app.SharedElementCallback$1@1891362664 (0x70bbe768)
| mExitTransitionListener = android.app.SharedElementCallback$1@1891362664 (0x70bbe768)
| mFinished = true
| mFragments = android.app.FragmentController@854922144 (0x32f513a0)
| mHandler = android.os.Handler@854932480 (0x32f53c00)
| mHasCurrentPermissionsRequest = false
| mIdent = 25156784
| mInstanceTracker = android.os.StrictMode$InstanceTracker@854922160 (0x32f513b0)
| mInstrumentation = android.app.Instrumentation@854702848 (0x32f1bb00)
| mIntent = android.content.Intent@854926784 (0x32f525c0)
| mLastNonConfigurationInstances = null
| mMainThread = android.app.ActivityThread@851456256 (0x32c03100)
| mManagedCursors = java.util.ArrayList@854932512 (0x32f53c20)
| mManagedDialogs = null
| mMenuInflater = null
| mParent = null
| mReferrer = java.lang.String@854939296 (0x32f556a0)
| mResultCode = 0
| mResultData = null
| mResumed = false
| mSearchEvent = null
| mSearchManager = null
| mStartedActivity = false
| mStopped = true
| mTemporaryPause = false
| mTitle = java.lang.String@854871200 (0x32f44ca0)
| mTitleColor = 0
| mTitleReady = true
| mToken = android.os.BinderProxy@854880608 (0x32f47160)
| mTranslucentCallback = null
| mUiThread = java.lang.Thread@1968490976 (0x7554c9e0)
| mVisibleBehind = false
| mVisibleFromClient = true
| mVisibleFromServer = true
| mVoiceInteractor = null
| mWindow = com.android.internal.policy.PhoneWindow@853848800 (0x32e4b2e0)
| mWindowAdded = true
| mWindowManager = android.view.WindowManagerImpl@854932544 (0x32f53c40)
| mInflater = com.android.internal.policy.PhoneLayoutInflater@854933904 (0x32f54190)
| mOverrideConfiguration = null
| mResources = android.content.res.XResources@854057696 (0x32e7e2e0)
| mTheme = android.content.res.Resources$Theme@854932576 (0x32f53c60)
| mThemeResource = 2131427511
| mBase = android.app.ContextImpl@854885504 (0x32f48480)
| shadow$_klass_ = cn.tellyouwhat.checkinsystem.activities.MainActivity
| shadow$_monitor_ = 1073760580
Excluded Refs:
| Field: android.view.inputmethod.InputMethodManager.mNextServedView
| Field: android.view.inputmethod.InputMethodManager.mServedView
| Field: android.view.inputmethod.InputMethodManager.mServedInputConnection
| Field: android.view.inputmethod.InputMethodManager.mCurRootView
| Field: android.os.UserManager.mContext
| Field: android.net.ConnectivityManager.sInstance
| Field: android.view.Choreographer$FrameDisplayEventReceiver.mMessageQueue (always)
| Thread:FinalizerWatchdogDaemon (always)
| Thread:main (always)
| Thread:LeakCanary-Heap-Dump (always)
| Class:java.lang.ref.WeakReference (always)
| Class:java.lang.ref.SoftReference (always)
| Class:java.lang.ref.PhantomReference (always)
| Class:java.lang.ref.Finalizer (always)
| Class:java.lang.ref.FinalizerReference (always)
用application作为context注册一个全局单例