Search Unity

新たなベストプラクティスガイド – Unity におけるメモリ管理

, 6月 27, 2018

エンタープライズサポートでは Unity の機能を様々に組み合わせて多数の事業をサポートさせていただいておりますが、サポートを通じて関わったゲームにはすべてメモリの使用方法に改善の余地がありました。私たちがベストプラクティスガイド「Unity におけるメモリ管理」をまとめた背景はそこにあります。

現場に出向いた際はいつも、まずプロファイリングに最優先で取り組んでいます。CPU に小さいながらも不必要な負担を与えるコーディングパターンや、またはメモリ断片化やアセット重複の原因となる重要な問題など、見つかる問題は様々ですが、いずれにしても早い段階から頻繁にゲームのプロファイリングをするのがアプリケーションを健全な状態に保つ最善の策と言えます。最も大きな成功を収めるチームはプロジェクトのメモリをプロファイリングしているものです。

メモリは非常に希少なリソースです(今日の市場でも 30% を占める、搭載メモリが 1GB 以下のモバイル端末においては特に希少なものとなります)。そのため、自分のメモリがどこで使われていて、また何故そこで使われているのかを知ることは必要不可欠と言えます。 プラットフォーム間でメモリが異なる方法で管理されている場合、メモリが消費されている場所と、メモリが CPU と GPU のパフォーマンスにどのような影響を与えているのかを理解することは極めて重要と言えます。

しかし尻込みする必要はありません。この度、新たなベストプラクティスガイドを作成させていただきました。Unity におけるメモリ管理。このガイドはメモリプロファイリングに利用可能なツールを幅広く紹介しており、それらの効果的な使用方法も詳細に記載しています。Unity マニュアルのベストプラクティスガイド > メモリも参照しつつ、こちらのガイドに記載されたテクニックを用いることで、効率的に問題を見つけ出し、解消することができるでしょう。

これらのガイドを読み、もっと別のベストプラクティスも見てみたくなりましたか?最も新しいベストプラクティスガイドである「Unity におけるメモリ管理の他にも、これまでに私たちがまとめた他のベストプラクティスガイドが多数あり、これらをすべてご覧いただけます。パフォーマンスを取り戻し、プロジェクトに本来の力を発揮させるためのヒントや方法の数々が収められています。ぜひチェックしてみてください。

これらのガイドは定期的に更新されたり、新たな要素が追加されるので時折チェックして新情報を確認してください!

25 コメント

コメントの配信登録

