android linkToDeath内存泄露分析

2023-10-27


Register the recipient for a notification if this binder goes away. //注册binder死亡的通知

public void linkToDeath(@NonNull DeathRecipient recipient, int flags) throws RemoteException;

现场复现

提供给三方应用的sdk中的实现,而对外提供的是一个Manager 单例

private final IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
    @Override
    public void binderDied() {
        bindService(); //在service死亡的时候重新拉起保活
    }
};

发现三方应用actiivty在销毁后有内存泄漏,持有引用的是deathReceipent

private final ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
       
        try {
            iBinder.linkToDeath(mDeathRecipient, 0);
        } catch (Throwable e) {
            Log.e(TAG, "linkToDeath fail : ", e);
        }
    }
​​​​​​​}

这里service重新链接的时候,之前已经链接的java对象iBinder会被回收,而注册到iBinder的deathRecipient没有被回收导致内存泄漏

​​​​​​​

​疑问

​​​​​​​iBinder释放的时候是否是unlink DeathRecipient,如果主动释放就不会存在内存泄漏的问题

BinderProxy.java

​​​​​        public static final long sNativeFinalizer = getNativeFinalizer();
        /* Returns the native free function 随java对象一起释放的native对象 */
        private static native long getNativeFinalizer();
        public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
                BinderProxy.class.getClassLoader(), sNativeFinalizer, NATIVE_ALLOCATION_SIZE);

frameworks/base/core/jni/android_util_Binder.cpp

JNIEXPORT jlong JNICALL android_os_BinderProxy_getNativeFinalizer(JNIEnv*, jclass) {
    return (jlong) BinderProxy_destroy;
}
static void BinderProxy_destroy(void* rawNativeData)
{
    BinderProxyNativeData * nativeData = (BinderProxyNativeData *) rawNativeData;
    LOGDEATH("Destroying BinderProxy: binder=%p drl=%p\n",
            nativeData->mObject.get(), nativeData->mOrgue.get());
    delete nativeData;
    IPCThreadState::self()->flushCommands();
    --gNumProxies;
}

struct BinderProxyNativeData {
    // Both fields are constant and not null once javaObjectForIBinder returns this as
    // part of a BinderProxy.

    // The native IBinder proxied by this BinderProxy.
    sp<IBinder> mObject;

    // Death recipients for mObject. Reference counted only because DeathRecipients
    // hold a weak reference that can be temporarily promoted.
    sp<DeathRecipientList> mOrgue;  // Death recipients for mObject.
};

如上,iBinder销毁只是简单调用了BinderProxyNativeData析构函数,并没有将JavaDeathRecipient从List中移除,

如果发现binderProxy销毁了,并且List不为空,则会发出内存泄露警告

DeathRecipientList::~DeathRecipientList() {
    LOGDEATH("Destroy DRL @ %p", this);
    AutoMutex _l(mLock);

    // Should never happen -- the JavaDeathRecipient objects that have added themselves
    // to the list are holding references on the list object.  Only when they are torn
    // down can the list header be destroyed.
    if (mList.size() > 0) {
        List< sp<JavaDeathRecipient> >::iterator iter;
        for (iter = mList.begin(); iter != mList.end(); iter++) {
            (*iter)->warnIfStillLive();
        }
    }
}
class JavaDeathRecipient : public IBinder::DeathRecipient
{
public:
    JavaDeathRecipient(JNIEnv* env, jobject object, const sp<DeathRecipientList>& list)
        : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)),
          mObjectWeak(NULL), mList(list)
    {
        // These objects manage their own lifetimes so are responsible for final bookkeeping.
        // The list holds a strong reference to this object.
        LOGDEATH("Adding JDR %p to DRL %p", this, list.get());
        list->add(this);

        gNumDeathRefsCreated.fetch_add(1, std::memory_order_relaxed);
        gcIfManyNewRefs(env);
    } 
    
    void clearReference()
    {
        sp<DeathRecipientList> list = mList.promote();
        if (list != NULL) {
            LOGDEATH("Removing JDR %p from DRL %p", this, list.get());
            list->remove(this);
        } else {
            LOGDEATH("clearReference() on JDR %p but DRL wp purged", this);
        }
    }

    void warnIfStillLive() {
        if (mObject != NULL) {
            // Okay, something is wrong -- we have a hard reference to a live death
            // recipient on the VM side, but the list is being torn down.
            JNIEnv* env = javavm_to_jnienv(mVM);
            ScopedLocalRef<jclass> objClassRef(env, env->GetObjectClass(mObject));
            ScopedLocalRef<jstring> nameRef(env,
                    (jstring) env->CallObjectMethod(objClassRef.get(), gClassOffsets.mGetName));
            ScopedUtfChars nameUtf(env, nameRef.get());
            if (nameUtf.c_str() != NULL) {
                ALOGW("BinderProxy is being destroyed but the application did not call "
                        "unlinkToDeath to unlink all of its death recipients beforehand.  "
                        "Releasing leaked death recipient: %s", nameUtf.c_str());
            } else {
                ALOGW("BinderProxy being destroyed; unable to get DR object name");
                env->ExceptionClear();
            }
        }
    }
    }

List添加是在JavaDeathRecipient构造函数中, 移除只有在unlinkToDeath的时候调用clearReference会从list中移除

static jboolean android_os_BinderProxy_unlinkToDeath(JNIEnv* env, jobject obj,
                                                 jobject recipient, jint flags)
{
    jboolean res = JNI_FALSE;
    if (recipient == NULL) {
        jniThrowNullPointerException(env, NULL);
        return res;
    }

    BinderProxyNativeData* nd = getBPNativeData(env, obj);
    IBinder* target = nd->mObject.get();
    if (target == NULL) {
        ALOGW("Binder has been finalized when calling linkToDeath() with recip=%p)\n", recipient);
        return JNI_FALSE;
    }

    LOGDEATH("unlinkToDeath: binder=%p recipient=%p\n", target, recipient);

    if (!target->localBinder()) {
        status_t err = NAME_NOT_FOUND;

        // If we find the matching recipient, proceed to unlink using that
        DeathRecipientList* list = nd->mOrgue.get();
        sp<JavaDeathRecipient> origJDR = list->find(recipient);
        LOGDEATH("   unlink found list %p and JDR %p", list, origJDR.get());
        if (origJDR != NULL) {
            wp<IBinder::DeathRecipient> dr;
            err = target->unlinkToDeath(origJDR, NULL, flags, &dr);
            if (err == NO_ERROR && dr != NULL) {
                sp<IBinder::DeathRecipient> sdr = dr.promote();
                JavaDeathRecipient* jdr = static_cast<JavaDeathRecipient*>(sdr.get());
                if (jdr != NULL) {
                    jdr->clearReference();//从list中移除
                }
            }
        }

        if (err == NO_ERROR || err == DEAD_OBJECT) {
            res = JNI_TRUE;
        } else {
            jniThrowException(env, "java/util/NoSuchElementException",
                              base::StringPrintf("Death link does not exist (%s)",
                                                 statusToString(err).c_str())
                                      .c_str());
        }
    }

    return res;
}

解决方案

在收到binderDied通知后主动调用unlinkToDeath

private static class DeathRecipient implements IBinder.DeathRecipient {
    private IBinder mBinder;
    private Manager mManager;

    public DeathRecipient(IBinder iBinder, Manager manager) {
        mBinder = iBinder;
        mManager = manager;
    }

    @Override
    public void binderDied() {
        mBinder.unlinkToDeath(this, 0);
        mManager.bindService();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android linkToDeath内存泄露分析 的相关文章

  • Android:java.lang.OutOfMemoryError:

    我在 Android 上开发了一个使用大量图像的应用程序 可绘制文件夹中有很多图像 比如说超过 100 张 我正在开发图像动画应用程序 我使用 imageview 来显示 GIF 图像 我使用了将 gif 图像分割成多个 PNG 格式图像的
  • 如何从 Java 访问 Windows 设备管理器中的信息?

    我有一个串行 USB 设备 并且其中多个设备可以连接到计算机 我需要查询和检索设备连接到的 COM 端口列表 在 Windows 设备管理器中 您可以获得当前连接的设备的 COM 端口 友好名称 该列表是动态的 从注册表中读取不工作 htt
  • C++/Windows:如何报告内存不足异常(bad_alloc)?

    我目前正在为 Windows MSVC 9 0 应用程序开发基于异常的错误报告系统 即异常结构和类型 继承 调用堆栈 错误报告和日志记录等 我现在的问题是 如何正确报告和记录内存不足错误 当发生此错误时 例如作为bad alloc抛出的ne
  • 获取Android库中的上下文

    我正在编写一个 Android 应用程序 它的一些功能封装在内部库中 但是 要使此功能发挥作用 库需要一个应用程序上下文的实例 为图书馆提供这种上下文的最佳方式是什么 我看到了一些选择 但没有一个有吸引力 Have my library c
  • NDK8d 未找到 std::string 操作(即 stol、stoi)

    我尝试使用 ndk r8d 和 c 11 支持来建立我的第一个 android 项目 一些 c 11 机制工作正常 即 lambda 表达式 但是当我尝试时 使用新的字符串操作之一 编译失败 错误 stol 不是 std 的成员 这是我的
  • 改造将多个图像上传到单个密钥

    我正在使用 Retrofit 将图像上传到我的服务器 这里我需要为一个密钥上传多个图像 我已经尝试使用 Postman 网络客户端 它运行良好 这是一个屏幕截图 以下是请求的键值对 调查图像 文件1 文件2 文件3 属性图像 文件DRA j
  • 返回到上一个活动的最后状态

    我有两个活动 A 和 B 当应用程序启动时 活动 A 会被加载 我会从数据库将数据加载到其中 我可以毫无问题地转到活动 B 但是当我返回活动 A 时 会查看旧数据一段时间 然后重新加载活动 知道如何解决这个问题吗 在从数据库加载数据之前清除
  • 可以用 Django 制作移动应用程序吗?

    我想知道我是否可以在我的网站上使用 Django 代码 并以某种方式在移动应用程序 Flutter 等框架中使用它 那么是否可以使用我现在拥有的 Django 后端并在移动应用程序中使用它 所以就像models views etc 是的 有
  • Ionic 框架 - Config.xml

    我需要修改 config xml 文件 因此在针对 Android 进行编译时我会获取以下权限
  • 如何去掉android状态栏的电池图标?

    我通过去掉背景图像来删除 Android 中显示网络 电池和时间信息的状态栏 但图标仍然存在 我也想知道如何删除电池图标 不是用于应用程序 而是用于框架开发 提前致谢 你试过打电话吗 getWindow setFlags WindowMan
  • 当类明显存在时,我收到 java.lang.NoClassDefFoundError

    当我尝试在运行时使用该库时 出现以下错误 java lang NoClassDefFoundError com google api client extensions android2 AndroidHttp 我已经将此库添加到我的项目中
  • Google Wallet for Digital Goods API 与 Google Play 应用内结算

    想知道 Google 电子钱包结算 API 和 Google Play 应用内结算之间有什么区别 与 Google 电子钱包结算 API 相比 使用 GooglePlay 应用内购买结算服务有何优势 我看到 Wallet API 也支持 A
  • 以编程方式创建 FloatingActionButton(无需 xml)

    我很欣赏 Android 的 FloatingActionButton fab 功能 并希望在我的项目中的许多不同地方使用它们 现在 我有这样的东西 我有几个 xml 规范 除了 id 图标和 onclick 之外 所有这些规范都是相同的
  • Android Studio - 无法解析符号“firebase”

    我目前正在将应用程序升级到新的 Firebase 版本 我按照指南进行操作 包括classpath com google gms google services 3 0 0 在我的项目 build gradle 的依赖项中以及compile
  • Firebase:用户注册后如何进行电话号码验证?

    所以我知道我可以使用电子邮件验证或电话号码验证 但我想做的是在用户注册或登录后进行电话号码验证 如何连接这两种身份验证方法 最后 Firebase中是否有一个函数可以检查用户是否通过电话号码验证 谢谢 即使用户已通过身份验证 您仍然可以使用
  • 使用bindService启动IntentService时是否应该调用onHandleIntent?

    我的服务延伸IntentService当它开始时startService onHandleIntent被叫 但是 当服务启动时bindService 我确实需要绑定 onHandleIntent没有被调用 Should onHandleIn
  • 我想要有条件的登录导航,没有 MAIN 片段或按钮

    我正在使用 Android Jetpack 导航组件 实时数据和 Firebase 我希望工作流程就像用户打开应用程序时一样 然后根据登录 注销状态导航到登录 配置文件片段 而不需要任何主片段或按钮 请 我的应用程序中没有主要片段 用户启动
  • 带有不透明导航栏的深色文本透明状态栏

    等等 这不是重复的 类似的问题已被问到here https stackoverflow com questions 38025865 android fully transparent status bar with non transpa
  • 从 Dropbox 下载文件并将其保存到 SDCARD

    现在我真的很沮丧 我想从 Dropbox 下载一个文件并将该文件保存到 SD 卡中 我得到的代码为 private boolean downloadDropboxFile String dbPath File localFile throw
  • 无法解析“:feature@debugFeature/compileClasspath”的依赖关系:无法解析androidx.annotation:annotation:1.0.0-rc01

    将 Android Studio 更新到 3 3 Canary 10 从之前的 Canary 后 我开始遇到 Gradle 同步问题 ERROR Unable to resolve dependency for feature debugF

随机推荐

  • 95-38-030-Buffer-Java NIO中-关于DirectBuffer,HeapBuffer的疑问

    文章目录 1 说明 2 疑问 3 RednaxelaFX 1 说明 本文摘要 https www zhihu com question 57374068 2 疑问 Java NIO中 关于DirectBuffer HeapBuffer的疑问
  • CUDA的几种Synchronize

    首先对这三个函数做一下解释 cudaDeviceSynchronize 等待所有线程都处理完成 kernel function处理完成 用在cpu的c code中 cudaThreadSynchronize 功能和cudaDeviceSyn
  • Pycharm的使用技巧与效率提升

    总第010篇 本文主要梳理了pycharm在使用过程中的一些技巧 便于提升工作效率 pycharm主要分为两个版本 一个是专业版本 此版本功能强大 主要是为python和web开发者准备的 需要付费 另一个是社区版本 比较轻量级 主要是为p
  • 读别人写的代码 VS 自己写代码

    概述 专业程序员非常重要的一项技能是读别人写的代码 这项技能甚至比自己写代码更重要 分析 这让我想到很多程序员讨厌去阅读代码 来接受它吧 人人都喜欢编写代码 写代码是很有乐趣的事 但阅读代码却是一种困难的工作 它不仅仅繁重 而且很无聊 让我
  • 使用Docker进行模型部署

    文章目录 1 思路 2 实现步骤 2 1 数据 模型准备 2 2 镜像制作 2 3 使用 1 思路 因为多数公司正式集群都不能使用公网环境 对于模型部署比较麻烦 所以想这在公网环境下完成模型调试 然后根据相关环境和参数直接制作一个docke
  • C++中static_cast/const_cast/dynamic_cast/reinterpret_cast的区别和使用

    C风格的强制转换较简单 如将float a转换为int b 则可以这样 b int a 或者b int a C 类型转换分为隐式类型转换和显示类型转换 隐式类型转换又称为标准转换 包括以下几种情况 1 算术转换 在混合类型的算术表达式中 最
  • gsonformat java代码_插件GsonFormat快速實現JavaBean

    寫在前面的話 本文章只適合使用AndroidStudio的小伙伴觀看 還在糾結eclipse的小伙伴趕緊洗洗睡吧 最近看見一篇快速實現javaBean的屎丟丟插件 這是一個根據JSONObject格式的字符串 自動生成實體類參數 如果想要使
  • windows编程中wParam和lParam消息

    windows编程中wParam和lParam消息 1 WM PAINT消息 LOWORD lParam 是客户区的宽 HIWORD lParam 是客户区的高 2 滚动条WM VSCROLL或WM HSCROLL消息 LOWORD wPa
  • 【Vim】Vim 常用编辑操作

    目录 正则表达式 vim 命令 vim的工作模式 撤销修改 重做与保存 光标移动命令 文本插入操作 文本删除操作 文本复制 剪切与粘贴 文本的修改与替换 多窗口操作 正则表达式 简单地说 正则表达式是一种符号表示法 用于识别文本模式 在某种
  • 神经网络中epoch、batch、batch_size、epoch、iteration理解

    1 epoch 当一个完整的数据集通过神经网络一次并且返回一次的过程称为一个epoch 然而 当一个epoch对于计算机太过庞大时 就需要把它分成多个小块 2 batch 在不能将数据一次性通过神经网络的适合 就需要将数据集分成几个batc
  • 记录服务器上,不定时出现io.lettuce.core.RedisCommandTimeoutException: Command timed out after xxx millisecond(s)

    记录服务器上 不定时出现io lettuce core RedisCommandTimeoutException Command timed out after 12 millisecond s 日志 org springframework
  • E--释怀的RT--2023河南萌新联赛第(二)场:河南工业大学

    示例1 输入 5 0 1 0 0 10 输出 4 说明 前四个方格被最后一个心岩照亮 示例2 输入 5 0 1 0 0 1 输出 3 说明 第一个方格和第三个方格被第二个格子的心岩照亮 第四个方格被第五个格子的心岩照亮 一共有三个格子被照亮
  • SQLZOO习题(我的错题)

    SQLZOO习题 目录 SQLZOO习题 一 All the vowels 参考答案 备注 二 Nobel Quiz 解释 三 Knights of the realm 答案 备注 四 Chemistry and Physics last
  • shell转义,单引号与双引号,反撇号

    http www cnblogs com mydomain archive 2011 10 15 2213017 html 1 转义 单引号和双引号都能关闭shell对特殊字符的处理 不同的是 双引号没有单引号严格 单引号关闭所有有特殊作用
  • 我们的那些故事(写给1987—1990年出生的同学,希望您能够看看)

    花开无声 岁月无痕 突然回首 人生的旅途已经走了四分之一 我们这一辈 也就是96 97年开始上小学 02年上初中 05年上高中 07 08 09年上大学的 12年或者13年毕业的 我们知道的太多 看到了太多 听到了太多 新的 旧的 保守的
  • linux 查看内存 udimm rdimm,服务器UDIMM、LRDIMM、RDIMM三种内存如何区别

    随着应用程序的不断增长 内存被迫承担着更大压力 目前不管是服务器还是PC领域 DDR4内存技术依旧是主流 由于DDR4采用并行传输 为保证并行数据能有效传输 必须在内存条上下功夫 而在服务器领域 目前使用的内存条类型 DIMM 主要有三种
  • 关于STM32通用定时器更新事件中断

    定时器3中断服务程序 void TIM3 IRQHandler void if TIM3 gt SR 0X0001 产生更新事件 LED1 LED1 LED0 LED0 TIM3 gt SR 1 lt lt 0 清除中断标志位 通用定时器中
  • iOS常用类库

    ios 常用第三方类库 分享类型 游戏开发相关 http blog csdn net wstarx article details 6317779 http iosdeveloper diandian com post 2011 05 21
  • 【项目】小帽外卖(十四)

    小帽外卖 第十四章 Nginx 一 Nginx 概述 1 Nginx 介绍 Nginx是一款轻量级的Web 服务器 反向代理服务器及电子邮件 IMAP POP3 代理服务器 其特点是占有内存少 并发能力强 事实上nginx的并发能力在同类型
  • android linkToDeath内存泄露分析

    Register the recipient for a notification if this binder goes away 注册binder死亡的通知 public void linkToDeath NonNull DeathRe