+ All Categories
Home > Spiritual > [UniteKorea2013] Memory profiling in Unity

[UniteKorea2013] Memory profiling in Unity

Date post: 11-Jan-2015
Category:
Upload: unity-technologies-korea
View: 14,034 times
Download: 1 times
Share this document with a friend
Description:
유나이트 코리아 2013 발표자료: 메모리 프로파일링 (킴 스틴 리버)
26
Memory profiling in Unity Kim Steen Riber ([email protected] )
Transcript
Page 1: [UniteKorea2013] Memory profiling in Unity

Memory profiling in UnityKim Steen Riber ([email protected])

Page 2: [UniteKorea2013] Memory profiling in Unity

Page 2

Who am I?

• Core developer @ Unity• Released games

• LIMBO• Watchmen• Total overdose

• Unity focus areas• CPU performance• Memory optimizations

10-04-2023

Page 3: [UniteKorea2013] Memory profiling in Unity

Page 3

Optimizing your game

• FPS• CPU usage (Gamecode, Physics, Skinning, Particles, …)• GPU usage (Drawcalls, Shader usage, Image effects,

…)

• 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 detection

10-04-2023

Page 4: [UniteKorea2013] Memory profiling in Unity

Page 4

Performance

• Unity Profiler (Pro Feature)• CPU • GPU• Memory usage

• New Detailed memory view (Unity 4.1)• Memory reference view (Unity 4.2)

10-04-2023

Page 5: [UniteKorea2013] Memory profiling in Unity

Page 5

CPU 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 small scenes

10-04-2023

Page 6: [UniteKorea2013] Memory profiling in Unity

Page 7

Memory Profiler

• Simple and Detailed memory view (Unity 4.1)• Detail view is taken as a snapshot (too expensive for per

frame)• Reference view (Unity 4.2)

• See where an object is referenced – Good for leak hunting

10-04-2023

Page 7: [UniteKorea2013] Memory profiling in Unity

Page 8

Memory Profiler

10-04-2023

• Simple memory view• Unity reserves chunks of memory from the

os• Mono allocates from a reserved heap • GfxMemory is an estimate

Page 8: [UniteKorea2013] Memory profiling in Unity

Page 9

Mono vs. Unity Memory

• Managed - Mono• Script objects• Wrappers for

unity objects• Game objects• Assets• Components• …

• Memory is garbage collected

10-04-2023

• Native - Unity internal• Asset data

• Textures• Meshes• Audio• Animation

• Game objects• Engine internals

• Rendering• Particles• Webstreams• Physics• …..

Page 9: [UniteKorea2013] Memory profiling in Unity

Page10

Mono 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

even though memory is not exhausted• Heap blocks are kept in Mono for later use

• Memory can be given back to the system after a while

10-04-2023

Page 10: [UniteKorea2013] Memory profiling in Unity

Page11

Fragmentation

• Memory will get fragmentet if there is a lot of activity

10-04-2023

Mono: 16K System: 64K

Mono: 32K System: 64K

Mono: 32K System: 64K

Mono: 48K System: 128K

Page 11: [UniteKorea2013] Memory profiling in Unity

Page12

Avoiding Allocations

• Reuse temporary buffers• If buffers for data processing are needed every

frame, 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

the heap

• Don’t use OnGUI• Even empty OnGUI calls are very memory

intensive

10-04-2023

Page 12: [UniteKorea2013] Memory profiling in Unity

Page13

Avoiding Allocations• Use the CPU profiler to identify mono allocations

10-04-2023

Page 13: [UniteKorea2013] Memory profiling in Unity

Page14

Unity Object wrapper

• Some Objects used in scripts have large native backing memory in unity• Memory not freed until Finalizers have run

10-04-2023

WWWDecompression buffer

Compressed file

Decompressed file

Managed Native

Page 14: [UniteKorea2013] Memory profiling in Unity

Page15

Mono 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 internal

memory• Eventually called from finalizer• Manually call Dispose() to

cleanup

10-04-2023

Main thread Finalizer thread

www = null;

new(someclass);//no more heap-> GC.Collect();

www.Dispose();

.....

Page 15: [UniteKorea2013] Memory profiling in Unity

Page16

Garbage collect and Dispose

Demo

10-04-2023

Page 16: [UniteKorea2013] Memory profiling in Unity

Page17

Assetbundle memory usage

• WebStream• Compressed file• Decompression buffers• Uncompressed file

• Assetbundle• Map of objects and offsets in WebStream

• Instantiated objects• Textures, Meshes, etc.

10-04-2023

Page 17: [UniteKorea2013] Memory profiling in Unity

Page18

Assetbundle 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 and never deallocated

10-04-2023

Page 18: [UniteKorea2013] Memory profiling in Unity

Page19

Assetbundle memory usage

• AssetBundle ab = www.assetBundle;• Loads the map of where objects are in the

webstream• Retains the www WebStream

10-04-2023

WebStream

Tex

Tex

Mesh

AssetBundle

Page 19: [UniteKorea2013] Memory profiling in Unity

Page20

Assetbundle memory usage

• AssetBundle ab = www.assetBundle;• Loads the map of where objects are in the

webstream• Retains the www WebStream

10-04-2023

WebStream

Tex

Tex

Mesh

AssetBundle

Loaded objects

Page 20: [UniteKorea2013] Memory profiling in Unity

Page21

Assetbundle 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 the largest Texture or Vertexbuffer (never shrinks)

10-04-2023

Page 21: [UniteKorea2013] Memory profiling in Unity

Page22

Assetbundle 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 the

assetbundle

10-04-2023

Page 22: [UniteKorea2013] Memory profiling in Unity

Page23

Assetbundle 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 remaining references• In Unity 4.1 a reason why a Object is given• In Unity 4.2 specific scripts or Objects referencing a

given object are shown

10-04-2023

Page 23: [UniteKorea2013] Memory profiling in Unity

Page24

Assetbundle memory usage

• For best memory efficiency do one assetbundle at a time

• If more assetbundles are needed at the same time, load one www object into memory at a time, to reduce decompression buffer usage

10-04-2023

Page 24: [UniteKorea2013] Memory profiling in Unity

Page25

Assetbundle memory usage

Demo

10-04-2023

Page 25: [UniteKorea2013] Memory profiling in Unity

Page26

Conclusions

• 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 assets

10-04-2023

Page 26: [UniteKorea2013] Memory profiling in Unity

Page27

Questions?

10-04-2023


Recommended