2017-09-24 47 views
4

我使用smart-location-lib来获取设备的当前位置一次。它需要在活动的生命周期中稍后使用。由com.google.android.gms.internal引起的内存泄漏*

一旦做了finish()从活动回到以前的活动我得到一个内存泄漏。

下面的代码:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ... 

    SmartLocation.with(this).location() 
    .oneFix() 
    .start(new OnLocationUpdatedListener() { 
     @Override 
     public void onLocationUpdated(Location location) { 
      mCurrentLocation = location; 
     } 
    }); 

    ... 
} 

... 

@Override 
protected void onStop() { 
    super.onStop(); 

    SmartLocation.with(this).location().stop(); 
} 

正如你可以看到我在做智能位置LIB的.stop()作为文档here中所描述的活动的的onStop()调用。

但是这并不能防止泄漏。以下是有关与LeakCanary收集泄漏信息:

In com.companyname.appname.development:1.0-development:1. 
* com.companyname.appname.activities.TaskCheckoutActivity has leaked: 
* GC ROOT com.google.android.gms.internal.zzbyw.zzfwc 
* references com.google.android.gms.internal.zzbyt.zzhyy 
* references io.nlopez.smartlocation.location.providers.LocationGooglePlayServicesProvider.context 
* leaks com.companyname.appname.activities.TaskCheckoutActivity instance 

* Retaining: 1,6 MB. 
* Reference Key: ba2a19b1-d5ca-4971-a5c7-e0fd99ea95a6 
* Device: samsung samsung SM-G930F heroltexx 
* Android Version: 7.0 API: 24 LeakCanary: 1.5.4 74837f0 
* Durations: watch=5010ms, gc=215ms, heap dump=1792ms, analysis=128865ms 

