執行個體清理最佳做法
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁說明在不再需要執行個體時,如何按照最佳做法清除執行個體。
導覽工作階段結束時使用 Navigator#clearDestinations
和 Navigator#cleanup
為避免記憶體流失並確保正確清除,請在導覽工作階段完成且不再需要 Navigator 例項後,使用 Navigator#clearDestinations
和 Navigator#cleanup
。當使用者離開地圖,且系統叫用相關活動的 onDestroy()
時,即可執行這項操作。
不再需要 GoogleMap
元素時,請清除這些元素
如果您的應用程式使用 GoogleMap
執行個體提供非導航地圖體驗,請務必在不再需要時清除這個執行個體。舉例來說,呼叫 Polygon#remove
、Polyline#remove
和 Marker#remove
方法,即可分別清除與 GoogleMap
相關聯的 Polygon、Polyline 和 Marker 元素。然後,在不再需要 GoogleMap
例項時,呼叫 GoogleMap#clear
將例項指派給 null
,即可釋出例項。
如要進一步瞭解如何在應用程式中使用 GoogleMap
執行個體,請參閱 GoogleMap
互動最佳做法。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間: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)."]]