Instance cleanup best practices

  • Use Navigator#clearDestinations and Navigator#cleanup when navigation sessions are completed to prevent memory leaks.

  • Clean up GoogleMap elements like Polygons, Polylines, and Markers by calling their respective remove methods when they are no longer needed.

  • After a GoogleMap instance is no longer in use, call GoogleMap#clear and assign the instance to null to free up resources.

This page explains best practices for cleaning up instances after they are no longer needed.

Use Navigator#clearDestinations and Navigator#cleanup when navigation sessions are completed

To prevent memory leaks and ensure proper cleanup, use Navigator#clearDestinations and Navigator#cleanup after the navigation session is completed and the Navigator instance is no longer needed. This can be done when the user has navigated away from the map and when onDestroy() of the associated activity is invoked.

Clean up GoogleMap elements when they are no longer needed

If your app uses a GoogleMap instance for non-navigation maps experiences, be sure to clean up this instance when it is no longer needed. For example, clean up the Polygon, Polyline, and Marker elements associated with GoogleMap by calling the Polygon#remove, Polyline#remove, and Marker#remove methods, respectively. Then, to free up the GoogleMap instance after it is not longer needed, call GoogleMap#clear assign the instance to null.

For more information about using a GoogleMap instance in your app, see GoogleMap interaction best practices.