It seems like my activity does not finish properly because I get a bigger memory consumption every time I launch and close it. Can someone suggest me an idea? Here is the code:
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();
First thing the Android Studio has a built in support in finding a memory leak . Another option is to use Leak Canary . Now, in your code I see: mLocationManager
. I suspect it this causes the leak. You can verify it with the methods I mentioned earlier. When you constarct it, don't pass the activity context - use the mActivity.getApplicationContext()
.
There are few conditions where your ProgressDialog is never canceled, for example
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();
}
}
Your thread could never finished but it holds the refference to your activity (for example in ProgressDialog). I suggest you to refactor your code. For example you can cancel() your ProgressDialog in onStop() method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.