MemoryProfilerはメモリーリーク等、メモリー関連の調査に必要不可欠なツールですが、GUIから手動で行う場合、完全に同じタイミングでCaptureを実行することは出来ません。 UnityMemoryProfilerSupportKunはスクリプトからのCaptureを容易に運用する為のRuntime APIとEditor拡張をセットにしたものです。
- ScriptからMemoryProfilerのSnapshotを実行し、Application.temporaryCachePathへ保存します。
- 端末上に保存されたSnapshotをUnityEditorから取得出来ます。
- Unity2019.4.19f1
- Android
git clone https://github.com/katsumasa/UnityMemoryProfilerSupportKun.git
- Window-> PackageManager
- Addをクリックします
- パッケージを加えるためのオプションが表示されます。
- ドロップダウンから Add package from git URL を選択します。テキストフィールドと Add ボタンが表示されます
- テキストフィールドに https://github.com/katsumasa/UnityMemoryProfilerSupportKun.git を入力します
- Prefabs/UnityMemoryProfilerをSceneに配置して下さい。このPrefabは常に存在している必要があることに注意して下さい。
- アプリケーションをビルドする際、
Development Build
及びAutoConnect Profiler
の両方にチェックを入れた状態でビルドを行って下さい。 - MemoryProfilerのCaptureを行う箇所で下記のAPIを実行して下さい。
UnityMemoryProfilerSupportKunClient.instance.TakeCapture("スナップショットのファイル名");
Capture処理が完了すると、下記の変数がtrue
になります。
UnityMemoryProfilerSupportKunClient.instance.isDone
例えば、シーン切り替え直前・直後に上記メソッドを実行し差分を比較することでメモリーリークの解決の糸口を見つけることが出来る可能性があります。
Window->UnityMemoryProfilerSupportKunWindowでWindowが開きます。
接続先のPlayerを選択します。
端末に保存されたMemoryProfilerのsnapの一覧を取得します。
指定されたsnapを任意のフォルダーにダウンロードします。
指定されたsnapを端末から削除します。
下記の2種類のサンプルを用意しています。
実機側のボタンを押すことでSnapShotを取るシンプルなサンプル。シーンsimple.unty
のみビルドの対象として下さい。
Scene切り替えのタイミングでSnapShotを取るサンプル。下記の3シーンをビルドに含めて下さい。
SceneMain.unity
SceneSub0001.unity
SceneSub0002.unity