Android Studio 内存泄漏活动未在线程中完成

Posted

技术标签:

【中文标题】Android Studio 内存泄漏活动未在线程中完成【英文标题】:Android studio memory leak activity does not finish in a thread 【发布时间】:2017-08-08 18:28:11 【问题描述】:

我的活动似乎没有正确完成,因为每次启动和关闭它时都会消耗更多的内存。有人可以建议我一个想法吗?这是代码:

new Thread(new Runnable() 
        @Override
        public void run() 
            Looper.prepare();
            ProgressDialog mProgressDialog = new ProgressDialog(thisActivity);
            mProgressDialog.setTitle("Getting location");
            mProgressDialog.setMessage("Please wait while the device is being located.");
            mProgressDialog.show();
            long startTime = SystemClock.currentThreadTimeMillis();
            long time = SystemClock.currentThreadTimeMillis();
            while (mLastLocation == null)
            
                if(SystemClock.currentThreadTimeMillis() - startTime >= 5000)
                    mLastLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                
                if(SystemClock.currentThreadTimeMillis() - time >= 1000) 
                    if (ActivityCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
                        return;
                    
                    mLocationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mLocationListener, null);
                    time = SystemClock.currentThreadTimeMillis();
                
            

            String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());

            ServiceModel model = new ServiceModel(0, Globals.LoggedUser.Id, ScheduleAdapter.currentToilet.Id, edt_note.getText().toString(), photo, null, reason, date, mLastLocation.getLatitude(), mLastLocation.getLongitude());
            Globals.DomainServiceManager.ServiceRepository.Create(model);
            runOnUiThread(new Runnable() 
                @Override
                public void run() 
                    photo = null;
                    imv_camera.setImageBitmap(null);
                    ActivityScheduleList.setToolbarPosition(0, DATA_NOT_CAPTURED);
                
            );
            mProgressDialog.dismiss();
            ActivityNotServeced.this.finish();
        
    ).start();

【问题讨论】:

看来这个线程正在处理 UI 元素。你不应该这样做 【参考方案1】:

android Studio 首先有一个built in support in finding a memory leak。另一种选择是使用Leak Canary。现在,在您的代码中,我看到:mLocationManager。我怀疑这会导致泄漏。您可以使用我之前提到的方法进行验证。构建它时,不要传递活动上下文 - 使用 mActivity.getApplicationContext()

【讨论】:

【参考方案2】:

在少数情况下您的 ProgressDialog 永远不会被取消,例如

 while (mLastLocation == null)
        
            if(SystemClock.currentThreadTimeMillis() - startTime >= 5000)
                mLastLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            
            if(SystemClock.currentThreadTimeMillis() - time >= 1000) 
                if (ActivityCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
                    return;
                
                mLocationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mLocationListener, null);
                time = SystemClock.currentThreadTimeMillis();
            
        

您的线程永远无法完成,但它拥有对您的活动的引用(例如在 ProgressDialog 中)。我建议你重构你的代码。例如,您可以在 onStop() 方法中取消()您的 ProgressDialog。

【讨论】:

以上是关于Android Studio 内存泄漏活动未在线程中完成的主要内容,如果未能解决你的问题,请参考以下文章

手把手教你在Android Studio 3.0上分析内存泄漏

基于Android Studio的内存泄漏检测与解决

基于Android Studio的内存泄漏检测与解决全攻略

AsyncTask *偶尔*遇到连接超时/错误问题、潜在的内存泄漏或 Android Studio 错误?

我们必须在ondestroy方法中中断后台线程吗?

android Leakcanary/Studio Profiler/MAT 处理内存问题(泄漏和Big超大内存对象)