SlideShare a Scribd company logo
Optimizing mobile applications - Ian Dundore, Mark Harkness
Optimizing Mobile Applications
Introduction
Us
Mark Ian
About This Talk
• Getting Good Data
• General Best Practices
• Common Problems & Solutions
• Memory Usage
• CPU Performance
Profiling
Use The Best Tools
• iOS: Instruments
• Android: VTune, Snapdragon Profiler
• Unity Editor
• Timeline
• 5.3: Memory Profiler
Instruments!
• Free, included with XCode
• Works perfectly with Unity IL2CPP builds
• Best tool for mobile CPU profiling
• Best tool for startup time profiling
Instruments! (2)
• Instructions on how to run it:
• http://blogs.unity3d.com/2016/02/01/profiling-with-
instruments/
Instruments CPU Profiler: Startup Time
Instruments CPU Profiler: Runtime
Instruments: Reading the PlayerLoop
• BaseBehaviourManager::CommonUpdate
• Update, FixedUpdate and LateUpdate callbacks
• PhysicsManager::FixedUpdate
• PhysX simulation, OnCollision* and OnTrigger* callbacks
• Physics2DManager::FixedUpdate if using 2D physics
• DelayedCallManager::Update
• Resumed coroutines
Instruments: Reading the PlayerLoop (2)
• PlayerRender
• Draw calls, batching, OnWillRender & image effect callbacks
• UI::CanvasManager::WillRenderCanvases
• UI canvas rebatching, text mesh generation, etc.
• EnlightenRuntimeManager::Update
• Enlighten, precomputed realtime GI, reflection probes
Instruments: Examining a Callback
Instruments: Examining a Coroutine
Instruments: Coroutines (2)
• Coroutine execution is split between two places:
• The method where the coroutine was started.
• i.e. where StartCoroutine() was called
• DelayedCallManager
• StartCoroutine runs all code until the first yield
• DelayedCalledManager runs the rest
Instruments: Summarizing Distributed Costs
• Enter method name into “Search” box
• Suggested searches:
• “::Box”, “Box(“ and “_Box”
• “String_”
Instruments: Identifying Asset Loads
5.3 Memory Profiler
5.3 Memory Profiler
• Download code from Bitbucket
• http://bitbucket.org/Unity-Technologies/MemoryProfiler/
• Drop into an Editor folder inside Assets
• In Unity Editor: Window > MemoryProfilerWindow
• Connect Unity Profiler via Profiler Window
• Click “Take Snapshot”
5.3 Memory Profiler: Duplicated Textures
Examine
these
5.3 Memory Profiler: Duplicated Textures
Same Texture, Different Instances
Same
Different
5.3 Memory Profiler
Assets
Asset Auditing: Preventing Mistakes
• Developers are people (arguably)
• People make mistakes
• Mistakes cost dev time
• Write tools to prevent common, costly errors
Asset Auditing: Common Errors
• Insane texture sizes
• Asset compression
• Improper Avatar/Rig settings
• Different rules for different parts of project
Asset Auditing: HOWTO
public class AssetAuditExample : AssetPostprocessor {
public void OnPreprocessTexture() {
// …
}
public void OnPreprocessModel() {
// …
}
}
Asset Auditing: HOWTO (2)
• AssetPostprocessor classes receive callbacks on import
• Implement OnPreprocess* methods
• Apply your project’s rules to assetImporter instance
Asset Auditing: HOWTO (3)
public class ReadOnlyModelPostprocessor : AssetPostprocessor {
public void OnPreprocessModel() {
ModelImporter modelImporter = (ModelImporter)assetImporter;
if(modelImporter.isReadable) {
modelImporter.isReadable = false;
modelImporter.SaveAndReimport();
}
}
}
Common Rules: Textures
• Make sure Read/Write is disabled
• Disable mipmaps if possible
• Make sure textures are Compressed
• Ensure sizes aren’t too large
• 2048x2048 or 1024x1024 for UI atlases
• 512x512 or smaller for model textures
Common Rules: Models
• Make sure Read/Write is disabled
• Disable rig on non-character models
• Copy avatars for characters with shared rigs
• Enable mesh compression
Common Rules: Audio
• MP3 compression on iOS
• Vorbis compression on Android
• “Force Mono” for mobile games
• Set bitrate as low as possible
Memory in Unity
Managed Memory: How It Works
Texture #1Texture #2Audio ClipMesh
int[] Arraystringstringstring
Heap contains objects allocated for Assets and Scripts
Managed Memory: How It Works
Texture #1Texture #2Audio ClipMesh
int[] Arraystringstringstring
int[] someNumbers = new int[2048];
More memory is allocated when requested by code.
int[] Array
Managed Memory: How It Works
Texture #1Texture #2Audio ClipMesh
int[] Arraystringstringstring
GC.Collect();
Garbage collector runs periodically, looks for unused
objects.
Unused objects are deleted.
int[] Array
Managed Memory: How It Works
Texture #1Texture #2Audio ClipMesh
int[] Arraystring
Holes are not filled. This is Memory Fragmentation.
int[] Array
Managed Memory: How It Works
Texture #1Texture #2Audio ClipMesh
int[] Arraystring
When there isn’t enough space for new objects…
int[] Array
int[] Array
TOO SMALL
Managed Memory: How It Works
Texture #1Texture #2Audio ClipMesh
int[] Arraystring
The heap expands.
int[] Array
int[] Array
Managed Memory: Problems
• In Unity, the heap only expands. It never shrinks.
• iOS & Android still care about reserved pages.
• Detail: Unused blocks of the heap remain reserved, but
are paged out of the working set.
Managed Memory: Problems (2)
• Temporary memory allocations are really bad.
• 1 kilobyte of allocation per frame, 60 FPS
• = 60 kilobytes per second of allocation
• If GC runs once per minute (BAD for framerate)…
• 3600 kilobytes of memory needed!
Tracking Managed Memory Allocations
Use Unity Profiler.
Sort by “GC Alloc” column.
When user can interact with app, stay as
close to zero as possible.
(During loading, allocations aren’t as bad.)
Memory Conservation
• Reuse Collections (Lists, HashSets, etc.)
• Avoid string concatenation
• Reuse StringBuilders to compose strings
• Avoid closures & anonymous methods
Memory Conservation: Boxing
• Happens when passing a value type as a reference
type.
• Value is temporarily allocated on the heap.
• Example:
int x = 1;
object y = new object();
y.Equals(x); // Boxes “x” onto the heap
Memory Conservation: Boxing (2)
• Also happens when using enums as Dictionary keys
• Example:
enum MyEnum { a, b, c };
var myDictionary = new Dictionary<MyEnum, object>();
myDictionary.Add(MyEnum.a, new object()); // Boxes value “MyEnum.a”
• Workaround: Implement IEqualityComparer class
Memory Conservation: Foreach
• Allocates a Enumerator when loop begins
• Specific to Unity’s version of Mono
• Just don’t use it.
Memory Conservation: Unity APIs
• If a Unity API returns an array, it allocates a new copy.
• Every time it is accessed.
• Even if the values do not change.
Memory Conservation: Unity APIs (2)
• This code allocates many Touch[] arrays.
for ( int i = 0; i < Input.touches.Length; i++ )
{
Touch touch = Input.touches[i];
// …
}
Memory Conservation: Unity APIs (3)
• This code allocates only one copy of the Touch[] array.
Touch[] touches = Input.touches;
for ( int i = 0; i < touches.Length; i++ )
{
Touch touch = touches[i];
// …
}
CPU Performance Tips: Loading
XML, JSON & other text formats
• Parsing text is very slow.
• Avoid parsers built on Reflection — extremely slow.
• In 5.3: Use Unity’s JsonUtility class!
• Three strategies to speed up data parsing.
XML/JSON: Reduce Workload
• Strategy 1: Don’t parse text.
• Bake text data to binary
• Use ScriptableObject
• Useful for data that does not change often.
• e.g. Game design parameters
XML/JSON: Reduce Workload (2)
• Strategy 2: Do less work.
• Split data into smaller chunks.
• Parse only the parts that are needed.
• Cache parsing results for later reuse.
XML/JSON: Reduce Workload (3)
• Strategy 3: Threads.
• Pure C# types only.
• No Unity Assets (ScriptableObjects, Textures, etc.)
• Be VERY careful.
Large Prefabs
• All GameObjects & Components in a prefab are
serialized into the prefab’s data file.
• Includes all settings on all Components.
• 2 identical GameObjects in a prefab = 2 copies of data
Large Prefabs (2)
• For very large prefabs, split into smaller parts
• Use Awake callbacks to instantiate duplicated parts
The Resources Folder
• An index of Resources is loaded at startup.
• Cannot be avoided or deferred.
• Solution: Move assets from Resources to Asset
Bundles.
CPU Performance Tips: Runtime
Easy: Material/Animator/Shader Properties
• Never address Material, Shader, or Animator properties
by name.
• Internally, hashes the property name into an integer.
• Don’t do this:
material.SetColor(“_Color”, Color.white);
animator.SetTrigger(“attack”);
Cached Material/Animator/Shader
Properties
• Do hashing at startup, cache results and reuse them.
static readonly int material_Color = Shader.PropertyToID(“_Color”);
static readonly int anim_Attack = Animator.StringToHash(“attack”);
material.SetColor(material_Color, Color.white);
animator.SetTrigger(anim_Attack);
Boxing & String Manipulation
• These are so expensive we had to mention them twice.
• Slow: RegExps, String.StartsWith, String.EndsWith
• Instruments:
• Search for “::Box” and “_Box”
• Search for “String_”
Instruments: Identifying Boxing
Unity UI
Canvases, Draw Calls and Batching
• Canvases “rebuild” their batches to reduce draw calls
• Rebuilds are very expensive.
• A canvas rebuilds if any Drawable component changes.
• Any visual change will force a canvas to rebuild.
• Drawable = anything visible on a canvas.
Canvases, Draw Calls and Batching
• Cost of a rebuild based on number of elements.
• Includes children.
• Frequent changes + lots of UI = lots of CPU usage!
Reducing Batching Cost
• Reduce number of Drawables.
• Merge sprites, merge text objects
• Split up Canvases
• Balance cost of draw calls and cost of batching.
Splitting Canvases
• Can nest a canvas within another canvas
• Nested canvases isolate their children from rebuilds.
• Guideline: Move elements that change every frame onto
separate canvases from static elements.
Splitting Canvases: Example
Splitting Canvases: Example
Images
Splitting Canvases: Example
Change Every Frame Never Change
Splitting Canvases: Example
Dynamic Canvas Background Canvas
Trampolines
Remember…
• Profile before optimizing.
• Apply these techniques only when needed.
How Unity Invokes Callbacks
• Internally, C++ Linked List of Components with callbacks
• Update, LateUpdate, etc.
• Iterate over the Linked List and invoke each callback.
• Small overhead when invoking scripts
How Unity Invokes Callbacks
• If number of callbacks becomes very large, overhead
can become significant.
• Thousands of callbacks: 10-20% of Update CPU time
Replacing Callbacks
• Remove Update, LateUpdate, etc.
• Make a GameObject with a MonoBehaviour
• Implement Update, LateUpdate, and other callbacks on this
object
• All other code: Subscribe to needed callbacks
Replacing Callbacks: UpdateManager
object
public class UpdateManager : MonoBehaviour {
public static UpdateManager Instance { get; set; }
void Awake() { Instance = this; }
public UnityEvent OnUpdate = new UnityEvent();
void Update() {
OnUpdate.Invoke();
}
}
Replacing Callbacks: Advantages
• Eliminates native-to-managed trampoline overhead.
• Objects can intelligently unsubscribe.
• Don’t need to return out of Update callbacks!
• Works well with pooled objects.
Optimizing mobile applications - Ian Dundore, Mark Harkness

More Related Content

What's hot (17)

PDF
유니티 그래픽 최적화, 어디까지 해봤니 (Optimizing Unity Graphics) NDC15 Ver.
ozlael ozlael
 
PDF
【Unite Tokyo 2018】“100 Must-see Assets for 2018” by Virtual YouTuber, Cyber G...
Unity Technologies Japan K.K.
 
PDF
Introduction to the Unreal Development Kit
Nick Pruehs
 
PDF
Developing applications and games in Unity engine - Matej Jariabka, Rudolf Ka...
gamifi.cc
 
PPTX
Intro to GPGPU Programming with Cuda
Rob Gillen
 
PDF
【Unite Tokyo 2018】その最適化、本当に最適ですか!? ~正しい最適化を行うためのテクニック~
Unity Technologies Japan K.K.
 
PDF
Building Multiplayer Games (w/ Unity)
Noam Gat
 
PDF
Cuda tutorial
Mahesh Khadatare
 
PPTX
NVIDIA Gameworks, Libraries and Tools
DevGAMM Conference
 
PDF
그래픽 최적화로 가...가버렷! (부제: 배치! 배칭을 보자!) , Batch! Let's take a look at Batching! -...
ozlael ozlael
 
PPT
Vpu technology &gpgpu computing
Arka Ghosh
 
PDF
UIImageView vs Metal #tryswiftconf
Shuichi Tsutsumi
 
PPTX
Intro to GPGPU with CUDA (DevLink)
Rob Gillen
 
PDF
School For Games 2015 - Unity Engine Basics
Nick Pruehs
 
PPT
Game programming with Groovy
James Williams
 
PPT
CUDA
Rachel Miller
 
PDF
GPU: Understanding CUDA
Joaquín Aparicio Ramos
 
유니티 그래픽 최적화, 어디까지 해봤니 (Optimizing Unity Graphics) NDC15 Ver.
ozlael ozlael
 
【Unite Tokyo 2018】“100 Must-see Assets for 2018” by Virtual YouTuber, Cyber G...
Unity Technologies Japan K.K.
 
Introduction to the Unreal Development Kit
Nick Pruehs
 
Developing applications and games in Unity engine - Matej Jariabka, Rudolf Ka...
gamifi.cc
 
Intro to GPGPU Programming with Cuda
Rob Gillen
 
【Unite Tokyo 2018】その最適化、本当に最適ですか!? ~正しい最適化を行うためのテクニック~
Unity Technologies Japan K.K.
 
Building Multiplayer Games (w/ Unity)
Noam Gat
 
Cuda tutorial
Mahesh Khadatare
 
NVIDIA Gameworks, Libraries and Tools
DevGAMM Conference
 
그래픽 최적화로 가...가버렷! (부제: 배치! 배칭을 보자!) , Batch! Let's take a look at Batching! -...
ozlael ozlael
 
Vpu technology &gpgpu computing
Arka Ghosh
 
UIImageView vs Metal #tryswiftconf
Shuichi Tsutsumi
 
Intro to GPGPU with CUDA (DevLink)
Rob Gillen
 
School For Games 2015 - Unity Engine Basics
Nick Pruehs
 
Game programming with Groovy
James Williams
 
GPU: Understanding CUDA
Joaquín Aparicio Ramos
 

Viewers also liked (20)

PDF
뭣이 중헌디? 성능 프로파일링도 모름서 - 유니티 성능 프로파일링 가이드 (IGC16)
ozlael ozlael
 
PPTX
Shadow gunのサンプルから学べるモバイル最適化
Katsutoshi Makino
 
PPTX
Unity & VR (Unity Roadshow 2016)
ozlael ozlael
 
PPTX
Filmic Tonemapping for Real-time Rendering - Siggraph 2010 Color Course
hpduiker
 
PDF
中級グラフィックス入門~シャドウマッピング総まとめ~
ProjectAsura
 
PDF
CEDEC 2016 Metal と Vulkan を用いた水彩画レンダリング技法の紹介
Drecom Co., Ltd.
 
PPTX
Weighted Blended Order Independent Transparency
zokweiron
 
PDF
Aclt1
zokweiron
 
PPTX
声の実体化体験 - HTML5でつくるデジタルインスタレーション -
Yamato Honda
 
PPTX
レイトレ合宿3!!! 5分間アピールプレゼン―Pocol
ProjectAsura
 
PDF
シェーダ体系の話
fumoto kazuhiro
 
PPTX
Unity * スマートフォン開発で学んだこと
Katsutoshi Makino
 
PPTX
レイトレ合宿2!! 3分間アピールプレゼン―Pocol
ProjectAsura
 
PDF
CEDEC 2015 IoT向け汎用protocol MQTTのリアルタイムゲーム通信利用と実装、そして未来へ…
Drecom Co., Ltd.
 
PDF
Tabc vol3 テクニカルアーティストを始めるにあたって
fumoto kazuhiro
 
PDF
Kansai cedec 2015_fumoto
fumoto kazuhiro
 
PDF
AI in Games- Steering, Wander and Flocking behavior
ナム-Nam Nguyễn
 
PDF
Application parallelisation Android - Klaas Vangend
ナム-Nam Nguyễn
 
PDF
CEDEC 2015 Cocos2d-x と社内基盤の付き合い方 〜アップストリームファーストを目指して〜
Drecom Co., Ltd.
 
PPTX
ACES 1.0 OpenColorIO config - Siggraph 2015
hpduiker
 
뭣이 중헌디? 성능 프로파일링도 모름서 - 유니티 성능 프로파일링 가이드 (IGC16)
ozlael ozlael
 
Shadow gunのサンプルから学べるモバイル最適化
Katsutoshi Makino
 
Unity & VR (Unity Roadshow 2016)
ozlael ozlael
 
Filmic Tonemapping for Real-time Rendering - Siggraph 2010 Color Course
hpduiker
 
中級グラフィックス入門~シャドウマッピング総まとめ~
ProjectAsura
 
CEDEC 2016 Metal と Vulkan を用いた水彩画レンダリング技法の紹介
Drecom Co., Ltd.
 
Weighted Blended Order Independent Transparency
zokweiron
 
Aclt1
zokweiron
 
声の実体化体験 - HTML5でつくるデジタルインスタレーション -
Yamato Honda
 
レイトレ合宿3!!! 5分間アピールプレゼン―Pocol
ProjectAsura
 
シェーダ体系の話
fumoto kazuhiro
 
Unity * スマートフォン開発で学んだこと
Katsutoshi Makino
 
レイトレ合宿2!! 3分間アピールプレゼン―Pocol
ProjectAsura
 
CEDEC 2015 IoT向け汎用protocol MQTTのリアルタイムゲーム通信利用と実装、そして未来へ…
Drecom Co., Ltd.
 
Tabc vol3 テクニカルアーティストを始めるにあたって
fumoto kazuhiro
 
Kansai cedec 2015_fumoto
fumoto kazuhiro
 
AI in Games- Steering, Wander and Flocking behavior
ナム-Nam Nguyễn
 
Application parallelisation Android - Klaas Vangend
ナム-Nam Nguyễn
 
CEDEC 2015 Cocos2d-x と社内基盤の付き合い方 〜アップストリームファーストを目指して〜
Drecom Co., Ltd.
 
ACES 1.0 OpenColorIO config - Siggraph 2015
hpduiker
 
Ad

Similar to Optimizing mobile applications - Ian Dundore, Mark Harkness (20)

PPTX
Tales from the Optimization Trenches - Unite Copenhagen 2019
Unity Technologies
 
PPTX
Unity best practices (2013)
Benjamin Robert
 
PDF
【Unite Tokyo 2018】実践的なパフォーマンス分析と最適化
Unity Technologies Japan K.K.
 
PDF
【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~
Unity Technologies Japan K.K.
 
PPTX
Шлигін Олександр “Розробка ігор в Unity загальні помилки” GameDev Conference ...
Lviv Startup Club
 
PDF
Optimization in Unity: simple tips for developing with "no surprises" / Anton...
DevGAMM Conference
 
PPTX
[UniteKorea2013] Memory profiling in Unity
William Hugo Yang
 
PDF
Unity Internals: Memory and Performance
DevGAMM Conference
 
PDF
Game Programming 13 - Debugging & Performance Optimization
Nick Pruehs
 
PDF
What the Unity engine documentation does not tell you?
Łukasz Stępniak
 
PPTX
Maximizing performance of 3 d user generated assets in unity
WithTheBest
 
PDF
Unity optimization techniques applied in Catan Universe
Exozet Berlin GmbH
 
PDF
Unite 2013 optimizing unity games for mobile platforms
ナム-Nam Nguyễn
 
PDF
Unite2013-gavilan-pdf
David Gavilan
 
PPT
Well Behaved Mobile Apps on AIR - Performance Related
Renaun Erickson
 
PDF
Basic Optimization and Unity Tips & Tricks by Yogie Aditya
gamelanYK
 
PDF
Unity3d Game Development - Creatiosoft
CreatioSoft
 
PDF
【Unite 2017 Tokyo】最適化をする前に覚えておきたい技術
Unity Technologies Japan K.K.
 
PPTX
Advanced Mobile Optimizations
Транслируем.бел
 
PPTX
[Unite Seoul 2020] Mobile Graphics Best Practices for Artists
Owen Wu
 
Tales from the Optimization Trenches - Unite Copenhagen 2019
Unity Technologies
 
Unity best practices (2013)
Benjamin Robert
 
【Unite Tokyo 2018】実践的なパフォーマンス分析と最適化
Unity Technologies Japan K.K.
 
【Unite 2017 Tokyo】Unity最適化講座 ~スペシャリストが教えるメモリとCPU使用率の負担最小化テクニック~
Unity Technologies Japan K.K.
 
Шлигін Олександр “Розробка ігор в Unity загальні помилки” GameDev Conference ...
Lviv Startup Club
 
Optimization in Unity: simple tips for developing with "no surprises" / Anton...
DevGAMM Conference
 
[UniteKorea2013] Memory profiling in Unity
William Hugo Yang
 
Unity Internals: Memory and Performance
DevGAMM Conference
 
Game Programming 13 - Debugging & Performance Optimization
Nick Pruehs
 
What the Unity engine documentation does not tell you?
Łukasz Stępniak
 
Maximizing performance of 3 d user generated assets in unity
WithTheBest
 
Unity optimization techniques applied in Catan Universe
Exozet Berlin GmbH
 
Unite 2013 optimizing unity games for mobile platforms
ナム-Nam Nguyễn
 
Unite2013-gavilan-pdf
David Gavilan
 
Well Behaved Mobile Apps on AIR - Performance Related
Renaun Erickson
 
Basic Optimization and Unity Tips & Tricks by Yogie Aditya
gamelanYK
 
Unity3d Game Development - Creatiosoft
CreatioSoft
 
【Unite 2017 Tokyo】最適化をする前に覚えておきたい技術
Unity Technologies Japan K.K.
 
Advanced Mobile Optimizations
Транслируем.бел
 
[Unite Seoul 2020] Mobile Graphics Best Practices for Artists
Owen Wu
 
Ad

More from ozlael ozlael (20)

PDF
유니티 그래픽 최적화, 어디까지 해봤니 (Optimizing Unity Graphics) Unite Seoul Ver.
ozlael ozlael
 
PDF
Infinity Blade and beyond
ozlael ozlael
 
PDF
스티브잡스처럼 프레젠테이션하기
ozlael ozlael
 
PDF
유니티의 라이팅이 안 이쁘다구요? (A to Z of Lighting)
ozlael ozlael
 
PDF
Introduce coco2dx with cookingstar
ozlael ozlael
 
PDF
Deferred rendering case study
ozlael ozlael
 
PDF
Kgc make stereo game on pc
ozlael ozlael
 
PPTX
mssao presentation
ozlael ozlael
 
PDF
Modern gpu optimize blog
ozlael ozlael
 
PDF
Modern gpu optimize
ozlael ozlael
 
PDF
Bickerstaff benson making3d games on the playstation3
ozlael ozlael
 
PDF
DOF Depth of Field
ozlael ozlael
 
PDF
Hable uncharted2(siggraph%202010%20 advanced%20realtime%20rendering%20course)
ozlael ozlael
 
PDF
Deferred rendering in_leadwerks_engine[1]
ozlael ozlael
 
PDF
Deferred shading
ozlael ozlael
 
PDF
Deferred Rendering in Killzone 2
ozlael ozlael
 
PDF
Ssao
ozlael ozlael
 
PDF
Deferred lighting
ozlael ozlael
 
PDF
Inferred lighting
ozlael ozlael
 
유니티 그래픽 최적화, 어디까지 해봤니 (Optimizing Unity Graphics) Unite Seoul Ver.
ozlael ozlael
 
Infinity Blade and beyond
ozlael ozlael
 
스티브잡스처럼 프레젠테이션하기
ozlael ozlael
 
유니티의 라이팅이 안 이쁘다구요? (A to Z of Lighting)
ozlael ozlael
 
Introduce coco2dx with cookingstar
ozlael ozlael
 
Deferred rendering case study
ozlael ozlael
 
Kgc make stereo game on pc
ozlael ozlael
 
mssao presentation
ozlael ozlael
 
Modern gpu optimize blog
ozlael ozlael
 
Modern gpu optimize
ozlael ozlael
 
Bickerstaff benson making3d games on the playstation3
ozlael ozlael
 
DOF Depth of Field
ozlael ozlael
 
Hable uncharted2(siggraph%202010%20 advanced%20realtime%20rendering%20course)
ozlael ozlael
 
Deferred rendering in_leadwerks_engine[1]
ozlael ozlael
 
Deferred shading
ozlael ozlael
 
Deferred Rendering in Killzone 2
ozlael ozlael
 
Deferred lighting
ozlael ozlael
 
Inferred lighting
ozlael ozlael
 

Recently uploaded (20)

PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
July Patch Tuesday
Ivanti
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
July Patch Tuesday
Ivanti
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 

Optimizing mobile applications - Ian Dundore, Mark Harkness

  • 5. About This Talk • Getting Good Data • General Best Practices • Common Problems & Solutions • Memory Usage • CPU Performance
  • 7. Use The Best Tools • iOS: Instruments • Android: VTune, Snapdragon Profiler • Unity Editor • Timeline • 5.3: Memory Profiler
  • 8. Instruments! • Free, included with XCode • Works perfectly with Unity IL2CPP builds • Best tool for mobile CPU profiling • Best tool for startup time profiling
  • 9. Instruments! (2) • Instructions on how to run it: • http://blogs.unity3d.com/2016/02/01/profiling-with- instruments/
  • 12. Instruments: Reading the PlayerLoop • BaseBehaviourManager::CommonUpdate • Update, FixedUpdate and LateUpdate callbacks • PhysicsManager::FixedUpdate • PhysX simulation, OnCollision* and OnTrigger* callbacks • Physics2DManager::FixedUpdate if using 2D physics • DelayedCallManager::Update • Resumed coroutines
  • 13. Instruments: Reading the PlayerLoop (2) • PlayerRender • Draw calls, batching, OnWillRender & image effect callbacks • UI::CanvasManager::WillRenderCanvases • UI canvas rebatching, text mesh generation, etc. • EnlightenRuntimeManager::Update • Enlighten, precomputed realtime GI, reflection probes
  • 16. Instruments: Coroutines (2) • Coroutine execution is split between two places: • The method where the coroutine was started. • i.e. where StartCoroutine() was called • DelayedCallManager • StartCoroutine runs all code until the first yield • DelayedCalledManager runs the rest
  • 17. Instruments: Summarizing Distributed Costs • Enter method name into “Search” box • Suggested searches: • “::Box”, “Box(“ and “_Box” • “String_”
  • 20. 5.3 Memory Profiler • Download code from Bitbucket • http://bitbucket.org/Unity-Technologies/MemoryProfiler/ • Drop into an Editor folder inside Assets • In Unity Editor: Window > MemoryProfilerWindow • Connect Unity Profiler via Profiler Window • Click “Take Snapshot”
  • 21. 5.3 Memory Profiler: Duplicated Textures Examine these
  • 22. 5.3 Memory Profiler: Duplicated Textures Same Texture, Different Instances Same Different
  • 25. Asset Auditing: Preventing Mistakes • Developers are people (arguably) • People make mistakes • Mistakes cost dev time • Write tools to prevent common, costly errors
  • 26. Asset Auditing: Common Errors • Insane texture sizes • Asset compression • Improper Avatar/Rig settings • Different rules for different parts of project
  • 27. Asset Auditing: HOWTO public class AssetAuditExample : AssetPostprocessor { public void OnPreprocessTexture() { // … } public void OnPreprocessModel() { // … } }
  • 28. Asset Auditing: HOWTO (2) • AssetPostprocessor classes receive callbacks on import • Implement OnPreprocess* methods • Apply your project’s rules to assetImporter instance
  • 29. Asset Auditing: HOWTO (3) public class ReadOnlyModelPostprocessor : AssetPostprocessor { public void OnPreprocessModel() { ModelImporter modelImporter = (ModelImporter)assetImporter; if(modelImporter.isReadable) { modelImporter.isReadable = false; modelImporter.SaveAndReimport(); } } }
  • 30. Common Rules: Textures • Make sure Read/Write is disabled • Disable mipmaps if possible • Make sure textures are Compressed • Ensure sizes aren’t too large • 2048x2048 or 1024x1024 for UI atlases • 512x512 or smaller for model textures
  • 31. Common Rules: Models • Make sure Read/Write is disabled • Disable rig on non-character models • Copy avatars for characters with shared rigs • Enable mesh compression
  • 32. Common Rules: Audio • MP3 compression on iOS • Vorbis compression on Android • “Force Mono” for mobile games • Set bitrate as low as possible
  • 34. Managed Memory: How It Works Texture #1Texture #2Audio ClipMesh int[] Arraystringstringstring Heap contains objects allocated for Assets and Scripts
  • 35. Managed Memory: How It Works Texture #1Texture #2Audio ClipMesh int[] Arraystringstringstring int[] someNumbers = new int[2048]; More memory is allocated when requested by code. int[] Array
  • 36. Managed Memory: How It Works Texture #1Texture #2Audio ClipMesh int[] Arraystringstringstring GC.Collect(); Garbage collector runs periodically, looks for unused objects. Unused objects are deleted. int[] Array
  • 37. Managed Memory: How It Works Texture #1Texture #2Audio ClipMesh int[] Arraystring Holes are not filled. This is Memory Fragmentation. int[] Array
  • 38. Managed Memory: How It Works Texture #1Texture #2Audio ClipMesh int[] Arraystring When there isn’t enough space for new objects… int[] Array int[] Array TOO SMALL
  • 39. Managed Memory: How It Works Texture #1Texture #2Audio ClipMesh int[] Arraystring The heap expands. int[] Array int[] Array
  • 40. Managed Memory: Problems • In Unity, the heap only expands. It never shrinks. • iOS & Android still care about reserved pages. • Detail: Unused blocks of the heap remain reserved, but are paged out of the working set.
  • 41. Managed Memory: Problems (2) • Temporary memory allocations are really bad. • 1 kilobyte of allocation per frame, 60 FPS • = 60 kilobytes per second of allocation • If GC runs once per minute (BAD for framerate)… • 3600 kilobytes of memory needed!
  • 42. Tracking Managed Memory Allocations Use Unity Profiler. Sort by “GC Alloc” column. When user can interact with app, stay as close to zero as possible. (During loading, allocations aren’t as bad.)
  • 43. Memory Conservation • Reuse Collections (Lists, HashSets, etc.) • Avoid string concatenation • Reuse StringBuilders to compose strings • Avoid closures & anonymous methods
  • 44. Memory Conservation: Boxing • Happens when passing a value type as a reference type. • Value is temporarily allocated on the heap. • Example: int x = 1; object y = new object(); y.Equals(x); // Boxes “x” onto the heap
  • 45. Memory Conservation: Boxing (2) • Also happens when using enums as Dictionary keys • Example: enum MyEnum { a, b, c }; var myDictionary = new Dictionary<MyEnum, object>(); myDictionary.Add(MyEnum.a, new object()); // Boxes value “MyEnum.a” • Workaround: Implement IEqualityComparer class
  • 46. Memory Conservation: Foreach • Allocates a Enumerator when loop begins • Specific to Unity’s version of Mono • Just don’t use it.
  • 47. Memory Conservation: Unity APIs • If a Unity API returns an array, it allocates a new copy. • Every time it is accessed. • Even if the values do not change.
  • 48. Memory Conservation: Unity APIs (2) • This code allocates many Touch[] arrays. for ( int i = 0; i < Input.touches.Length; i++ ) { Touch touch = Input.touches[i]; // … }
  • 49. Memory Conservation: Unity APIs (3) • This code allocates only one copy of the Touch[] array. Touch[] touches = Input.touches; for ( int i = 0; i < touches.Length; i++ ) { Touch touch = touches[i]; // … }
  • 51. XML, JSON & other text formats • Parsing text is very slow. • Avoid parsers built on Reflection — extremely slow. • In 5.3: Use Unity’s JsonUtility class! • Three strategies to speed up data parsing.
  • 52. XML/JSON: Reduce Workload • Strategy 1: Don’t parse text. • Bake text data to binary • Use ScriptableObject • Useful for data that does not change often. • e.g. Game design parameters
  • 53. XML/JSON: Reduce Workload (2) • Strategy 2: Do less work. • Split data into smaller chunks. • Parse only the parts that are needed. • Cache parsing results for later reuse.
  • 54. XML/JSON: Reduce Workload (3) • Strategy 3: Threads. • Pure C# types only. • No Unity Assets (ScriptableObjects, Textures, etc.) • Be VERY careful.
  • 55. Large Prefabs • All GameObjects & Components in a prefab are serialized into the prefab’s data file. • Includes all settings on all Components. • 2 identical GameObjects in a prefab = 2 copies of data
  • 56. Large Prefabs (2) • For very large prefabs, split into smaller parts • Use Awake callbacks to instantiate duplicated parts
  • 57. The Resources Folder • An index of Resources is loaded at startup. • Cannot be avoided or deferred. • Solution: Move assets from Resources to Asset Bundles.
  • 59. Easy: Material/Animator/Shader Properties • Never address Material, Shader, or Animator properties by name. • Internally, hashes the property name into an integer. • Don’t do this: material.SetColor(“_Color”, Color.white); animator.SetTrigger(“attack”);
  • 60. Cached Material/Animator/Shader Properties • Do hashing at startup, cache results and reuse them. static readonly int material_Color = Shader.PropertyToID(“_Color”); static readonly int anim_Attack = Animator.StringToHash(“attack”); material.SetColor(material_Color, Color.white); animator.SetTrigger(anim_Attack);
  • 61. Boxing & String Manipulation • These are so expensive we had to mention them twice. • Slow: RegExps, String.StartsWith, String.EndsWith • Instruments: • Search for “::Box” and “_Box” • Search for “String_”
  • 64. Canvases, Draw Calls and Batching • Canvases “rebuild” their batches to reduce draw calls • Rebuilds are very expensive. • A canvas rebuilds if any Drawable component changes. • Any visual change will force a canvas to rebuild. • Drawable = anything visible on a canvas.
  • 65. Canvases, Draw Calls and Batching • Cost of a rebuild based on number of elements. • Includes children. • Frequent changes + lots of UI = lots of CPU usage!
  • 66. Reducing Batching Cost • Reduce number of Drawables. • Merge sprites, merge text objects • Split up Canvases • Balance cost of draw calls and cost of batching.
  • 67. Splitting Canvases • Can nest a canvas within another canvas • Nested canvases isolate their children from rebuilds. • Guideline: Move elements that change every frame onto separate canvases from static elements.
  • 70. Splitting Canvases: Example Change Every Frame Never Change
  • 71. Splitting Canvases: Example Dynamic Canvas Background Canvas
  • 73. Remember… • Profile before optimizing. • Apply these techniques only when needed.
  • 74. How Unity Invokes Callbacks • Internally, C++ Linked List of Components with callbacks • Update, LateUpdate, etc. • Iterate over the Linked List and invoke each callback. • Small overhead when invoking scripts
  • 75. How Unity Invokes Callbacks • If number of callbacks becomes very large, overhead can become significant. • Thousands of callbacks: 10-20% of Update CPU time
  • 76. Replacing Callbacks • Remove Update, LateUpdate, etc. • Make a GameObject with a MonoBehaviour • Implement Update, LateUpdate, and other callbacks on this object • All other code: Subscribe to needed callbacks
  • 77. Replacing Callbacks: UpdateManager object public class UpdateManager : MonoBehaviour { public static UpdateManager Instance { get; set; } void Awake() { Instance = this; } public UnityEvent OnUpdate = new UnityEvent(); void Update() { OnUpdate.Invoke(); } }
  • 78. Replacing Callbacks: Advantages • Eliminates native-to-managed trampoline overhead. • Objects can intelligently unsubscribe. • Don’t need to return out of Update callbacks! • Works well with pooled objects.