Mastering Memory Optimization for Unity Mobile Games: Practical Steps and Tools
This article explains why memory optimization is critical for Unity mobile games, outlines a three‑step workflow of testing, analysis, and fixing, details practical testing methods, data‑analysis toolchains, and concrete optimization techniques such as resource LOD, array pre‑allocation, string deduplication, compression, and atlas management, and concludes with a concise checklist for effective memory management.
Why Memory Optimization Matters
Memory optimization is a key part of performance tuning; out‑of‑memory (OOM) crashes directly affect player experience, especially as mobile games become more graphically intensive and feature‑rich.
Overall Approach
Memory work is divided into incremental and stock optimization. Early in a project the focus is on stock optimization—cleaning up engine frameworks, business code architecture, and art standards—because early fixes yield the greatest benefit. Later, when the game is live, the focus shifts to incremental optimization, which relies on a stable testing process, effective analysis tools, and careful code changes.
Memory‑Optimization Workflow
QA testing generates memory data.
Analyze the data to locate memory issues.
Develop and apply solutions to reduce memory usage.
Testing Phase
QA Testing Focus
Main‑process memory peak.
Peak after multiple main‑process runs.
Special‑scene peaks (events, third‑party SDKs).
Data stability.
Data Analysis Focus
Tool efficiency.
Data archiving.
Development Solution Focus
Prevention > fallback.
Optimize core growth points.
Generating Test Data
Tests must be repeatable, comparable, and as comprehensive as possible.
Daily Smoke Test
Runs the main gameplay flow with fixed variables (team, skin, device, account, server) to isolate memory increments caused by logic changes, new battle modes, or asset updates. Both automated scripts and skill‑flow scripts are used to ensure coverage.
Functional Test
Targets new features such as new skins, subsystem changes, and operation‑activity updates.
Special Test
Simulated player behavior.
Special‑device testing.
Competitor analysis.
Data Analysis Phase
Hierarchical Data Model
Overall data : total memory footprint to assess OOM risk.
Module data : memory per large subsystem, enabling targeted team effort.
Detailed data : allocation stacks and resource names to pinpoint exact sources.
Toolchain for Overall Data
PerfDog – internal performance analysis tool.
XUP – internal performance analysis tool.
Xcode MemGraph – Apple‑provided memory graph.
Toolchain for Module Data
Unity Profiler does not capture all memory (e.g., third‑party SDK, engine core). Solutions include:
Hooking third‑party SDK and engine memory (Android: plt Hook, iOS: FishHook).
Custom tools such as LoliProfiler.
Toolchain for Detailed Data
Combine
MemGraph,
vmmap, and
malloc_history:
Capture a MemGraph snapshot via Xcode.
Use
vmmapto categorize memory zones (Malloc_Large/Small/Tiny = heap, VMALLOCATE = mmap, IOAccelerator = GPU).
Map addresses to allocation stacks with
malloc_historyand separate GameCore and IL2CPP Managed memory.
Specific Optimizations
Business‑Level Optimizations
Resource LOD : load lower‑resolution assets on low‑memory devices and higher‑resolution assets on high‑memory devices.
Pre‑allocate large arrays : reserve sufficient space up‑front to avoid costly runtime expansions.
Avoid string redundancy : replace repeated strings with IDs.
Compression : use appropriate ASTC settings, ACL for animations, and compress tables/strings.
Atlas management : disable read/write on atlases, limit atlas size, and verify settings with automated tools.
Heap Memory
Heap = Unity engine + third‑party SDK + Unity native allocations. Obtain allocation stacks via Instruments Allocations (preferred) or by printing heap addresses and feeding them to
malloc_history.
GameCore Memory
GameCore is written in C++; memory allocation is controlled via a unified interface, allowing direct statistics insertion.
IL2CPP Managed Memory
Use Unity MemoryProfiler to view object categories, but for large projects employ MemoryClawer to export objects, filter by size, and aggregate stacks, making it feasible to locate thousands of strings or arrays.
IOAccelerator (GPU) Memory
Analyze GPU memory with Xcode frame capture tools, which provide clear dirty and swapped size metrics.
Summary Checklist
Establish a stable, repeatable testing pipeline.
Collect “overall‑module‑detail” hierarchical data.
Prioritize preventive optimizations over reactive fixes.
By applying this workflow and the listed techniques, teams can significantly improve memory analysis efficiency and guide concrete optimization actions.
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.