Async TaskGroup not releasing memory

When I run this sample app as a command line tool in macOS, after a few seconds it is consuming 1GB of memory and it keeps ramping up from there, until it crashes. I'm not sure what I'm doing wrong. I am waiting for the result of each call to test so it should not be queueing up anything, as far as I can tell, no memory should be retained after each call to `test.

struct TaskGroupTest {
    static func test() async {
        await withTaskGroup(of: Void.self) { group in
            (0..<10).forEach { _ in
                group.addTask {}

            for await _ in group {}

    static func main() async {
        for _ in (0..<1000000) {
            await Self.test()

Are you running in release mode, with optimizations enabled? That should help optimize away some of the memory use.

Thanks, unfortunately I'm seeing the same behavior in release mode though - runaway memory consumption

There was a bug about this which is now fixed. I'm not sure about when the fix landed, exactly. @ktoso?

There was a few of those but it was a long time ago:

This specifically would have been the first one.

It seems this was already in 5.6 and 5.6.1

1 Like

Hmmm, looks like I already have 5.6, maybe something else?. Interesting thing - if I put this code in iOS app, no problem. It runs forever without increasing memory.... The problem only happens to me on macOS.

xcrun --find swift

xcrun swift -version
swift-driver version: 1.45.2 Apple Swift version 5.6 (swiftlang- clang-1316.0.20.8)

This is a runtime fix, and the runtime is in the OS. So you’d have to update your macOS I suppose.

OK, thanks

1 Like

We're looking at whether we can back-deploy this fix among others, but as usual, we can't make any promises.

1 Like