Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

[UniteKorea2013] Memory profiling in Unity

25.133 Aufrufe

Veröffentlicht am

유나이트 코리아 2013 발표자료: 메모리 프로파일링 (킴 스틴 리버)

Veröffentlicht in: Seele & Geist, Technologie
  • Als Erste(r) kommentieren

[UniteKorea2013] Memory profiling in Unity

  1. 1. Memory profiling in UnityKim Steen Riber (kim@unity3d.com)
  2. 2. PageWho am I?• Core developer @ Unity• Released games• LIMBO• Watchmen• Total overdose• Unity focus areas• CPU performance• Memory optimizations09-05-2013 2
  3. 3. PageOptimizing your game• FPS• CPU usage(Gamecode, Physics, Skinning, Particles, …)• GPU usage (Drawcalls, Shader usage, Imageeffects, …)• Hickups• Spikes in framerate caused by heavy tasks (e.g.GC.Collect)• Physics world rebuild due to moved static colliders• Memory• Maintaining small runtime memory on device• Avoid GC Hickups by reducing memory activity• Leak detection09-05-2013 3
  4. 4. PagePerformance• Unity Profiler (Pro Feature)• CPU• GPU• Memory usage• New Detailed memory view (Unity 4.1)• Memory reference view (Unity 4.2)09-05-2013 4
  5. 5. PageCPU Profiler• Cpu time consumption for methods• Mono memory activity• Remote Profiling of you game running on target device• Deep profile (editor only)• Detailed view, but large overhead, only usable for very smallscenes09-05-2013 5
  6. 6. PageMemory Profiler• Simple and Detailed memory view (Unity 4.1)• Detail view is taken as a snapshot (too expensive for perframe)• Reference view (Unity 4.2)• See where an object is referenced – Good for leak hunting09-05-2013 7
  7. 7. PageMemory Profiler09-05-2013 8• Simple memory view• Unity reserves chunks of memory from the os• Mono allocates from a reserved heap• GfxMemory is an estimate
  8. 8. PageMono vs. Unity Memory• Managed - Mono• Script objects• Wrappers for unityobjects• Game objects• Assets• Components• …• Memory is garbagecollected09-05-2013 9• Native - Unity internal• Asset data• Textures• Meshes• Audio• Animation• Game objects• Engine internals• Rendering• Particles• Webstreams• Physics• …..
  9. 9. PageMono Memory Internals• Allocates system heap blocks for allocations• Garbage collector cleans up• Will allocate new heap blocks when needed• Fragmentation can cause new heap blocks eventhough memory is not exhausted• Heap blocks are kept in Mono for later use• Memory can be given back to the system after a while09-05-2013 10
  10. 10. PageFragmentation• Memory will get fragmentet if there is a lot ofactivity09-05-2013 11Mono: 16K System:64KMono: 32K System:64KMono: 32K System:64KMono: 48K System:128K
  11. 11. PageAvoiding Allocations• Reuse temporary buffers• If buffers for data processing are needed everyframe, allocate the buffer once and reuse• Allocate pools of reusable objects• Create freelists for objects that are needed often• Use structs instead of classes• Structs are placed on the stack, while classes uses theheap• Don’t use OnGUI• Even empty OnGUI calls are very memory intensive09-05-2013 12
  12. 12. PageAvoiding Allocations• Use the CPU profiler to identify mono allocations09-05-2013 13
  13. 13. PageUnity Object wrapper• Some Objects used in scripts have large nativebacking memory in unity• Memory not freed until Finalizers have run09-05-2013 14WWWDecompression bufferCompressed fileDecompressed fileManaged Native
  14. 14. PageMono Garbage Collection• Object goes out of scope• GC.Collect runs when• Mono exhausts the heap space• Or user calls System.GC.Collect()• Finalizers• Run on a separate thread• Unity native memory• Dispose() cleans up internalmemory• Eventually called from finalizer• Manually call Dispose() to cleanup09-05-2013 15Main thread Finalizer threadwww = null;new(someclass);//no more heap-> GC.Collect();www.Dispose();.....
  15. 15. PageGarbage collect and DisposeDemo09-05-2013 16
  16. 16. PageAssetbundle memory usage• WebStream• Compressed file• Decompression buffers• Uncompressed file• Assetbundle• Map of objects and offsets in WebStream• Instantiated objects• Textures, Meshes, etc.09-05-2013 17
  17. 17. PageAssetbundle memory usage• WWW www = new WWW(assetbundle_url);• Loads the compressed file into memory• Constructs decompression buffers (8MB per file)• Decompresses the file into memory• Deallocates the decompression buffers• One decompression buffer of 8MB is reused andnever deallocated09-05-2013 18
  18. 18. PageAssetbundle memory usage• AssetBundle ab = www.assetBundle;• Loads the map of where objects are in the webstream• Retains the www WebStream09-05-2013 19WebStreamTexTexMeshAssetBundle
  19. 19. PageAssetbundle memory usage• AssetBundle ab = www.assetBundle;• Loads the map of where objects are in the webstream• Retains the www WebStream09-05-2013 20WebStreamTexTexMeshAssetBundleLoaded objects
  20. 20. PageAssetbundle memory usage• Texture2D tex =ab.Load(“MyTex", typeof(Texture2D));• Instantiates a texture from the assetbundle• Uploads the texture to the GPU• On editor a system copy is retained (2x memory)• Transferbuffer for the RenderThread will grow to fit thelargest Texture or Vertexbuffer (never shrinks)09-05-2013 21
  21. 21. PageAssetbundle memory usage• Deleting the WebStream• www.Dispose(); // will count down the retain count• If not called, finalizers will clean the memory eventually• Deleting the Assetbundle• ab.Unload(false);• Unloads the map and counts down the www retain count• ab.Unload(true);• Will also force unload all assets created from theassetbundle09-05-2013 22
  22. 22. PageAssetbundle memory usage• Removing the loaded objects from memory• ab.Unload(true)• Force unload objects loaded by assetbundle• UnloadUnusedAssets()• Unloads when there are no more references to the object• Use the memory profiler to find remainingreferences• In Unity 4.1 a reason why a Object is given• In Unity 4.2 specific scripts or Objects referencing agiven object are shown09-05-2013 23
  23. 23. PageAssetbundle memory usage• For best memory efficiency do one assetbundle ata time• If more assetbundles are needed at the sametime, load one www object into memory at a time, toreduce decompression buffer usage09-05-2013 24
  24. 24. PageAssetbundle memory usageDemo09-05-2013 25
  25. 25. PageConclusions• Avoid memory activity• Use the memory profiler to monitor memory usage• Load one WWW object at a time• Use Dispose() on objects that derive from IDisposable• Specially if they have a large native memory footprint• Use UnloadUnusedAssets() to clean up assets09-05-2013 26
  26. 26. PageQuestions?09-05-2013 27

×