美文网首页
内存泄露

内存泄露

作者: guoheng1996 | 来源:发表于2021-04-28 17:49 被阅读0次

    内存泄漏(Memory Leak)是指程序中已动态分配的堆内存由于某种原因程序未释放或无法释放,造成系统内存的浪费,导致程序运行速度减慢甚至系统崩溃等严重后果。------来自百度百科

    听到内存泄露最多的地方估计就是工作中遇到以及在各种面试题中,
    经常能听到引起内存泄露如下所示:

    1. 非静态内部类handler
    2. 忘记解注册广播?????
    3. bitmap使用未回收
    4. 静态变量引用context
    5. hashmap使用自定义对象当key未重写hashcode,equals
    6. 跨进程通信
    7. 待补充

    说说我理解的内存泄露,一句话解释: 即当一个东西被你用完,你不再需要,在gc后他还存在内存中

    下面来看看几个例子

    1、非静态内部类handler
        public Handler mHandler1 = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
            }
        };
    
        public Handler mHandler2 = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                return false;
            }
        });
    
        public Handler mHandler3 = new MyHandler();
    
        public class MyHandler extends Handler{
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            findViewById(R.id.button).setOnClickListener(v -> {
                Toast.makeText(this, "你点击了-----", Toast.LENGTH_SHORT).show();
                Message obtain = Message.obtain();
                mHandler1.sendMessage(obtain);
                obtain = Message.obtain();
                mHandler2.sendMessage(obtain);
                obtain = Message.obtain();
                mHandler3.sendMessage(obtain);
            });
        }
    

    如上是我们经常使用handler的几种方法,以上三种都会造成内存泄露吗?
    测试代码为从MainActivity 跳转到MainActivity2 点击按钮,发送handler消息,然后退出MainActivity2界面,来回几次,然后利用profile抓取内存中dump信息,查看是否发生泄漏
    我们来实测一下: 使用工具 Android studio profiler mat

    截屏2021-04-25 下午10.04.47.png 说好的内存泄露呢?.png

    修改代码如下:

                Message obtain = Message.obtain();
                mHandler1.sendMessageDelayed(obtain, 25000);
                obtain = Message.obtain();
                mHandler2.sendMessageDelayed(obtain, 25000);
                obtain = Message.obtain();
                mHandler3.sendMessageDelayed(obtain, 25000);
    

    再次重复刚才的操作,欧吼,内存泄露出来了。

    截屏2021-04-25 下午10.27.23.png
    截屏2021-04-25 下午10.31.42.png

    可以看到当前引用链如下所示:

    ActivityThread -> MessageQueue -> Message -> target(此处就是我们的handler) -> MainActivity2
    
    至此我们就能发现handler并不一定能引起内存泄漏,仅仅当我们发送的消息还没处理完时,才会发生内存泄漏。
    2、忘记解注册广播会引起吗?

    关于忘记解注册广播,仅限于听说过,比较解注册的成本太低,随手一写就行,我们来实际测试一下:操作流程启动MainActivity2再推出,重复4次。

    public class MainActivity2 extends AppCompatActivity {
        public static final String TAG = "MainActivity2";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            IntentFilter intentFilter1 = new IntentFilter();
            intentFilter1.addAction(ACTION_MEDIA_MOUNTED);
            intentFilter1.addDataScheme("file");
            BroadcastReceiver aaa = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    Log.w(MainActivity2.TAG, "AAA");
                }
            };
            registerReceiver(aaa, intentFilter1);
        }
    }
    
    image.png

    可以看到其实是没有泄露的,Android studio 没检测出来。
    回家使用我的电脑:结果又是有的

    截屏2021-04-27 下午8.39.56.png

    接下来使用专业点的mat来看看。

    1. 选择Heap Dump文件保存下来。
    2. 使用sdk转换工具转换mat能识别格式。sdk/platform-tools/hprof-conv 原文件.hprof 新文件.hprof
    3. 使用mat打开新文件。查看我们关心的对象是否存在强引用
      具体使用可以参考如下链接:
      https://www.jianshu.com/p/7207deafd785

    使用公司电脑:

    image.png

    家里电脑:


    image.png

    到底是怎样一回事呢?通过查看Android-30源码,可以看到在activity解注册时,会清理掉当前注册的广播和绑定的service
    android.app.ActivityThread#handleDestroyActivity
    ......
    android.app.LoadedApk#removeContextRegistrations

    public void removeContextRegistrations(Context context,
                String who, String what) {
            final boolean reportRegistrationLeaks = StrictMode.vmRegistrationLeaksEnabled();
            synchronized (mReceivers) {
                ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> rmap =
                        mReceivers.remove(context);
                if (rmap != null) {
                    for (int i = 0; i < rmap.size(); i++) {
                        LoadedApk.ReceiverDispatcher rd = rmap.valueAt(i);
                        IntentReceiverLeaked leak = new IntentReceiverLeaked(
                                what + " " + who + " has leaked IntentReceiver "
                                + rd.getIntentReceiver() + " that was " +
                                "originally registered here. Are you missing a " +
                                "call to unregisterReceiver()?");
                        leak.setStackTrace(rd.getLocation().getStackTrace());
                        Slog.e(ActivityThread.TAG, leak.getMessage(), leak);
                        if (reportRegistrationLeaks) {
                            StrictMode.onIntentReceiverLeaked(leak);
                        }
                        try {
                            ActivityManager.getService().unregisterReceiver(
                                    rd.getIIntentReceiver());
                        } catch (RemoteException e) {
                            throw e.rethrowFromSystemServer();
                        }
                    }
                }
                mUnregisteredReceivers.remove(context);
            }
    
            synchronized (mServices) {
                //Slog.i(TAG, "Receiver registrations: " + mReceivers);
                ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> smap =
                        mServices.remove(context);
                if (smap != null) {
                    for (int i = 0; i < smap.size(); i++) {
                        LoadedApk.ServiceDispatcher sd = smap.valueAt(i);
                        ServiceConnectionLeaked leak = new ServiceConnectionLeaked(
                                what + " " + who + " has leaked ServiceConnection "
                                + sd.getServiceConnection() + " that was originally bound here");
                        leak.setStackTrace(sd.getLocation().getStackTrace());
                        Slog.e(ActivityThread.TAG, leak.getMessage(), leak);
                        if (reportRegistrationLeaks) {
                            StrictMode.onServiceConnectionLeaked(leak);
                        }
                        try {
                            ActivityManager.getService().unbindService(
                                    sd.getIServiceConnection());
                        } catch (RemoteException e) {
                            throw e.rethrowFromSystemServer();
                        }
                        sd.doForget();
                    }
                }
                mUnboundServices.remove(context);
                //Slog.i(TAG, "Service registrations: " + mServices);
            }
        }
    
    这是什么情况.png

    接下来排查一下为什么 会这样子:

    // android.app.ActivityThread#handleDestroyActivity 中在清理之前会有一个判断,通过打断点调试,可以看到发生内存泄漏此工程当前c对象不是ContextImpl 而是ContextThemeWrapper
            Context c = r.activity.getBaseContext();
            if (c instanceof ContextImpl) {
                ((ContextImpl) c).scheduleFinalCleanup(
                        r.activity.getClass().getName(), "Activity");
            }
    

    继续排查为啥出现这种情况

    泄漏版本:

    image.png image.png image.png

    未泄漏版本:

    image.png image.png

    关键原因就是

    implementation 'androidx.appcompat:appcompat:1.2.0'
    // 这个库1.1.0版本不会泄漏,1.2.0版本会泄漏。
    变更细节如下:有兴趣自行阅读
    https://developer.android.google.cn/jetpack/androidx/releases/appcompat?hl=zh-cn
    
    3、我和内存泄漏的故事

    在我实际遇到并解决内存泄漏问题之前的感觉就是:高端,经常能在博客,微信公众号上面看到,但是从没接触,很陌生。
    在自己真正解决过之后:就这?
    问题如下:

            Intent intent = new Intent(this, MyService.class);
            ServiceConnection serviceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Toast.makeText(MainActivity2.this, "bind success", Toast.LENGTH_SHORT).show();
                    iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                }
    
                @Override
                public void onServiceDisconnected(ComponentName name) {
    
                }
            };
            bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
            ILoginListener.Stub onLoginResult1 = new ILoginListener.Stub() {
    
                @Override
                public void onLoginResult() throws RemoteException {
                    Toast.makeText(MainActivity2.this, "onLoginResult",
                            Toast.LENGTH_SHORT).show();
                }
            };
            try {
                iMyAidlInterface.registerMediaListener(onLoginResult1);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
    
    image.png

    其中 ILoginListener.Stub 即为一个binder对象

        public Binder(@Nullable String descriptor)  {
            mObject = getNativeBBinderHolder();
            NoImagePreloadHolder.sRegistry.registerNativeAllocation(this, mObject);
    
            if (FIND_POTENTIAL_LEAKS) {
                final Class<? extends Binder> klass = getClass();
                if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                        (klass.getModifiers() & Modifier.STATIC) == 0) {
                    Log.w(TAG, "The following Binder class should be static or leaks might occur: " +
                        klass.getCanonicalName());
                }
            }
            mDescriptor = descriptor;
        }
    
    public final void writeStrongBinder(IBinder val) {
            //调用native方法
            nativeWriteStrongBinder(mNativePtr, val);
        }
    
    static void android_os_Parcel_writeStrongBinder(JNIEnv* env, jclass clazz, jint nativePtr, jobject object)  
    {  
        Parcel* parcel = reinterpret_cast<Parcel*>(nativePtr);  
        if (parcel != NULL) {
            //ibinderForJavaObject,这里的object就是对应java层IBinder也就是networkCallback
            const status_t err = parcel->writeStrongBinder(ibinderForJavaObject(env, object));  
            if (err != NO_ERROR) {  
                signalExceptionForError(env, clazz, err);  
            }  
        }  
    } 
    
    sp<IBinder> ibinderForJavaObject(JNIEnv* env, jobject obj)  
    {  
        if (obj == NULL) return NULL;  
        //这里obj是Java层的Binder对象,走下面这部分逻辑。最后调用jbh->get获得native层的IBinder对象指针。
        if (env->IsInstanceOf(obj, gBinderOffsets.mClass)) {  
            JavaBBinderHolder* jbh = (JavaBBinderHolder*)env->GetIntField(obj, gBinderOffsets.mObject);  
            return jbh != NULL ? jbh->get(env, obj) : NULL;  
        }  
        if (env->IsInstanceOf(obj, gBinderProxyOffsets.mClass)) {  
            return (IBinder*)env->GetIntField(obj, gBinderProxyOffsets.mObject);  
        }  
        ALOGW("ibinderForJavaObject: %p is not a Binder object", obj);  
        return NULL;  
    }
    
    sp<JavaBBinder> get(JNIEnv* env, jobject obj)  
    {  
        AutoMutex _l(mLock);  
        sp<JavaBBinder> b = mBinder.promote();  
        if (b == NULL) {  
            b = new JavaBBinder(env, obj);  
            mBinder = b;  
            ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%d\n",  
                 b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount());  
        }  
    
        return b;  
    }
    
    JavaBBinder(JNIEnv* env, jobject object)  
        : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object))  
        //here,创建了一个全局引用,如不主动调用env->DeleteGlobalRef(object),Java层的对象也就是networkCallback就不会被释放。
    {  
        ALOGV("Creating JavaBBinder %p\n", this);  
        android_atomic_inc(&gNumLocalRefs);  
        incRefsCreated(env);  
    }
    
    详细分析跨进程内存泄漏:
    https://blog.csdn.net/skqcsy/article/details/51882049
    

    HashMap 例子

    https://juejin.cn/post/6854573213427433480

    小技巧

    经常打开Android studio 查看源码,发现跳转失败,报红,是SDK未下载完整版
    https://github.com/anggrayudi/android-hidden-api
    https://drive.google.com/drive/folders/17oMwQ0xBcSGn159mgbqxcXXEcneUmnph
    下载对应版本,替换即可众享丝滑

    相关文章

      网友评论

          本文标题:内存泄露

          本文链接:https://www.haomeiwen.com/subject/rlkjrltx.html