Repository navigation
Release the map view and the controller when the map leaves the composition - #23
Merged
Merged
Conversation
The MapView was destroyed only on a configuration change; a provider switch left it alive, and the SDK thread behind it kept the controller with every overlay. It is now released through MapViewBase's onDestroyMapView, once, whatever the reason the map went away. The lifecycle observer keeps forwarding onResume / onPause only.
The SDK keeps the TileProvider of every tile overlay in a process-wide table, even after the overlay is removed and the MapView destroyed (measured). Our provider reached the renderer, its holder and the MapView, so every Google map the user switched away from stayed in memory. The SDK now gets a provider that only forwards; it is cut from the real one when its overlay is removed, retired or the controller destroyed. Destroying the controller also removes every overlay it added and closes the local tile queues, before the MapView is destroyed.
The state kept its own controller field, set on attach and never cleared, so a state the app keeps held the destroyed controller and every overlay of the old map. It now reads the base's attached controller, which MapViewBase detaches when the map is torn down.
This was referenced Nov 8, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
なぜ
MapView を破棄していたのは設定変更のときだけで、プロバイダを切り替えると MapView が生きたまま残り、SDK のスレッド経由でコントローラとオーバーレイがすべて残っていました。
加えて、Google Maps SDK はタイルオーバーレイの
TileProviderを、オーバーレイを外して MapView を破棄した後もプロセス全体の表に持ち続けます(実測)。こちらの TileProvider はレンダラ、holder、MapView に届くので、切り替えた Google の地図はすべて残っていました。何を変えたか
MapViewBaseのonDestroyMapViewから 1 回だけ破棄します。理由を問わず、地図が外れたときです。ライフサイクル observer は onResume / onPause だけを転送します。DetachableTileProviderを渡します。オーバーレイを外したとき、差し替えたとき、コントローラを破棄したときに本物から切り離します。コントローラの破棄では、追加したオーバーレイをすべて外し、ローカルのタイルキューを閉じてから MapView を破棄します。controllerフィールド(接続時に入れて二度と消さない)をやめ、基底の接続中のコントローラを読むようにしました。MapViewBaseが地図を破棄するときに外すので、アプリが持ち続ける state に古いコントローラとオーバーレイが残りません。残るもの
Google の地図を 1 回作るごとに、SDK のタイル用スレッド 3 本と Executor が増え続けます(SDK 側の問題で、こちらでは止められません)。1 回あたり数 KB 程度で、PSS では増加が見えませんでした。こちらの
DetachableTileProvider(20 B)も 1 個ずつこのプールに握られます。スレッドに残っていた作業用バッファは android-sdk-core のScratchPoolで解消しています。関連 PR(同じ修正の一式)
fix/mapview-teardownは次のリポジトリにまたがります。core → compose → 各プロバイダの順にマージしてください。プロバイダの変更は、core のMapViewState.detachControllerと、compose のMapViewBase(onDestroyMapView = …)/MapViewLifecycleOwnerを使います。検証
タブレット(HA26Z6JL)の example-app で確認しました。区画データ(約 10 万ポリゴン)はポリゴンのタイル描画が要るので、検証には、このブランチのコミットを
feat/polygon-tiling/feat/overlay-on-addedの上に一時的に cherry-pick したブランチ(test/teardown-on-tiling、push していません)を使っています。am dumpheap -gで GC させて、GC 後のヒープを測りました。MapLibreViewController/GoogleMapViewControllerが残り、それぞれ 98,843 件の PolygonEntity を抱えていました。testDebugUnitTestとapiCheckを流しました。新しく足したテストはすべて通っています。失敗は次の 5 つで、どれも develop の時点で同じ理由で落ちています。確認できていないもの: