2022 Graphics & Games
WWDC22 · 38 min · Graphics & Games
Profile and optimize your game’s memory
Learn how Apple platforms calculate and allocate memory for your game. We’ll show you how to use Instruments and the Game Memory template to profile your game, take a memory graph to monitor current memory use, and analyze it using Xcode Memory Debugger and command line tools. We’ll also explore Metal resources in Metal Debugger and provide tips and tricks to further help you optimize memory usage.
Watch at developer.apple.com ↗Code shown on screen · 8 snippets
Available memory for the process
API_UNAVAILABLE(macos) API_AVAILABLE(ios(13.0), tvos(13.0), watchos(6.0))
size_t os_proc_available_memory(void); Current and peak footprint
int proc_pid_rusage(int pid, int flavor, rusage_info_t *buffer)
__OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0);
rusage_info_current rusage_payload;
int ret = proc_pid_rusage(getpid(),
RUSAGE_INFO_CURRENT, // I.e., new RUSAGE_INFO_V6 this year.
(rusage_info_t *)&rusage_payload);
NSCAssert(ret == 0, @"Could not get rusage: %i.", errno); // Look up in `man errno`.
uint64_t footprint = rusage_payload.ri_phys_footprint;
uint64_t footprint_peak = rusage_payload.ri_lifetime_max_phys_footprint; Record an Instruments trace
xctrace record --template "Game Memory" \
--attach ModernRenderer \
--output ModernRenderer.trace \
--time-limit 30s Record an Instruments trace, on a selected device
xctrace record --device-name "Seth's iPhone" \
--template "Game Memory" \
--attach ModernRenderer \
--output ModernRenderer.trace \
--time-limit 30s MallocStackLogging
# See `man malloc`.
MallocStackLogging=lite # Live allocations only.
MallocStackLogging=1 # All allocation and free history. Capture a memory graph
leaks $PID --outputGraph foo.memgraph
# or
leaks GameName --outputGraph foo.memgraph Tag mapped anonymous memory
size_t length;
int tag = VM_MAKE_TAG(VM_MEMORY_APPLICATION_SPECIFIC_1); // Check out `man mmap`.
void * reservation = mmap(NULL,
length,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
tag, // Instead of using default `-1`.
0);
if (reservation == MAP_FAILED) {
@throw [[NSError alloc] initWithDomain:NSPOSIXErrorDomain
code:errno
userInfo:nil];
}
return reservation; Tag anonymous memory
size_t page_count;
mach_vm_size_t allocation_size = page_count * PAGE_SIZE;
mach_vm_address_t vm_address;
kern_return_t kr;
kr = mach_vm_allocate(mach_task_self(),
&vm_address,
allocation_size,
VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_MEMORY_APPLICATION_SPECIFIC_1));
if (kr != KERN_SUCCESS) { // Refer to mach/kern_return.h.
@throw [[NSError alloc] initWithDomain:NSMachErrorDomain
code:kr
userInfo:nil];
}
return vm_address; Related sessions
-
29 min -
34 min -
40 min -
29 min