View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Videos

Abrir menú Cerrar menú
  • Colecciones
  • Todos los videos
  • Información

Más videos

  • Información
  • Código
  • 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.

    Recursos

      • Video HD
      • Video SD

    Videos relacionados

    WWDC23

    • Optimize app power and performance for spatial computing

    WWDC22

    • Go bindless with Metal 3

    WWDC21

    • Detect and diagnose memory issues
    • Discover Metal debugging, profiling, and asset creation tools
  • Buscar este video…
    • 6:53 - Available memory for the process

      #import <os/proc.h>
      
      API_UNAVAILABLE(macos) API_AVAILABLE(ios(13.0), tvos(13.0), watchos(6.0))
      size_t os_proc_available_memory(void);
    • 7:07 - Current and peak footprint

      #if __has_include(<libproc.h>)
      #include <libproc.h> // On macOS.
      #else
      #include <sys/resource.h> // On iOS, iPadOS and tvOS.
      int proc_pid_rusage(int pid, int flavor, rusage_info_t *buffer)  
          __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0);
      #endif
      
      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;
    • 10:04 - Record an Instruments trace

      xctrace record --template "Game Memory" \
                     --attach ModernRenderer \
                     --output ModernRenderer.trace \
                     --time-limit 30s
    • 10:14 - 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
    • 16:52 - MallocStackLogging

      # See `man malloc`.
      MallocStackLogging=lite # Live allocations only.
      MallocStackLogging=1    # All allocation and free history.
    • 18:07 - Capture a memory graph

      leaks $PID     --outputGraph foo.memgraph
      # or
      leaks GameName --outputGraph foo.memgraph
    • 20:12 - 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;
    • 20:30 - 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;

Developer Footer

  • Videos
  • WWDC22
  • Profile and optimize your game's memory
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines