当我的 Activity 中有零代码时微调器泄漏

Spinner is leaking when I have ZERO code inside my Activity

我的 MainActivity 中的代码为零,只有我创建的 XML 布局和 Spinner 正在泄漏,不确定为什么会发生这种情况或如何解决此问题。有谁知道为什么?我正在使用 LeakCanary 库

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

我的 XML 布局 activity_main:

在我的 mainactivity.java

中没有添加任何其他内容
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        app:contentInsetStart="0dp"
        app:layout_collapseMode="pin"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/toolbar_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:background="?attr/actionBarItemBackground"
                android:paddingStart="12dp"
                android:paddingEnd="3dp"
                app:srcCompat="@drawable/ic_white_printer" />

            <TextView
                android:id="@+id/toolbar_app_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginEnd="16dp"
                android:layout_toStartOf="@+id/toolbar_more"
                android:layout_toEndOf="@+id/toolbar_icon"
                android:ellipsize="end"
                android:maxLines="2"
                android:text="Test app"
                android:textColor="@color/colorWhite"
                android:textSize="24sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/toolbar_more"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:background="?attr/actionBarItemBackground"
                android:paddingStart="16dp"
                android:paddingEnd="16dp"
                app:srcCompat="@drawable/ic_white_more_vertical" />

        </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>

    <Spinner
        android:id="@+id/spinner_printer_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:dropDownVerticalOffset="35dp"
        android:gravity="end"
        android:layout_margin="16dp"
        app:backgroundTint="@color/standardBlue"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:layout_constraintStart_toStartOf="parent"/>

    <Spinner
        android:id="@+id/spinner_roll_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:dropDownVerticalOffset="35dp"
        android:gravity="end"
        android:layout_marginStart="16dp"
        app:layout_constraintHorizontal_bias="1"
        app:backgroundTint="@color/standardBlue"
        app:layout_constraintStart_toEndOf="@+id/spinner_printer_type"
        app:layout_constraintBottom_toBottomOf="@+id/spinner_printer_type"
        app:layout_constraintTop_toTopOf="@+id/spinner_printer_type"/>


    <Button
        android:id="@+id/btn_upload"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@color/colorAccent"
        android:text="upload"
        android:textColor="@color/colorWhite"
        app:layout_constraintBottom_toBottomOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

您可以自己尝试使用

debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.6'

日志:

┬───
│ GC Root: Global variable in native code
│
├─ android.view.RenderNodeAnimator instance
│    Leaking: NO (MaterialButton↓ is not leaking)
│    ↓ RenderNodeAnimator.mTarget
├─ android.view.RenderNode instance
│    Leaking: NO (MaterialButton↓ is not leaking)
│    ↓ RenderNode.mOwningView
├─ com.google.android.material.button.MaterialButton instance
│    Leaking: NO (ConstraintLayout↓ is not leaking and View attached)
│    View is part of a window view hierarchy
│    View.mAttachInfo is not null (view attached)
│    View.mID = R.id.btn_upload
│    View.mWindowAttachCount = 1
│    mContext instance of com.example.smartlabels.MainActivity with
│    mDestroyed = false
│    ↓ View.mParent
├─ androidx.constraintlayout.widget.ConstraintLayout instance
│    Leaking: NO (AppCompatSpinner↓ is not leaking and View attached)
│    View is part of a window view hierarchy
│    View.mAttachInfo is not null (view attached)
│    View.mWindowAttachCount = 1
│    mContext instance of com.example.smartlabels.MainActivity with
│    mDestroyed = false
│    ↓ ViewGroup.mChildren
├─ android.view.View[] array
│    Leaking: NO (AppCompatSpinner↓ is not leaking)
│    ↓ View[].[1]
├─ androidx.appcompat.widget.AppCompatSpinner instance
│    Leaking: NO (View attached)
│    View is part of a window view hierarchy
│    View.mAttachInfo is not null (view attached)
│    View.mID = R.id.spinner_printer_type
│    View.mWindowAttachCount = 1
│    mPopupContext instance of com.example.smartlabels.MainActivity
│    with mDestroyed = false
│    mContext instance of com.example.smartlabels.MainActivity with
│    mDestroyed = false
│    ↓ AppCompatSpinner.mPopup
│                       ~~~~~~
├─ androidx.appcompat.widget.AppCompatSpinner$DropdownPopup instance
│    Leaking: UNKNOWN
│    Retaining 14.8 kB in 245 objects
│    mContext instance of com.example.smartlabels.MainActivity with
│    mDestroyed = false
│    ↓ ListPopupWindow.mPopup
│                      ~~~~~~
├─ androidx.appcompat.widget.AppCompatPopupWindow instance
│    Leaking: UNKNOWN
│    Retaining 8.8 kB in 154 objects
│    mContext instance of com.example.smartlabels.MainActivity with
│    mDestroyed = false
│    ↓ PopupWindow.mEnterTransition
│                  ~~~~~~~~~~~~~~~~
├─ android.transition.TransitionSet instance
│    Leaking: UNKNOWN
│    Retaining 2.9 kB in 76 objects
│    ↓ Transition.mTargets
│                 ~~~~~~~~
├─ java.util.ArrayList instance
│    Leaking: UNKNOWN
│    Retaining 60 B in 2 objects
│    ↓ ArrayList.elementData
│                ~~~~~~~~~~~
├─ java.lang.Object[] array
│    Leaking: UNKNOWN
│    Retaining 40 B in 1 objects
│    ↓ Object[].[0]
│               ~~~
├─ android.widget.PopupWindow$PopupBackgroundView instance
│    Leaking: UNKNOWN
│    Retaining 4.2 kB in 35 objects
│    View not part of a window view hierarchy
│    View.mAttachInfo is null (view detached)
│    View.mWindowAttachCount = 1
│    mContext instance of com.example.smartlabels.MainActivity with
│    mDestroyed = false
│    ↓ View.mParent
│           ~~~~~~~
╰→ android.widget.PopupWindow$PopupDecorView instance
​     Leaking: YES (ObjectWatcher was watching this because android.widget.
​     PopupWindow$PopupDecorView received View#onDetachedFromWindow() callback)
​     Retaining 2.3 kB in 23 objects
​     key = 8914b38a-5678-4920-89b9-160635defdc7
​     watchDurationMillis = 552488
​     retainedDurationMillis = 547487
​     View not part of a window view hierarchy
​     View.mAttachInfo is null (view detached)
​     View.mWindowAttachCount = 1
​     mContext instance of com.example.smartlabels.MainActivity with
​     mDestroyed = false

METADATA

Build.VERSION.SDK_INT: 27
Build.MANUFACTURER: Google
LeakCanary version: 2.6
App process name: com.example.smartlabels
Stats: LruCache[maxSize=3000,hits=1393,misses=26800,hitRate=4%]
RandomAccess[bytes=1377952,reads=26800,travel=7146276921,range=9328169,size=1209
4081]
Heap dump reason: user request
Analysis duration: 3014 ms

这是 LeakCanary 2.6 中的已知问题,已在 LeakCanary 2.7 中修复

https://square.github.io/leakcanary/changelog/#finer-grained-root-view-watching

In version 2.6, LeakCanary added detection of root views retained after View.onDetachedFromWindow(). This helps find more leaks, but unfortunately some Android widgets keep a detached root view around to reattach it later (e.g. spinner). App developers also sometimes do the same with dialogs, keeping a single instance around and calling show() and hide() as needed. As a result, LeakCanary would report leaks that were actually not leaks.

In version 2.7, the default behavior changed: LeakCanary will continue to detect leaks of toasts, but will ignore root views created by a PopupWindow (which is what Android widgets use). It will also ignore root views created by a dialog by default, and you can turn this back on by setting the leak_canary_watcher_watch_dismissed_dialogs resource boolean to true: