Android 泄漏com.myapp.main活动实例6.9mb

Android 泄漏com.myapp.main活动实例6.9mb,android,locationmanager,leakcanary,Android,Locationmanager,Leakcanary,嘿,最近我在我的应用程序中添加了泄漏金丝雀,我在我的GPSTracker类中遇到了这个奇怪的泄漏 泄漏报告说: 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$Li

嘿,最近我在我的应用程序中添加了泄漏金丝雀,我在我的
GPSTracker
类中遇到了这个奇怪的泄漏

泄漏报告说:

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$0
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课程是

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;
}

}
您可以注意到,当
main活动
被销毁时,我试图删除更新。但还是没有什么变化,一次又一次地发生同样的泄漏


糊涂了

GPSTracker类正在保留对MainActivity的引用。尝试这样做:

public GPSTracker(Context context) {
    this.context = context.getApplicationContext();
    getLocation();
}