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
  • Analyze heap memory

    Dive into the basis for your app's dynamic memory: the heap! Explore how to use Instruments and Xcode to measure, analyze, and fix common heap issues. We'll also cover some techniques and best practices for diagnosing transient growth, persistent growth, and leaks in your app.

    Capítulos

    • 0:00 - Introduction
    • 1:05 - Heap memory overview
    • 3:45 - Tools for inspecting heap memory issues
    • 7:40 - Transient memory growth overview
    • 10:34 - Managing autorelease pool growth in Swift
    • 13:57 - Persistent memory growth overview
    • 16:00 - How the Xcode memory graph debugger works
    • 20:15 - Reachability and ensuring memory is deallocated appropriately
    • 21:54 - Resolving leaks of Swift closure contexts
    • 24:13 - Leaks FAQ
    • 26:51 - Comparing performance of weak and unowned
    • 30:44 - Reducing reference counting overhead
    • 32:06 - Cost of measurement
    • 32:30 - Wrap up

    Recursos

    • The Swift Programming Language: Automatic Reference Counting
    • Forum: Developer Tools & Services
      • Video HD
      • Video SD

    Videos relacionados

    WWDC24

    • Consume noncopyable types in Swift
    • Explore Swift performance

    WWDC22

    • Profile and optimize your game's memory

    WWDC21

    • Detect and diagnose memory issues

    WWDC18

    • iOS Memory Deep Dive
  • Buscar este video…
    • 10:01 - ThumbnailLoader.makeThumbnail(from:) implementation

      func makeThumbnail(from photoURL: URL) -> PhotoThumbnail {
        validate(url: photoURL)
        var coreImage = CIImage(contentsOf: photoURL)!
      
        let sepiaTone = CIFilter.sepiaTone()
        sepiaTone.inputImage = coreImage
        sepiaTone.intensity = 0.4
        coreImage = sepiaTone.outputImage!
      
        let squareSize = min(coreImage.extent.width, coreImage.extent.height)
        coreImage = coreImage.cropped(to: CGRect(x: 0, y: 0, width: squareSize, height: squareSize))
      
        let targetSize = CGSize(width:64, height:64)
        let scalingFilter = CIFilter.lanczosScaleTransform()
      
        scalingFilter.inputImage = coreImage
        scalingFilter.scale = Float(targetSize.height / coreImage.extent.height)
        scalingFilter.aspectRatio = Float(Double(coreImage.extent.width) / Double(coreImage.extent.height))
        coreImage = scalingFilter.outputImage!
      
        let imageData = context.generateImageData(of: coreImage)
      
        return PhotoThumbnail(size: targetSize, data: imageData, url: photoURL)
      }
    • 10:23 - ThumbnailLoader.loadThumbnails(with:), with autorelease pool growth issues

      func loadThumbnails(with renderer: ThumbnailRenderer) {
        for photoURL in urls {
          renderer.faultThumbnail(from: photoURL)
        }
      }
    • 10:33 - Simple autorelease example

      print("Now is \(Date.now)") // Produces autoreleased .description String
    • 11:08 - Autorelease pool growth in loop

      autoreleasepool {
        // ...
        
        for _ in 1...1000 {
          // Autoreleases into single pool, causing growth as loop runs
          print("Now is \(Date.now)")
        }
        
        // ...
      }
    • 11:50 - Autorelease pool growth in loop, managed by nested pool

      autoreleasepool {
        // ...
        
        for _ in 1...1000 {
          autoreleasepool {
            // Autoreleases into nested pool, preventing outer pool from bloating
            print("Now is \(Date.now)")
          }
        }
        
        // ...
      }
    • 12:16 - ThumbnailLoader.loadThumbnails(with:), with nested autorelease pool growth issues fixed

      func loadThumbnails(with renderer: ThumbnailRenderer) {
          for photoURL in urls {
              autoreleasepool {
                  renderer.faultThumbnail(from: photoURL)
              }
          }
      }
    • 17:27 - C++ class with virtual method

      class Coconut {
        Swallow *swallow;
        virtual void virtualMethod() {}
      };
    • 17:40 - C++ class without virtual method

      class Coconut {
        Swallow *swallow;
      };
    • 18:41 - ThumbnailRenderer.faultThumbnail(from:), caching thumbnails incorrectly

      func faultThumbnail(from photoURL: URL) {
        // Cache the thumbnail based on url + creationDate
        let timestamp = UInt64(Date.now.timeIntervalSince1970) // Bad - caching with wrong timestamp
        let cacheKey = CacheKey(url: photoURL, timestamp: timestamp)
      
        let thumbnail = cacheProvider.thumbnail(for: cacheKey) {
          return makeThumbnail(from: photoURL)
        }
        images.append(thumbnail.image)
      }
    • 19:28 - ThumbnailRenderer.faultThumbnail(from:), caching thumbnails correctly

      func faultThumbnail(from photoURL: URL) {
        // Cache the thumbnail based on url + creationDate
        let timestamp = cacheKeyTimestamp(for: photoURL) // Fixed - caching with correct timestamp
        let cacheKey = CacheKey(url: photoURL, timestamp: timestamp)
      
        let thumbnail = cacheProvider.thumbnail(for: cacheKey) {
          return makeThumbnail(from: photoURL)
        }
        images.append(thumbnail.image)
      }
    • 22:19 - Code creating reference cycle with closure context

      let swallow = Swallow()
      swallow.completion = {
        print("\(swallow) finished carrying a coconut")
      }
    • 23:11 - PhotosView image loading code, with leak

      // ...
      let renderer = ThumbnailRenderer(style: .vibrant)
      let loader = ThumbnailLoader(bundle: .main, completionQueue: .main)
      loader.completionHandler = {
        self.thumbnails = renderer.images // implicit strong capture of renderer causes strong reference cycle
      }
      loader.beginLoading(with: renderer)
      // ...
    • 23:40 - PhotosView image loading code, with leak fixed

      // ...
      let renderer = ThumbnailRenderer(style: .vibrant)
      let loader = ThumbnailLoader(bundle: .main, completionQueue: .main)
      loader.completionHandler = { [weak renderer] in
      	guard let renderer else { return }
      
        self.thumbnails = renderer.images
      }
      loader.beginLoading(with: renderer)
      // ...
    • 24:24 - Intentional leak of manually-managed allocation

      let oops = UnsafeMutablePointer<Int>.allocate(capacity: 16)
      // intentional mistake: missing `oops.deallocate()`
    • 25:12 - Loop over intentional leak of manually-managed allocations

      for _ in 0..<100 {
        let oops = UnsafeMutablePointer<Int>.allocate(capacity: 16)
        // intentional mistake: missing `oops.deallocate()`
      }
    • 26:11 - Nonreturning function which can see leaks of allocations owned by local variables

      func beginServer() {
        let singleton = Server(delegate: self)
        dispatchMain() // __attribute__((noreturn))
      }
    • 26:22 - Fix for reported leak in nonreturning function

      static var singleton: Server?
      
      func beginServer() {
        Self.singleton = Server(delegate: self)
        dispatchMain()
      }
    • 27:21 - Weak reference example

      weak var holder: Swallow?
    • 27:43 - Unowned reference example

      unowned let holder: Swallow
    • 29:07 - Implicit use of self by method causes reference cycle

      class ByteProducer {
        let data: Data
        private var generator: ((Data) -> UInt8)? = nil
      
        init(data: Data) {
          self.data = data
          generator = defaultAction // Implicitly uses `self`
        }
      
        func defaultAction(_ data: Data) -> UInt8 {
          // ...
        }
      }
    • 29:25 - Break reference cycle cause day implicit use of self by method, using weak

      class ByteProducer {
        let data: Data
        private var generator: ((Data) -> UInt8)? = nil
      
        init(data: Data) {
          self.data = data
          generator = { [weak self] data in
            return self?.defaultAction(data)
          }
        }
      
        func defaultAction(_ data: Data) -> UInt8 {
          // ...
        }
      }
    • 29:41 - Break reference cycle cause day implicit use of self by method, using unowned

      class ByteProducer {
        let data: Data
        private var generator: ((Data) -> UInt8)? = nil
      
        init(data: Data) {
          self.data = data
          generator = { [unowned self] data in
            return self.defaultAction(data)
          }
        }
      
        func defaultAction(_ data: Data) -> UInt8 {
          // ...
        }
      }
    • 31:14 - Struct with non-trivial init/copy/deinit

      struct Nontrivial {
        var number: Int64
        var simple: CGPoint?
        var complex: String // Copy-on-write, requires non-trivial struct init/copy/destroy
      }

Developer Footer

  • Videos
  • WWDC24
  • Analyze heap 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