BinderProxy 泄露导致的 Crash

简介: 无

问题描述

国庆后正式辞职了,在交接完成前,也就摸摸鱼或者帮同事分析一些Jira上严重的bug,同事负责的车载项目已经进行小批量试产,Monkey 测试的强度也开始提高,然后不出意外的话是要出意外了,一个车辆核心功能的 service 在高强度的 monkey 测试中几乎必挂。

出问题的 service 负责车辆『数据埋点』通信的业务,车机中几十个应用和服务都需要经过这个 service 来向 『T-box』 汇报埋点数据,IPC 通信方式使用经典的 AIDL 实现。

完整的日志如下:

01-01 13:36:45.936     0     0 I binder  : release 1684:1718 transaction 57988729 in, still active
01-01 13:36:45.936     0     0 I binder  : send failed reply for transaction 57988729 to 3135:3254
09-30 21:53:02.514  1684  1718 E AndroidRuntime: FATAL EXCEPTION: Binder:1684_2
09-30 21:53:02.514  1684  1718 E AndroidRuntime: Process: com.xxx.xxx.xxxservice, PID: 1684
09-30 21:53:02.514  1684  1718 E AndroidRuntime: java.lang.AssertionError: Binder ProxyMap has too many entries: 20691 (total), 20691 (uncleared), 20691 (uncleared after GC). BinderProxy leak?
09-30 21:53:02.514  1684  1718 E AndroidRuntime:         at android.os.BinderProxy$ProxyMap.set(BinderProxy.java:230)
09-30 21:53:02.514  1684  1718 E AndroidRuntime:         at android.os.BinderProxy.getInstance(BinderProxy.java:432)
09-30 21:53:02.514  1684  1718 E AndroidRuntime:         at android.os.Parcel.nativeReadStrongBinder(Native Method)
09-30 21:53:02.514  1684  1718 E AndroidRuntime:         at android.os.Parcel.readStrongBinder(Parcel.java:2483)
09-30 21:53:02.514  1684  1718 E AndroidRuntime:         at com.xxx.xxx.xxxservice.diagnostics.IDiagnosticsEvent$Stub.onTransact(IDiagnosticsEvent.java:121)
09-30 21:53:02.514  1684  1718 E AndroidRuntime:         at android.os.Binder.execTransactInternal(Binder.java:1154)
09-30 21:53:02.514  1684  1718 E AndroidRuntime:         at android.os.Binder.execTransact(Binder.java:1123)
09-30 21:53:02.221   754   754 I chatty  : uid=1000(system) Binder:754_3 identical 1 line
09-30 21:53:02.222   754   754 D NotificationService: 0|com.xxx.xxx.persontime|124|null|1000: updating permissions
09-30 21:53:02.521   754  4583 I DropBoxManagerService: add tag=system_app_crash isTagEnabled=true flags=0x2
09-30 21:53:02.523   754   962 I PackageWatchdog: Updated health check state for package com.xxx.xxx.xxxservice: INACTIVE -> INACTIVE
09-30 21:53:02.524   754  3061 W ActivityManager: Force-killing crashed app com.xxx.xxx.xxxservice at watcher's request
09-30 21:53:02.526  1684  1718 I Process : Sending signal. PID: 1684 SIG: 9

原因分析

问题从日志上看很明显了, Binder 也就是跨进程通信模块产生了内存泄露

java.lang.AssertionError: Binder ProxyMap has too many entries: 20691 (total), 20691 (uncleared), 20691 (uncleared after GC ). BinderProxy leak?

然后 linux 内核发送 signal=9 信号,系统主动杀死了该进程

Linux 信号也是一个非常有用的日志分析入口,有时候进程没有任何产生crash也被内核杀死的话,就可以考虑分析是不是收到了 Linux 的信号,再逐步往上推,往往会有意想不到的收获

09-30 21:53:02.526  1684  1718 I Process : Sending signal. PID: 1684 SIG: 9

继续看日志,可以发现实际触发binder泄露的代码在IDiagnosticsEvent.class的第121行

 AndroidRuntime:         at com.xxx.xxx.xxxservice.diagnostics.IDiagnosticsEvent$Stub.onTransact(IDiagnosticsEvent.java:121)

IDiagnosticsEvent.java是AIDL接口编译后产生的一个临时类,它的位置是:

工程MODULE/build/generated/aidl_source_output_dir/debug/out/com/xxx/xxx/xxxservice/xxx/

case  TRANSACTION_setSendListener:
{
  data.enforceInterface(descriptor);
  com.xxx.xxx.xxxservice.diagnostics.ISendEventListener _arg0;
_arg0 = com.xxx.xxx.xxxservice.diagnostics.ISendEventListener.Stub. asInterface (data.readStrongBinder());
  com.xxx.xxx.xxxservice.diagnostics.IDiagnosticsEvent _result = this.setSendListener(_arg0);
  reply.writeNoException();
  reply.writeStrongBinder((((_result!=null))?(_result.asBinder()):(null)));
  return  true;
}

第121行是 _arg0 = com.xxx.xxx.xxxservice.diagnostics.ISendEventListener.Stub. asInterface (data.readStrongBinder()); 也就是ISendEventListener这个类没有得到释放,那么就需要去代码中看setSendListener()这方法做了什么

