泄漏com.myapp.MainActivity实例6.9mb

时间:2017-02-07 12:28:59

标签: 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$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;
}

}

你可以注意到我在MainActivity被破坏时试图删除更新。但是一次又一次地发生同样的泄漏仍然没有改变。

困惑!

1 个答案:

答案 0 :(得分:1)

GPSTracker类保留对MainActivity的引用。试着这样做:

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