* Details: 
* Instance of com.google.android.gms.internal.zzbyw 
| static $classOverhead = byte[752]@858164225 (0x33268c01) 
| zzfwc = [email protected] (0x335fd7d8) 
| mDescriptor = [email protected] (0x32f0e6a0) 
| mObject = 501026202704 
| mOwner = [email protected] (0x33068840) 
| shadow$_klass_ = com.google.android.gms.internal.zzbyw 
| shadow$_monitor_ = 0 
* Instance of com.google.android.gms.internal.zzbyt 
| static $classOverhead = byte[792]@855184385 (0x32f91401) 
| zzhyy = io.nlopez.smar[email protected]856875576 (0x3312e238) 
| zzfda = [email protected] (0x33119d80) 
| zzfip = [email protected] (0x32f01118) 
| mStatus = [email protected] (0x3313b598) 
| zzaj = true 
| zzaop = [email protected] (0x330114a0) 
| zzfhl = [email protected] (0x3313b598) 
| zzfiz = [email protected] (0x32f01060) 
| zzfja = [email protected] (0x33068860) 
| zzfjb = [email protected] (0x332202e0) 
| zzfjc = [email protected] (0x33220370) 
| zzfjd = null 
| zzfje = [email protected] (0x330114b0) 
| zzfjf = null 
| zzfjg = false 
| zzfjh = false 
| zzfji = null 
| zzfjj = null 
| zzfjk = false 
| shadow$_klass_ = com.google.android.gms.internal.zzbyt 
| shadow$_monitor_ = -1953408656 
* Instance of io.nlopez.smartlocation.location.providers.LocationGooglePlayServicesProvider 
| static REQUEST_CHECK_SETTINGS = 20001 
| static GMS_ID = [email protected] (0x33117f10) 
| static $classOverhead = byte[748]@857097217 (0x33164401) 
| static REQUEST_START_LOCATION_FIX = 10001 
| alwaysShow = true 
| checkLocationSettings = false 
| client = [email protected] (0x3314b970) 
| context = [email protected]0 (0x33475800) 
| fulfilledCheckLocationSettings = false 
| googlePlayServicesListener = io.nlopez.smartlocation.lo[email protected]856791392 (0x33119960) 
| listener = [email protected]680 (0x3311b9c0) 
| locationRequest = [email protected] (0x3312e510) 
| locationStore = [email protected] (0x333b3250) 
| logger = [email protected] (0x32efb0c8) 
| serviceListener = null 
| settingsResultCallback = io.nlopez.smartl[email protected]856798672 (0x3311b5d0) 
| shouldStart = false 
| stopped = true 
| shadow$_klass_ = io.nlopez.smartlocation.location.providers.LocationGooglePlayServicesProvider 
| shadow$_monitor_ = -2147272239 
* Instance of com.companyname.appname.activities.TaskCheckoutActivity 
| static $classOverhead = byte[4720]@856289281 (0x3309f001) 
| BARCODE_SCANNER_REQUEST = [email protected] (0x330b6210) 
| BEGIN_SIGNATURE_REQUEST = [email protected] (0x330b6200) 
| mApiKey = [email protected] (0x33144240) 
| mCheckoutEmptyStepView = null 
| mCurrentLocation = [email protected] (0x333c90d0) 
| mDamagedAddressUnknownDescriptionCheckFailed = [email protected] (0x702c1510) 
| mDamagedAddressUnknownDescriptionEditText = null 
| mDamagedAddressUnknownStepView = null 
| mJobManager = [email protected] (0x32c45ea0) 
| mKibCardInputCheckFailed = [email protected] (0x702c1510) 
| mKibCardInputEditText = null 
| mKibCardInputStepView = null 
| mKibCardNextTimeframe = null 
| mKibNextAbbrevation = null 
| mKibNextDay = null 
| mMarginBetweenStatusAndIcons = [email protected] (0x33469800) 
| mNameCheckFailed = [email protected] (0x702c1510) 
| mNeighbourAddressStepView = null 
| mNeighbourCityFailed = [email protected] (0x702c1510) 
| mNeighbourCityInputEditText = null 
| mNeighbourStreetFailed = [email protected] (0x702c1510) 
| mNeighbourStreetInputEditText = null 
| mNeighbourStreetNumberFailed = [email protected] (0x702c1500) 
| mNeighbourStreetNumberInputEditText = null 
| mNeighbourZipcodeFailed = [email protected] (0x702c1510) 
| mNeighbourZipcodeInputEditText = null 
| mOpenBarcodeScannerButton = null 
| mParcelKey = [email protected] (0x33149100) 
| mPaymentCompleted = [email protected] (0x702c1510) 
| mPaymentStepView = null 
| mPaymentSwitch = null 
| mPaymentTextView = null 
| mRecipientContainer = [email protected] (0x33481400) 
| mRecipientNameEditText = [email protected] (0x33482800) 
| mRefusedReasonCheckFailed = [email protected] (0x702c1510) 
| mRefusedReasonEditText = null 
| mRefusedStepView = null 
| mSignatureBitmap = [email protected] (0x32e8a160) 
| mSignatureButton = [email protected] (0x33483400) 
| mSignatureFragmentContainer = [email protected] (0x33483000) 
| mSignaturePreviewImageView = [email protected] (0x33483800) 
| mSignatureStepView = [email protected] (0x33481000) 
| mSigned = [email protected] (0x702c1500) 
| mSpecialContainerTaskStatusTextView = [email protected] (0x33469000) 
| mSpecialIconA = [email protected] (0x33471400) 
| mSpecialIconB = [email protected] (0x33471800) 
| mSpecialIconC = [email protected] (0x33471c00) 
| mSpecialIconD = [email protected] (0x33472000) 
| mSpecialIconsLinearLayout = [email protected] (0x33468c00) 
| mSteps = java.lang.String[2]@860283592 (0x3346e2c8) 
| mTask = [email protected] (0x3320f400) 
| mTaskCheckout = [email protected] (0x33395c48) 
| mTaskCheckoutMainLinLayout = [email protected] (0x330fb400) 
| mTaskStatus = [email protected] (0x32c01e00) 
| mTaskStatusLabel = [email protected] (0x33480800) 
| mTaskStatusSelectedIndex = [email protected] (0x70325030) 
| mTaskStatusText = [email protected] (0x3324a2e0) 
| mTaskStatusTextView = [email protected] (0x33480c00) 
| mTaskStatusView = [email protected] (0x33477800) 
| mVerticalStepperForm = er[email protected]860300288 (0x33472400) 
| mDelegate = [email protected] (0x330a73a0) 
| mResources = null 
| mThemeId = 2131427510 
| mCreated = true 
| mFragments = [email protected] (0x330b61f0) 
| mHandler = [email protected] (0x330b90a0) 
| mNextCandidateRequestIndex = 0 
| mPendingFragmentActivityResults = [email protected] (0x330ba310) 
| mReallyStopped = true 
| mRequestedPermissionsFromFragment = false 
| mResumed = false 
| mRetaining = false 
| mStopped = true 
| mStartedActivityFromFragment = false 
| mStartedIntentSenderFromFragment = false 
| mExtraDataMap = [email protected] (0x330a3d18) 
| mActionBar = null 
| mActionModeTypeStarting = 0 
| mActivityInfo = [email protected] (0x32ff8f70) 
| mActivityTransitionState = [email protected] (0x3308fd60) 
| mAppLockCheckRunnable = [email protected] (0x330b61c0) 
| mAppLockIsInMultiWindowMode = false 
| mApplication = [email protected] (0x32c6fa10) 
| mCalled = true 
| mChangeCanvasToTranslucent = false 
| mChangingConfigurations = false 
| mComponent = android.content.ComponentNa[email protected] (0x330a5050) 
| mConfigChangeFlags = 0 
| mCurrentConfig = [email protected] (0x33002f88) 
| mDecor = null 
| mDefaultKeyMode = 0 
| mDefaultKeySsb = null 
| mDestroyed = true 
| mDoReportFullyDrawn = false 
| mEatKeyUpEvent = false 
| mEmbeddedID = null 
| mEnableDefaultActionBarUp = false 
| mEnterTransitionListener = [email protected] (0x70bb4870) 
| mExitTransitionListener = [email protected] (0x70bb4870) 
| mFinished = true 
| mFlipfont = 0 
| mFragments = [email protected] (0x330b61a0) 
| mHandler = [email protected] (0x330b9060) 
| mHasCurrentPermissionsRequest = false 
| mIdent = 226856808 
| mInstanceTracker = [email protected] (0x330b61b0) 
| mInstrumentation = [email protected] (0x32c40fb8) 
| mIntent = [email protected] (0x332cdfc0) 
| mLastNonConfigurationInstances = null 
| mMainThread = [email protected] (0x32c04160) 
| mManagedCursors = [email protected] (0x330a3ce8) 
| mManagedDialogs = null 
| mMenuInflater = null 
| mParent = null 
| mPolicyManager = null 
| mReferrer = [email protected] (0x330af520) 
| mResultCode = -1 
| mResultData = null 
| mResumed = false 
| mScreenChangeListener = null 
| mSearchEvent = null 
| mSearchManager = null 
| mStartedActivity = false 
| mStopped = true 
| mTaskDescription = [email protected] (0x330b9080) 
| mTemporaryPause = false 
| mTitle = [email protected] (0x32c6fc40) 
| mTitleColor = 0 
| mTitleReady = true 
| mToken = [email protected] (0x330a6080) 
| mTranslucentCallback = null 
| mUiThread = [email protected] (0x772a1530) 
| mVisibleBehind = false 
| mVisibleFromClient = true 
| mVisibleFromServer = true 
| mVoiceInteractor = null 
| mWindow = [email protected] (0x334b9e90) 
| mWindowAdded = true 
| mWindowManager = [email protected] (0x330ba118) 
| mInflater = [email protected] (0x33284ee0) 
| mOverrideConfiguration = null 
| mResources = [email protected] (0x332b29c0) 
| mTheme = [email protected] (0x330b62d0) 
| mThemeResource = 2131427510 
| mBase = [email protected] (0x32f15f90) 
| shadow$_klass_ = com.companyname.appname.activities.TaskCheckoutActivity 
| shadow$_monitor_ = 1073743558 
* Excluded Refs: 
| Field: android.view.inputmethod.InputMethodManager.mNextServedView 
| Field: android.view.inputmethod.InputMethodManager.mServedView 
| Field: android.view.inputmethod.InputMethodManager.mServedInputConnection 
| Field: android.view.textservice.SpellCheckerSession$1.this$0 
| Field: com.samsung.android.content.clipboard.SemClipboardManager.mContext 
| Field: com.samsung.android.emergencymode.SemEmergencyManager.mContext 
| 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) 

