インスタンスのクリーンアップのベスト プラクティス
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このページでは、インスタンスが不要になった後にインスタンスをクリーンアップするためのベスト プラクティスについて説明します。
ナビゲーション セッションが完了したら Navigator#clearDestinations
と Navigator#cleanup
を使用する
メモリリークを防ぎ、適切なクリーンアップを確実に行うには、ナビゲーション セッションが完了し、Navigator インスタンスが不要になったら、Navigator#clearDestinations
と Navigator#cleanup
を使用します。これは、ユーザーが地図から移動したとき、および関連付けられたアクティビティの onDestroy()
が呼び出されたときに実行できます。
不要になった GoogleMap
要素をクリーンアップする
アプリでナビゲーション以外の地図エクスペリエンスに GoogleMap
インスタンスを使用する場合は、そのインスタンスが不要になった時点で必ずクリーンアップしてください。たとえば、GoogleMap
に関連付けられた Polygon、Polyline、Marker の各要素をクリーンアップするには、それぞれ Polygon#remove
、Polyline#remove
、Marker#remove
の各メソッドを呼び出します。次に、不要になった GoogleMap
インスタンスを解放するには、GoogleMap#clear
を呼び出してインスタンスを null
に割り当てます。
アプリで GoogleMap
インスタンスを使用する方法について詳しくは、GoogleMap
のインタラクションに関するベスト プラクティスをご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-04 UTC。
[null,null,["最終更新日 2025-09-04 UTC。"],[[["\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)."]]