コメント受付を終了しました。

  1. I am getting the exception with the unity lib files that are compiled only at the time we make the android build. the issue is with libunity.so file

  2. Hi, Thank you so much for writing this article. Is it not recommend to write your game logic/timing inside corountine? Coroutine should be only use for system such as loading asset?

    1. David Berger

      6月 29, 2018 5:36 pm

      Essentially this is true. Lond lasting coroutines with a lot of memory allocations is not good. Keep them short and simple. Instead use Update or even better a UpdateManager to handle your game logic. See details here: https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity3.html

      1. Oke. Thanks.

  3. 1.) THANK YOU! I’m dying for these “best practices” kind of articles/informational-tutorials. They are wonderful.

    2.) Q about Unloading Scenes: https://unity3d.com/learn/tutorials/topics/best-practices/native-memory?playlist=30089#Unloading%20Scenes
    This doesn’t happen automatically? (If so, shouldn’t the documentation for LoadScene and LoadSceneAsync clarify?)

    3.) How do we leave feedback for these articles in the future? (Say, once this blog post is no longer being monitored.)

    4.) Have you considered hosting your docs on github like Microsoft does, to facilitate organized feedback and external contributions?

    5.) Thank you again, please keep these kinds of tutorials updated! :)

    1. David Berger

      6月 27, 2018 11:43 pm

      1.) Glad you like it!
      2.) If you look close enough, the documentation does tell you that this is the case, but we want to emphasize it again.
      3.) It’s always possible to reach out to our customer service team at https://unity3d.com/learn/support and tell them about it, but the blog is monitored for sure as well.
      4.) That’s out of the scope of this blog post, but I’m gonna forward the feedback.
      5.) We try to update them as often as possible! :)

      1. So https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.Single.html implicitly calls UnloadScene for all currently loaded scenes? That would make UnloadScene only relevant for scenarios where Additive scene loading is used? (This is what I assumed/expected, but isn’t how I interpreted the tutorial’s section on it.)

        1. David Berger

          6月 28, 2018 12:32 am

          Yes, but it can also be used to unload a scene completely.

      2. “Yes, but it can also be used to unload a scene completely.” — I don’t understand the distinction here. Beyond unloading a additively loaded scene(s), You’re saying I could load a single scene, and then use it to unload the single scene I have loaded, and just sit there scene-less? Is that useful? Or is there some aspect I’m missing? (Thanks for the help, btw!)

        1. David Berger

          7月 2, 2018 7:59 pm

          In theory, you can do that, if you try to clean all the memory and want be 100% sure nothing holds on to it, you could unload a Scene. As you’d end up having no Scene and potential Screen artifacts, we do recommend to load an “empty” Scene instead and load the new content from that “empty” Scene instead.

  4. Mahdi Jeddi

    6月 27, 2018 8:05 pm

    Nice! We just solved some memory problems in our game. The sprites that we use weren’t being compressed because of the non-power of 2 resolutions. The solution was to put them in atlases. Even though most of the images are around the max size of the atlases (4Kx4K) the result was still 4-8x smaller memory footprint.

    1. David Berger

      6月 27, 2018 8:58 pm

      Yes! That’s a great way to gain back disc space and runtime memory. We might talk about atlases in this section in future: https://unity3d.com/learn/tutorials/topics/best-practices/textures#PVRTC

  5. You should mention the existence of Heap Explorer, the ultimate memory profiler, debugger and analyzer for Unity:
    https://forum.unity.com/threads/wip-heap-explorer-memory-profiler-debugger-and-analyzer-for-unity.527949/

    1. Heap explorer looks nice but its not released yet. And the developer is not looking for any more testers at this moment in time.

  6. Excellent resource
    It was not obvious to me for a long time that Unity.Object classes need Destroy to be called, even if there is nothing left in scope using the resource (thus no handle to get a hold of it again), to release them from memory.
    It is not mentioned in the docs around Texture2D but it is mentioned with RenderTextures for example, not sure if I just missed this or the info is not clear enough in the general scripting docs. Or perhaps UnityEngine.Object is easily confused with a standard C# object?!

    1. David Berger

      6月 27, 2018 7:08 pm

      This is true and we should add this to the documentation, as native Asset types, such as Texture2D or RenderTextures have not only managed memory which gets garbage collected, but also a native memory which needs to be freed. This happens automatically when switching scenes synchronously or calling Resources.UnloadUnusedAssets (https://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html) but only if nothing in the Scenes hold onto it anymore, by making it DontDestroyOnLoad (https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html) for example.

    2. Does this apply to Mesh objects as well?

    1. David Berger

      6月 27, 2018 4:41 pm

      Cheers! It’s fixed now and should be working in a couple of minutes when the cache refreshed.

  7. Managed Memory is not accessible currently.. runs into an error.

    1. Ciro Continisio

      6月 27, 2018 3:19 pm

      You mean a link is broken? I just checked all of them, seem to work for me…

      1. After going to “Memory Management in Unity” the first link in there (Managed Memory) runs into an error. The links in here are ok ;)

      2. David Perret

        6月 27, 2018 3:49 pm

        Links to “Assets” and “IL2CPP & Mono” are working but main “Managed Memory” points to https://cms.hq.unity3d.com … instead of unity3d.com and lead to a 404 error ;)

        1. David Berger

          6月 27, 2018 4:38 pm

          Thanks for the info. It’s fixed now and should be working in a couple of minutes when the cache refreshed.