【Unite Tokyo 2018】その最適化、本当に最適ですか!? ~正しい最適化を行うためのテクニック~

1.5K Views

May 08, 18

スライド概要

講演者:Valentin Simonov(Unity Technologies)

こんな人におすすめ
・中級・上級の開発者
・最適化のテクニックを学びたい方
・Unityでの最適化ツールの使用方法を習得した経験豊富なプログラマー

受講者が得られる知見
・プロファイリングと最適化を開始する前に知っておきたいプラットフォーム別の問題解決方法
・ 最適化の例
・ Unityのシステムの内部的な仕組み

profile-image

リアルタイム3Dコンテンツを制作・運用するための世界的にリードするプラットフォームである「Unity」の日本国内における販売、サポート、コミュニティ活動、研究開発、教育支援を行っています。ゲーム開発者からアーティスト、建築家、自動車デザイナー、映画製作者など、さまざまなクリエイターがUnityを使い想像力を発揮しています。

シェア

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

Valentin Simonov Field Engineer Unity Technologies val@unity3d.com valyard

2.

Understanding Memory on iOS • Partly applicable to other platforms • But needs testing

3.

No need to take pictures Detailed doc at the end* * yes, you will have to sit through the entire presentation

4.

How much memory does my game take?

5.

- How much memory does my game take? - What kind of memory? • Physical/Resident Memory • Graphics Memory • Virtual Memory • Malloc Heap • Dirty Memory • Native Memory • Mono Heap

6.

Physical Memory (RAM) • The total amount of memory on-chip • You can’t have more* Physical Memory * But Android users can... until it is mysteriously gone

7.

Virtual Memory (VM) • App’s private address space • Divided into Pages (4KB or 16KB) • Pages are mapped to Physical Memory by the OS Allocated Virtual Memory Available Address Space iOS apps don’t work with Physical Memory directly, they work with Virtual Memory Physical Memory •

8.

Resident Memory • An app can allocate a block of memory in Virtual Memory but not use it (”reserve”) • The app has to modify the allocated VM for the OS to map it to PM (”commit”) • Resident Memory is the total amount of Physical Memory used by the app at any moment Resident Memory committed (part of RM) Available Address Space Physical Memory reserved (not part of RM)

9.

Clean Memory and Dirty Memory Dirty Memory — everything else in Resident Memory • Apps can share Clean Memory pages (like, system frameworks) Dirty Available Address Space • Clean Clean Memory — read-only pages of Resident Memory which iOS can remove and reload Physical Memory •

10.

Clean Memory and Dirty Memory App’s binaries, static code segments, • memory-mapped files, • System frameworks and dynamic libraries, etc. Binaries • Dirty Available Address Space Memory allocated for the following data is Clean (iOS can reload this from disk): Physical Memory •

11.

Graphics Memory (VRAM) iOS uses a Unified Memory Architecture — GPU and CPU share Physical Memory • Graphics Driver allocates Virtual Memory for its resources • Most of this is Resident and Dirty Binaries Physical Memory Graphics Memory Dirty Available Address Space •

12.

Malloc Heap A region of VM where the app can allocate memory using malloc and calloc functions • This is where Unity allocates all memory for its needs • The maximum size is unknown, but seems to be 2x Physical Memory Binaries Physical Memory Graphics Memory Malloc Heap Available Address Space •

13.

Swapped (compressed) Memory • But it can compress them and store in some other region of Physical Memory • SCM is a compressed part of the app’s Dirty Memory • The algorithm is unknown, but iOS usually tries to compress as much as it can Clean iOS doesn’t have a page file and can’t dump rarely used Virtual Memory pages to disk Physical Memory • Not Compressed Dirty Memory Compressed Dirty Memory

14.

Native (Unity) Memory Unity is a C++ engine with a .NET Virtual Machine • Native Memory — part of Malloc Heap (VM Region) used for all Unity’s allocations • All asset data is in Native Memory, exposed to C# as lightweight wrappers Binaries Physical Memory Graphics Memory Native Memory Available Address Space •

15.

Native Plugins Native plugins do their own allocations in Malloc Heap • Their code binaries are “Clean” Binaries Physical Memory Graphics Memory Native Plugins Native Memory Available Address Space •

16.

Mono Heap A part of Native Memory allocated for the needs of the .NET Virtual Machine • Contains all managed C# allocations, maintained by Garbage Collector Binaries Physical Memory Graphics Memory Native Plugins Native Memory Mono Heap Available Address Space •

18.

Reality Imagination Mono Heap Managed Objects Unused Mono Heap Native Memory • Mono Heap • It is allocated in blocks to store objects of similar size • There can be allocated, committed but unused blocks • If a block is still unused after 8 Garbage Collection passes, its Physical Memory will be returned to the system (decommitted) Block Allocated but unused block Block Almost empty block Block Allocated but unused block is not a large contiguous region of Native Memory

19.

iOS Memory Management • iOS is a multitasking OS • Each app works with its own Virtual Memory address space mapped to Physical Memory

20.

iOS Memory Management • When the amount of free Physical Memory gets low, iOS starts trying to reduce memory pressure: 1. Removes Clean Memory pages (they can be safely reloaded later) 2. If the app takes too much Dirty Memory, iOS sends memory warnings 3. If the app fails to free resources, iOS terminates it • We don’t know the details about this algorithm • Sometimes an app can allocate more, sometimes less

21.

Minimize the Size of Dirty Memory! Measure the app’s Dirty Memory and see if it grows over time, • Reduce the amount of objects contributing to Dirty Memory, • Note: some data can be compressed better. Binaries Physical Memory Graphics Memory Native Plugins Native Memory Dirty Memory Mono Heap Available Address Space •

22.

Minimize the Size of Dirty Memory! • • Reasonable limits of Dirty Memory: • 180Mb for 512Mb devices, • 360Mb for 1Gb devices, • 1.2Gb for 2Gb devices. ... but, iOS can still kill the app ¯¥_(ツ)_/¯

23.

Tools

24.

Unity Profiler

25.

Unity Profiler | Simple View Unity Profiler • Mono (used) — the size of Mono Heap (the total sum of pink and green blocks) • Mono (total) — the total committed memory for Mono Heap (the total size of pink, green and blue blocks) • GfxDriver — the total size of 2D textures, excluding render targets • FMOD — the total size of memory requested by FMOD (audio) • Other values should be ignored: • Total doesn’t include the size of game binaries, libs, frameworks, native plugins • GfxDriver doesn’t include a lot of other allocations done by the Graphics Driver • The Profiler only sees allocations done by Unity code

26.

Unity Profiler | Detailed View • Shows named objects in Native Memory and which objects reference them • Assets folder shows how much Native Memory assets take Unity Profiler Unity Profiler • Many large sounds • Probably duplicated textures (have identical names)

27.

MemoryProfiler

28.

MemoryProfiler | BitBucket • Shows a combination of: • Assets (Virtual Memory), • Assets (GPU) • Managed objects (Mono), • Object references • Easy to see relative sizes https://bitbucket.org/Unity-Technologies/memoryprofiler MemoryProfiler

29.

MemoryProfiler | BitBucket MemoryProfiler Array of strings An object in Mono Heap with its content listed https://bitbucket.org/Unity-Technologies/memoryprofiler

30.

MemoryProfiler Extension

31.

MemoryProfiler Extension | Github Block 1 Block 2 Block 3 • We can see Mono Heap • Total size: 256KB + 256KB + 128KB = 640KB • Used: 86.5KB. MemoryProfiler Ext https://github.com/robertoardila/support-unity-memoryprofiler

32.

Xcode Debug View • • • • Available in Xcode Debug Navigator view Shows if the app is doing OK memory wise Seems to be approximately Dirty Memory + Swapped Memory For detailed profiling should use Instruments

33.

VM Tracker Instrument

34.

VM Tracker

35.

VM Tracker (1) Total for the app (d) (e) (c) (a) (2) Binaries and libs (a) 1. Total memory consumption: a. The app has allocated 1GB of VM b. It has committed 351MB (186+165) c. But iOS has swapped 165MB d. Now it is 186MB Physical Memory e. 118MB of which is Dirty Memory 2. (b) Binaries, libs and frameworks a. They take Resident Memory b. But they are not Dirty (i.e., Clean)

36.

VM Tracker (3) Graphics Driver 1. 2. 3. 4. *All* — all allocations *Dirty* — all Dirty allocations IOKit — Graphics Driver VM_ALLOCATE — Mono allocations + Heap (4) Mono Heap 5. 6. 7. 8. MALLOC_* — Malloc Heap __TEXT + __LINKEDIT — app and lib binaries __DATA — writable executable data Perf. tool data — Instruments overhead

37.

Allocations Instrument

38.

Allocations Instrument Allocations Loading Thread A Scene is loading Total Allocated Memory FMOD Initializes Unity Allocates Memory for the Audio

39.

Allocations Instrument Allocations An Asset Bundle is being loaded A Class metadata is initialized Allocations

40.

Example | Parsing JSON 1. Load and parse a 350KB JSON file 2. GC.Collect() 3. 8x GC.Collect() iPhone 6, iOS 11.3 Unity 2018.1

41.

Step 0: Initial State Different tools showing the same data: 1. Unity Profiler shows 0.5MB committed but 364KB used 2. VM Tracker shows 1.25MB of allocations (in 3 blocks), 0.5MB of which is Mono Heap 3. Allocations Instrument shows each separate allocation (note the Category) Unity Profiler VM Tracker Allocations

42.

Step 0: Initial State Allocations Instrument shows call stack for all allocations: 1. The first 4 allocations were done during IL2CPP initialization 2. The 5th allocation was done when creating a managed object Allocations (2) (1)

43.

Step 0: Initial State • • • Two blocks of Mono Heap Total size of Managed objects: 58KB Notice the fragmentation Unity Profiler Block 1 Block 2 MemoryProfiler Ext

44.

Step 1: Parsing JSON 1. 2. 3. 4. Notice how expensive the operation was Total memory allocated during the process: 5.6MB But Mono Heap size hasn’t increased that much Because GC has run a few times to reclaim it Unity Profiler (2) Total Memory Allocated (1) CPU Time (4) Garbage Collector (3) Mono Heap Size Unity Profiler

45.

Step 1: Parsing JSON • • • • Allocations Instrument shows new allocations The same allocated blocks can be seen in VM Tracker Notice that allocations (2) and (3) are in the same block Mono Heap takes ~4MB of Virtual Memory, all of it is Dirty Allocations (3) (1) (1) (4) (2) (2+3) (4) VM Tracker

46.

Step 1: Parsing JSON Look at the sizes of the new allocs: • 1.75MB ~= 2 x 960KB • 960KB ~= 2 x 528KB • 528KB ~= 2 x 256KB • 256KB StringBuilder is expanding its reserved memory Allocations

47.

MemoryProfiler Ext Mono Heap Committed: 4.0MB Heap Size: 3.3MB Used: 1.0MB

48.

Step 2: GC.Collect() 1. 2. 3. 4. Mono Heap size, was: 3.0MB, now: 2.0MB Allocated Mono memory — no change Virtual Machine still has the same amount of committed memory Some of it is compressed Unity Profiler (1) Mono Heap Size (2) Mono (3) Mono (4) Compressed VM Tracker

49.

Step 3: GC.Collect() x8 1. 2. 8x GC.Collect() calls is expensive Unity Profiler shows that the reserved Mono Heap size has decreased Unity Profiler (1) CPU time spent (2) Mono Reserved Unity Profiler

50.

Step 3: GC.Collect() x8 1. 2. Two committed blocks were released They still reserve Virtual Memory but don’t contribute to Resident Memory Allocations (1) Released blocks (2) Blocks are not Resident VM Tracker

51.

Understanding Memory on iOS http://bit.ly/ios-memory Detailed document with much more info. Valentin Simonov Field Engineer Unity Technologies val@unity3d.com valyard