The Memory Advice API beta is now deprecated, and no longer recommended for use.
Unity ゲーム用 Memory Advice API スタートガイド
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このガイドでは、Unity 用 Memory Advice プラグインを使用して、Memory Advice API を Unity ゲームに統合する方法について説明します。
要件
このプラグインは次の環境で使用できます。
Unity 2019 と Android NDK r19
Unity 2020 と Android NDK r19
Unity 2021 と Android NDK r21
Unity 2022 と Android NDK r23
上記以外のバージョンの Unity と Android NDK を使用している場合、予期しない問題が発生する可能性があります。Unity のインストールで使用する NDK バージョンを確認するには、Unity の Android 環境設定ガイドをご覧ください。
プラグインをダウンロードする
プラグインをダウンロードします。
プラグインをインポートする
このプラグインは、プロジェクトにインポートできる Unity パッケージです。プラグインをインポートするには、[Assets] > [Import Package] > [Custom Package] をクリックし、ダウンロードした .unitypackage
ファイルを選択します。Unity プロジェクトを開いて .unitypackage
ファイルをダブルクリックしてもかまいません。
ライブラリを使用する
このセクションでは、ライブラリの使用方法について説明します。
ライブラリを初期化する
アプリの起動時に 1 回、ライブラリを初期化する必要があります。
そのために、次のコードをプロジェクトに追加します。
void Start()
{
MemoryAdviceErrorCode errorCode = MemoryAdvice.Init();
if(errorCode == MemoryAdviceErrorCode.Ok)
{
Debug.Log("Memory advice init successfully");
}
}
メモリ状態についてポーリングする
任意の間隔でライブラリをポーリングすることにより、アプリのメモリ状態を取得できます。ライブラリをポーリングする必要がある場合は、常に MemoryAdvice_getMemoryState 関数を使用してください。
MemoryState memoryState = MemoryAdvice.GetMemoryState();
switch (memoryState)
{
case MemoryState.Ok:
//The application can safely allocate memory.
break;
case MemoryState.ApproachingLimit:
// The application should minimize memory allocation.
break;
case MemoryState.Critical:
// The application should free memory as soon as possible
// until the memory state changes.
break;
}
ウォッチャーを設定する
ウォッチャーをセットアップして Memory Advice API を登録することもできます。これにより、メモリ状態が上限に近づいた際、またはひっ迫した際に、ウォッチャー関数が呼び出されるようにできます(ただし OK 状態では呼び出されません)。たとえば、次のコードでは、ウォッチャーを作成して 2 秒ごとに Memory Advice API 通知をリクエストします。
MemoryAdviceErrorCode errorCode = MemoryAdvice.RegisterWatcher(2000,
new MemoryWatcherDelegateListener((MemoryState state) =>
{
switch (memoryState)
{
case MemoryState.ApproachingLimit:
// The application should minimize memory allocation.
break;
case MemoryState.Critical:
// The application should free memory as soon as possible
// until the memory state changes.
break;
}
})
);
if(errorCode == MemoryAdviceErrorCode.Ok)
{
Debug.Log("Memory Advice watcher registered successfully");
}
次のステップ
Unity サンプル プロジェクトをダウンロードします。このサンプル プロジェクトでは、メモリの割り当てと解放を行うシンプルな UI を用意し、Memory Advice API を使用してメモリの状態をモニターします。
Memory Advice API の概要で、参考情報および問題とフィードバックについてご確認ください。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-08-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-08-26 UTC。"],[],[],null,["| **Note:** The Memory Advice API Beta is now over and the library is deprecated. [learn more about alternative memory management approaches](/games/optimize/memory-allocation).\n\nThis guide describes how to use the Memory Advice plugin for Unity to\nintegrate the\n[Memory Advice API](/games/sdk/memory-advice/overview) into\nyour Unity game.\n\nRequirements\n\nThe plugin is supported on:\n\n- Unity 2019 with Android NDK r19\n\n- Unity 2020 with Android NDK r19\n\n- Unity 2021 with Android NDK r21\n\n- Unity 2022 with Android NDK r23\n\nYou may face unexpected issues if you are using other versions of Unity and\nthe Android NDK. To find the NDK version used by your Unity installation, see\nthe\n[Android environment setup guide](https://docs.unity3d.com/Manual/android-sdksetup.html)\nfor Unity.\n\nDownload the plugin\n\nDownload [the plugin](https://dl.google.com/developers/android/games/memory_advice/memory-advice-1.0.0-beta01.unitypackage).\n\nImport the plugin\n\nThe plugin is a Unity Package that you can import into your project. To import\nthe plugin, click **Assets \\\u003e Import Package \\\u003e Custom Package** and select the\n`.unitypackage` file you downloaded. You can also double-click the\n`.unitypackage` file after opening your Unity project.\n\nUse the library\n\nThis section describes how to use the library.\n\nInitialize the library\n\nYou need to initialize the library once when the app starts.\nTo do so, add this code to your project: \n\n void Start()\n {\n MemoryAdviceErrorCode errorCode = MemoryAdvice.Init();\n if(errorCode == MemoryAdviceErrorCode.Ok)\n {\n Debug.Log(\"Memory advice init successfully\");\n }\n }\n\nPoll for memory state\n\nYou can retrieve the memory state of your app by polling the library at the\ninterval of your choosing. Use the [MemoryAdvice_getMemoryState](/reference/games/memory-advice/group/memory-advice#memoryadvice_getmemorystate)\nfunction whenever you need to poll the library: \n\n MemoryState memoryState = MemoryAdvice.GetMemoryState();\n switch (memoryState)\n {\n case MemoryState.Ok:\n //The application can safely allocate memory.\n break;\n case MemoryState.ApproachingLimit:\n // The application should minimize memory allocation.\n break;\n case MemoryState.Critical:\n // The application should free memory as soon as possible\n // until the memory state changes.\n break;\n }\n\nSet up a watcher\n\nYou can also set up [a watcher](/reference/games/memory-advice/group/memory-advice#memoryadvice_registerwatcher)\nand register the Memory Advice API, and your watcher function\nwill get called when the state is either approaching the limit or the critical\n[memory state](/reference/games/memory-advice/group/memory-advice#memoryadvice_memorystate)\n(but not for the ok state). For example, the following code creates a watcher\nand requests a Memory Advice API notification every 2 seconds: \n\n MemoryAdviceErrorCode errorCode = MemoryAdvice.RegisterWatcher(2000,\n new MemoryWatcherDelegateListener((MemoryState state) =\u003e\n {\n switch (memoryState)\n {\n case MemoryState.ApproachingLimit:\n // The application should minimize memory allocation.\n break;\n case MemoryState.Critical:\n // The application should free memory as soon as possible\n // until the memory state changes.\n break;\n }\n })\n );\n\n if(errorCode == MemoryAdviceErrorCode.Ok)\n {\n Debug.Log(\"Memory Advice watcher registered successfully\");\n }\n\nWhat's next\n\nYou can download our [Unity sample project](https://dl.google.com/developers/android/games/memory_advice/sample.zip)\nthat provides a simple UI for allocating and freeing memory, and uses\nMemory Advice API to monitor the memory state.\n\nSee the [overview](/games/sdk/memory-advice/overview) for\n[additional resources](/games/sdk/memory-advice/overview#additional_resources)\nand [reporting issues](/games/sdk/memory-advice/overview#issues_and_feedback)."]]