title: Async autoreleasepool
body: <p>I have a situation where I'm creating many Cocoa objects in a loop using <code>async/await</code>, and the memory spikes because the objects are only released when the loop is over (instead of every iteration).</p>
  <p>The solution would be to use an <code>autoreleasepool</code>. However, I can't seem to get <code>autoreleasepool</code> to work with <code>async/await</code>.</p>
  <p>Here is an example:</p>
  <pre><code>func getImage() async -&gt; NSImage? {
      return NSImage(named: &quot;imagename&quot;) // Do some work
  }
  
  Task {
      // This leaks
      for _ in 0 ..&lt; 1000000 {
          let image = await getImage()
          print(image!.backgroundColor)
      }
  }
  </code></pre>
  <p>The memory spikes all the way up to 220MB, which is a bit too much for me.</p>
  <p>Normally, you could wrap the inner loop in a <code>autoreleasepool</code>, and it would fix the problem, but when I try it with an <code>async</code> function, I get this error:</p>
  <pre><code>Cannot pass function of type '() async -&gt; ()' to parameter expecting synchronous function type
  </code></pre>
  <p>Is there any way around this? Or is there another method to accomplish the same goal of releasing the Cocoa objects inside of the loop?</p>