I do not know what else I can do to prevent my app from getting killed by Android. I cannot find the spot that leads to accumulate my memory.
In the Foregroundservice I am receiving my latitude/longitude via requestLocationUpdates. Then I am passing both values via BroadcastReceiver to my MainActivity where I add the values to my List polylinePoints and then I draw the polyline.
Other apps which are also tracking your path covered in real-time never get killed and I do not get it why.
Maybe someone can help me?
My app is on Github, too: https://github.com/bernd-roth/Ultrarunning
Here is a part of my code
ForegroundService
Thread that handles notifications
private void initializeWatchdog() {
if (mWatchdogThread == null || !mWatchdogThread.isAlive()) {
mWatchdogRunner = new WatchDogRunner();
mWatchdogThread = new Thread(mWatchdogRunner, "WorkoutWatchdog");
}
if (!mWatchdogThread.isAlive()) {
mWatchdogThread.start();
}
}
send broadcast to MapsActivity (still in ForegroundService)
private void sendBroadcastToMapsActivity(double oldLatitude, double oldLongitude, double currentLatitude, double currentLongitude) {
intent.setAction(SharedPref.STATIC_BROADCAST_ACTION);
bundle.putString("SPEED", String.valueOf(currentSpeed));
bundle.putFloat("DISTANCE", calc);
//old
bundle.putDouble("OLD-LAT", oldLatitude);
bundle.putDouble("OLD-LON", oldLongitude);
//new
bundle.putDouble("CURRENT-LAT", currentLatitude);
bundle.putDouble("CURRENT-LON", currentLongitude);
bundle.putString("SPEED", String.valueOf(currentSpeed));
bundle.putFloat("DISTANCE", calc);
intent.putExtras(bundle);
getApplicationContext().sendBroadcast(intent);
}
MapsActivity
Broadcastreceiver
private class DataBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Timber.d("DataBroadcastReceiver %s", action);
oldLatitude = intent.getExtras().getDouble("OLD-LAT");
oldLongitude = intent.getExtras().getDouble("OLD-LON");
currentLatitude = intent.getExtras().getDouble("CURRENT-LAT");
currentLongitude = intent.getExtras().getDouble("CURRENT-LON");
if(currentLatitude != 0 && currentLongitude != 0) {
speed = intent.getExtras().getString("SPEED");
coveredDistance = intent.getExtras().getFloat("DISTANCE");
oldLatitude = currentLatitude;
oldLongitude = currentLongitude;
polylinePoints.add(new LatLng(oldLatitude, oldLongitude));
createPolypoints(oldLatitude, oldLongitude, polylinePoints);
}
}
}
Here is the method which draws the polyline
private void createPolypoints(double lastLat, double lastLng, List<LatLng> polylinePoints) {
boolean isServiceRunning = isServiceRunning(getString(R.string.serviceName));
if (!isServiceRunning) {
toolbar_title.setText("Distance: 0.0 Km" + "\nSpeed: 0.0");
} else {
if(polyline!=null) {
polyline.remove();
mMap.clear();
}
polyline = mMap.addPolyline(new PolylineOptions().addAll(polylinePoints).color(Color.MAGENTA).jointType(JointType.ROUND).width(15.0f));
toolbar_title.setText("Distance: " + String.format("%.2f", coveredDistance) + "\nSpeed: " + speed);
}
}
And here is photo of my heap dump and if I interpret it right, it seems to be related to my LatLng but adding many of them to a list must not bring the phone to the edge, or?
Thank you very much in advance!

You'd need to debug by taking heap dumps and checking what object is growing at high rates. But a few things you can do to improve your code:
1)Your foreground service should NOT be passing data to the activity and adding it directly to a map. What if your Activity is delete because it isn't in the foreground? This could cause the entire Activity to leak. Instead post it to something like an RxJava subject or a ViewModel ir a Flow, and let the Activity subscribe to that (and make sure it unsubscribes
2)How often are you getting the location? You don't actually want to add a ton of updates to the map. Firstly because that's a lot of objects, and secondly because location is noisy and it will make your map look all over the place. You should only update every few seconds, and only if the change in location is large enough. Reducing the number of points in this way will reduce memory accumulation as well.
3)Consider a cap on the total number of points by dropping old points. Is the data from 30 minutes ago really needed? Or can the number of points saved be reduced? Do you really need that level of accuracy later- do you need 60 updates per minute, or would 1 be enough when looking back on the data later? This depends on what you're doing, but its likely that you can drop 90% of your data points after a few minutes.
4)Are you ever removing old polylines from the map? Or are you adding a new one every update on top of the old ones? That would cause an exponential increase in the number of points on the map, and cause other problems down the line.