在代码中setSendListener()是DiagnosticsEvent.class的一个成员方法,DiagnosticsEvent.class 本身也是一个Binder.Stub的逻辑实现类。

// DiagnosticsEvent
@Override
public IDiagnosticsEvent setSendListener(ISendEventListener listener) throws RemoteException {
    mListener = listener;
    mEvent.SetCallback(new DiagnosticsListener());
    return  this;
}

然后我们看一下 DiagnosticsListener.class这个类。

结果问题就出在这个在类中,DiagnosticListener也是一个Binder.Stub的实现类,但是作为内部类它持有了对外部类引用ISendEventListener。

到这里问题就已经很清晰了,是一个典型的内部类持有外部类的引用导致的内存泄露。

由于每次调用setSendListener()方法都会产生一个无法释放的DiagnosticsListener对象,久而久之就BinderProxy占用内存越来越大,最终导致进程被内核杀死。

private  class DiagnosticsListener extends ISendEventCallback.Stub {

    @Override
    public  void SendStatus(hal e) throws RemoteException {
        if (mListener != null) {
            EventInfos infos = new EventInfos();
            ...
            mListener.OnSendStatus(infos);
        }
    }
}

解决方案

既然知道了原因,那么修改就简单了:

1)内部类改为静态内部类,将对外部类的引用改为软引用

@Override
public IDiagnosticsEvent setSendListener(ISendEventListener listener) throws RemoteException {
    mEvent.SetCallback(new DiagnosticsListener(listener));
    return  this;
}

private  static  class DiagnosticsListener extends ISendEventCallback.Stub {

    private  final SoftReference<ISendEventListener> mSoftReference;

    public DiagnosticsListener(ISendEventListener listener) {
        mSoftReference = new SoftReference<>(listener);
    }

    @Override
    public  void SendStatus(t_hal e) throws RemoteException {
        ISendEventListener mListener = mSoftReference.get();
        if (mListener != null) {
            LogUtils.logD(TAG, "SendStatus" );
            EventInfos infos = new EventInfos();
            ...
            mListener.OnSendStatus(infos);
        }
    }
}

2)将内部类改为匿名内部类

匿名内部类不能绝对杜绝内存泄露,使用不当也会产生内存泄露

@Override
public IDiagnosticsEvent setSendListener(ISendEventListener listener) throws RemoteException {
    mEvent.SetCallback(new ISendEventCallback.Stub() {
        @Override
        public  void SendStatus(t_hal e) throws RemoteException {
            EventInfos infos = new EventInfos();
            ...
            listener.OnSendStatus(infos);
        }
    });
    return  this;
}

当然还有其它方法,比如把ISendEventCallback.Stub提前初始化成一个成员对象,而不是每次使用都去new等等。

问题排查完毕修改好代码后,再次执行 monkey 测试就没有出现该问题,应该是解决了。

相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
目录
相关文章
|
算法 编译器
内存学习(七):伙伴分配器(正式版)1
内存学习(七):伙伴分配器(正式版)1
671 0
|
5月前
|
存储 算法 数据安全/隐私保护
为什么RAR有RAR3、RAR5,唯独没有RAR4?一文彻底搞懂RAR加密原理与密码恢复
本文揭秘RAR格式命名之谜:所谓“消失”的RAR4实为RAR3(Format 2.9)的历史别称;梳理RAR2→RAR3→RAR5演进脉络,详解AES-128到AES-256、SHA-1到PBKDF2的加密升级,并解析密码恢复工具为何只标“RAR3/RAR5”——关键在加密结构,不在版本号。(239字)
1040 6
|
存储 安全 开发工具
深度解决 Git “fatal: refusing to merge unrelated histories” 错误解析什么是历史分支优雅草卓伊凡
深度解决 Git “fatal: refusing to merge unrelated histories” 错误解析什么是历史分支优雅草卓伊凡
1666 4
深度解决 Git “fatal: refusing to merge unrelated histories” 错误解析什么是历史分支优雅草卓伊凡
|
Java Linux API
统计android设备的网络数据使用量
统计android设备的网络数据使用量
1515 0
|
Linux 测试技术 语音技术
【车载Android】模拟Android系统的高负载环境
本文介绍如何将Linux压力测试工具Stress移植到Android系统,用于模拟高负载环境下的CPU、内存、IO和磁盘压力,帮助开发者优化车载Android应用在多任务并发时的性能问题,提升系统稳定性与用户体验。
1283 6
|
Android开发
Android源代码定制:Overlay目录定制|调试Overlay资源是否生效
Android源代码定制:Overlay目录定制|调试Overlay资源是否生效
2114 0
|
API 开发工具 Android开发
调用Android原生@SystemApi、@Hide方法
调用Android原生@SystemApi、@Hide方法
1629 1
|
缓存 安全 Java
Android中的persistent属性
Android中的persistent属性
1575 2
|
Android开发 Java C++
带你读《深入理解Android:Java虚拟机ART》之一:本书必读
本书将关注Android系统中至关重要的部分——Java虚拟机ART。随着Android设备的大规模普及,ART虚拟机已经成为当今使用最为广泛的JVM之一。所以,对ART虚拟机进行研究有着非同寻常的意义。本书的出现在一定程度上填补了这方面的空白。
|
API Android开发 开发者
failed to set system property error code: 0x18
failed to set system property error code: 0x18
1728 1