实例清理最佳实践
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页介绍了在不再需要实例后清理实例的最佳实践。
在导航会话完成后使用 Navigator#clearDestinations
和 Navigator#cleanup
为防止内存泄漏并确保正确清理,请在导航会话完成后且不再需要 Navigator 实例时使用 Navigator#clearDestinations
和 Navigator#cleanup
。当用户离开地图且调用关联 activity 的 onDestroy()
时,可以执行此操作。
在不再需要 GoogleMap
元素时清理它们
如果您的应用使用 GoogleMap
实例来提供非导航地图体验,请务必在不再需要该实例时将其清理干净。例如,通过分别调用 Polygon#remove
、Polyline#remove
和 Marker#remove
方法,清理与 GoogleMap
关联的 Polygon、Polyline 和 Marker 元素。然后,在不再需要 GoogleMap
实例后,调用 GoogleMap#clear
将该实例分配给 null
,以释放该实例。
如需详细了解如何在应用中使用 GoogleMap
实例,请参阅 GoogleMap
互动最佳实践。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eUse \u003ccode\u003eNavigator#clearDestinations\u003c/code\u003e and \u003ccode\u003eNavigator#cleanup\u003c/code\u003e when navigation sessions are completed to prevent memory leaks.\u003c/p\u003e\n"],["\u003cp\u003eClean up \u003ccode\u003eGoogleMap\u003c/code\u003e elements like Polygons, Polylines, and Markers by calling their respective \u003ccode\u003eremove\u003c/code\u003e methods when they are no longer needed.\u003c/p\u003e\n"],["\u003cp\u003eAfter a \u003ccode\u003eGoogleMap\u003c/code\u003e instance is no longer in use, call \u003ccode\u003eGoogleMap#clear\u003c/code\u003e and assign the instance to \u003ccode\u003enull\u003c/code\u003e to free up resources.\u003c/p\u003e\n"]]],[],null,["# Instance cleanup best practices\n\nThis page explains best practices for cleaning up instances after they are no\nlonger needed.\n\nUse `Navigator#clearDestinations` and `Navigator#cleanup` when navigation sessions are completed\n------------------------------------------------------------------------------------------------\n\nTo prevent memory leaks and ensure proper cleanup, use\n`Navigator#clearDestinations` and `Navigator#cleanup` after the navigation\nsession is completed and the Navigator instance is no longer needed. This can be\ndone when the user has navigated away from the map and when `onDestroy()` of the\nassociated activity is invoked.\n\nClean up `GoogleMap` elements when they are no longer needed\n------------------------------------------------------------\n\nIf your app uses a `GoogleMap` instance for non-navigation maps experiences, be\nsure to clean up this instance when it is no longer needed. For example, clean\nup the Polygon, Polyline, and Marker elements associated with `GoogleMap` by\ncalling the `Polygon#remove`, `Polyline#remove`, and `Marker#remove` methods,\nrespectively. Then, to free up the `GoogleMap` instance after it is not longer\nneeded, call `GoogleMap#clear` assign the instance to `null`.\n\nFor more information about using a `GoogleMap` instance in your app, see\n[`GoogleMap` interaction best\npractices](/maps/documentation/navigation/android-sdk/googlemap-interactions-best-practices)."]]