当我的活动中有零代码时,微调器正在泄漏

如何解决当我的活动中有零代码时,微调器正在泄漏

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

public class MainActivity extends AppCompatActivity {

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

我的 activity_main 的 XML 布局:

在我的 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>

enter image description here

您可以自己尝试使用

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

在 2.6 版本中,LeakCanary 添加了对 View.onDetachedFromWindow() 之后保留的根视图的检测。这有助于找到更多的泄漏,但不幸的是,一些 Android 小部件保留了一个分离的根视图,以便稍后重新附加它(例如 Spinner)。应用程序开发人员有时也会对对话框执行相同的操作,保留单个实例并根据需要调用 show() 和 hide()。因此,LeakCanary 会报告实际上并非泄漏的泄漏。

在 2.7 版本中,默认行为发生了变化:LeakCanary 将继续检测 toast 的泄漏,但将忽略由 PopupWindow(这是 Android 小部件使用的)创建的根视图。默认情况下,它还将忽略由对话框创建的根视图,您可以通过将 leak_canary_watcher_watch_dismissed_dialogs 资源布尔值设置为 true 来重新启用它:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-