泄漏 com.myapp.MainActivity 实例 6.9mb
leaks com.myapp.MainActivity instance 6.9mb
嘿,最近我在我的应用程序中添加了 leak-canary,我的 GPSTracker
class 上出现了这个奇怪的泄漏。
泄漏报告 说:
02-07 17:42:35.524 25788-26863/com.myapp D/LeakCanary: * com.myapp.MainActivity has leaked:
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * GC ROOT android.location.LocationManager$ListenerTransport.this[=11=]
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * references android.location.LocationManager.mContext
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * references android.app.ContextImpl.mOuterContext
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * leaks com.myapp .MainActivity instance
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Retaining: 6.9 MB.
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Reference Key: d4781b46-49a2-426c-b2ba-15b9a1207dd6
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Device: LGE google Nexus 5 hammerhead
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Android Version: 6.0.1 API: 23 LeakCanary: 1.5 00f37f5
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Durations: watch=5106ms, gc=156ms, heap dump=5555ms, analysis=32364ms
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Instance of android.location.LocationManager$ListenerTransport
我的 GPSTracker Class 是
public class GPSTracker implements LocationListener {
private Context context;
private boolean isGPSEnabled = false;
private boolean canGetLocation = false;
private Location location; // location
private double latitude; // latitude
private double longitude; // longitude
private final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5; // 5 meters
private final long MIN_TIME_BW_UPDATES = 1500; // 15 sec
private LocationManager locationManager;
public GPSTracker(Context context) {
this.context = context;
getLocation();
}
public boolean getGpsStatus() {
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
return isGPSEnabled;
}
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
Log.i("No Network Provider", "No Network Provider Available");
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, GPSTracker.this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("gps", "Location got from NETWORK");
Log.e("Current Location", "Lat : " + latitude + " Long : " + longitude);
}
}
} else {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, GPSTracker.this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("gps", "Location got from GPS");
Log.e("Current Location", "Lat : " + latitude + " Long : " + longitude);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
@Override
public void onLocationChanged(Location location) {
this.location = location;
try {
Log.e("GPS Status", "Lat : " + location.getLatitude() + " Long : " + location.getLatitude());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
onLocationChanged(locationManager.getLastKnownLocation(provider));
locationManager.requestLocationUpdates(provider, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
protected void finalize() throws Throwable {
removeLocationListener();
super.finalize();
}
public void removeLocationListener() {
context=null;
locationManager.removeUpdates(GPSTracker.this);
locationManager=null;
location=null;
}
}
你可以注意到我在 MainActivity
销毁时尝试删除更新。但是一次又一次的泄漏仍然没有任何改变。
困惑!
GPSTracker class 保持对 MainActivity 的引用。尝试这样做:
public GPSTracker(Context context) {
this.context = context.getApplicationContext();
getLocation();
}
嘿,最近我在我的应用程序中添加了 leak-canary,我的 GPSTracker
class 上出现了这个奇怪的泄漏。
泄漏报告 说:
02-07 17:42:35.524 25788-26863/com.myapp D/LeakCanary: * com.myapp.MainActivity has leaked:
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * GC ROOT android.location.LocationManager$ListenerTransport.this[=11=]
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * references android.location.LocationManager.mContext
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * references android.app.ContextImpl.mOuterContext
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * leaks com.myapp .MainActivity instance
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Retaining: 6.9 MB.
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Reference Key: d4781b46-49a2-426c-b2ba-15b9a1207dd6
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Device: LGE google Nexus 5 hammerhead
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Android Version: 6.0.1 API: 23 LeakCanary: 1.5 00f37f5
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Durations: watch=5106ms, gc=156ms, heap dump=5555ms, analysis=32364ms
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Instance of android.location.LocationManager$ListenerTransport
我的 GPSTracker Class 是
public class GPSTracker implements LocationListener {
private Context context;
private boolean isGPSEnabled = false;
private boolean canGetLocation = false;
private Location location; // location
private double latitude; // latitude
private double longitude; // longitude
private final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5; // 5 meters
private final long MIN_TIME_BW_UPDATES = 1500; // 15 sec
private LocationManager locationManager;
public GPSTracker(Context context) {
this.context = context;
getLocation();
}
public boolean getGpsStatus() {
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
return isGPSEnabled;
}
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
Log.i("No Network Provider", "No Network Provider Available");
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, GPSTracker.this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("gps", "Location got from NETWORK");
Log.e("Current Location", "Lat : " + latitude + " Long : " + longitude);
}
}
} else {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, GPSTracker.this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("gps", "Location got from GPS");
Log.e("Current Location", "Lat : " + latitude + " Long : " + longitude);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
@Override
public void onLocationChanged(Location location) {
this.location = location;
try {
Log.e("GPS Status", "Lat : " + location.getLatitude() + " Long : " + location.getLatitude());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
onLocationChanged(locationManager.getLastKnownLocation(provider));
locationManager.requestLocationUpdates(provider, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
protected void finalize() throws Throwable {
removeLocationListener();
super.finalize();
}
public void removeLocationListener() {
context=null;
locationManager.removeUpdates(GPSTracker.this);
locationManager=null;
location=null;
}
}
你可以注意到我在 MainActivity
销毁时尝试删除更新。但是一次又一次的泄漏仍然没有任何改变。
困惑!
GPSTracker class 保持对 MainActivity 的引用。尝试这样做:
public GPSTracker(Context context) {
this.context = context.getApplicationContext();
getLocation();
}