我不使用智能定位lib和执行代码编写的官方培训文档中从Android的here获得当前位置尝试。但这也会导致内存泄漏com.google.android.gms.internal.*

我试过这两个设备(Android 7.0上的Galaxy S7 & Android 5.1.1上的Galaxy J2 2016),它们都产生内存泄漏。该项目使用compile 'com.google.android.gms:play-services-location:11.2.2'

我在做什么错了?

+0

让我知道,如果我的回答工作。 –

+0

你解决了吗,我也得到了完全相同的泄漏。 –

回答

1

我在android.widget对象像imageview,按钮等同样的问题,同时做动画,所以我创建了静态内部类像下面而不是匿名或非静态内部类和静态我存储android.widget对象在一个弱的参考。

private static class ImageViewAnimation implements ValueAnimator.AnimatorUpdateListener { 

    private final WeakReference<ImageView> mpetImageView; 
    private int axis; 

    public ImageViewAnimation(ImageView mpetImageView, int axis) { 
     this.mpetImageView = new WeakReference<>(mpetImageView); 
     this.axis = axis; 
    } 

    @Override 
    public void onAnimationUpdate(ValueAnimator valueAnimator) { 
     ImageView view = mpetImageView.get(); 
     int value = (int) valueAnimator.getAnimatedValue(); 
     if (this.axis == 1) { 
      view.setTranslationX(value); 
     } else if (this.axis == 2) { 
      view.setTranslationY(value); 
     } 
    } 
} 

我用上面的类在我的活动作为 petx.addUpdateListener(new ImageViewAnimation(petImageView, 1));

从这个例子中带走您的使用情况是以下

  • 尝试,并创建一个静态内部类和使它实现OnLocationUpdatedListener并且使用这个类实例在你想要的地方。
  • mcurretLocation您使用的是确保android.Location实例非静态
  • 尝试使android.Location实例空明确onPause()onDestroy()
+0

这并没有工作 – narzero

+0

@narzero你使用堆分析器来跟踪泄漏或使用泄漏金丝雀 –

+0

我使用两个... – narzero