You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[vm] Use autorelease pools in message loop on iOS/macOS #61129
Flutter wraps every message loop task in an autorelease pool. We should do the same in Dart.
Without this, ObjC interop on background isolates is a prone to pseudo memory leaks. An ObjC object created on a background isolate may be kept alive by a thread-scoped autorelease pool, and live for a lot longer than it should.
@liamappelbe there is a question about whether this code belongs in the standalone embedder (does the default VM has objective C interop specifics)? Maybe automatically detect if we are running FFI code involving object-C and do it in the FFI generated code.
there is a question about whether this code belongs in the standalone embedder (does the default VM has objective C interop specifics)?
It would make sense for it to live in the embedder, but I don't know of a way for the embedder to run things in the VM's message loop. The issue is these VM managed background isolates. The embedder doesn't really have any control over them.
Maybe automatically detect if we are running FFI code involving object-C and do it in the FFI generated code.
There's no distinction between C FFI code and ObjC FFI code. Some other possibilities:
If there was a function in Dart's embedding API to enable this feature then I could have package:objective_c invoke that function. That seems a bit roundabout to me though.
It might be possible to detect whether any of the core ObjC functions are called via FFI. All ObjC interop goes through objc_msgSend etc, so maybe we could detect if one of those functions is called then enable the autorelease pool?
I guess the concern is the performance impact? Do any golem benchmarks run on iOS/macOS?
My goal is for this to only affect VM managed isolates. Flutter has its own message loop implementation that already uses autorelease pools before calling into the VM, so I don't want to duplicate that work by adding another autorelease pool to that code path. There's probably a layer I can insert this into that is part of the VM's message loop but not Flutter's.
It's going to be hard to asses the performance impact of this. Golem doesn't run any iOS or macOS benchmarks. I'd have to rely on Flutter's benchmarks, but it's unlikely that their benchmarks would touch this case. We'd need a benchmark that does a lot of small chunks of work, separated by async gaps, on a background isolate. Best bet is probably to write my own microbenchmark. I expect autorelease pools to be super cheap, but it'd be good to confirm this.
If it turns out they're not negligibly cheap, the easiest way to only enable this when it's needed would be to add a function to the dart:ffi library, eg set enableObjCAutoreleasePool(bool value). This would simply set a flag that the VM can check before creating the pool. In fact, the flag should probably be per-isolate. Users wouldn't need to call the setter, it would be called automatically by package:objective_c.
Note to self: Make sure to guard against the flag being modified inside the task. If we implement this naively, and the flag is set to true by the task being run in the message loop, then we would try to release a non-existent pool, causing a crash. Can either store the flag value before running the task, or write the control flow in such a way that we only check the flag once.
My gut feeling is that we shouldn't be doing this. It's a bad smell to rely on a top-level autorelease pool to free memory.
It requires all threads to have this auto release pool: puts burden on embedder for embedder-managed isolates (flutter isn't only embedder), may be some overhead that's not needed in many cases, cannot guarantee such pool exists on 3rd party threads that use FFI callbacks to call back into Dart
It delays freeing memory, possibly indefinitely & therefore leading to effective memory leaks: I can imagine use cases where one spawns a isolate or shared isolate that just stays alive and does synchronous work all the time (never yields back to event loop) - with our move to shared memory multithreading, we're going to get more and more into a world where threads can synchronously coordinate (see e.g. package:native_synchronization)
It can accumulate lots of memory before yielding to event loop: Imagine a while (true) { ... <something allocates into top-level autorelease pool> ... }
Without this, ObjC interop on background isolates is a prone to pseudo memory leaks. An ObjC object created on a background isolate may be kept alive by a thread-scoped autorelease pool, and live for a lot longer than it should.
It doesn't seem like there's a guarantee that each OS thread will have an autorelease pool. Only some certain specific ones do.
Assuming we run code on a thread without an autorelease pool and make it call ObjC that allocates into an auto release pool then this should result in a "no active autorelease pool available" error/crash, no?
That being said, I think it may make sense to have an API in package:objective_c to run code with an active auto release pool - similar to package:ffis using((arena) { ... }).
Is there a way we can detect if an allocation goes into a top-level/event-loop-level autorelease pool and error?
It doesn't seem like there's a guarantee that each OS thread will have an autorelease pool. Only some certain specific ones do.
Yeah, depending on what thread creation API you use, there may or may not be a top level pool.
Assuming we run code on a thread without an autorelease pool and make it call ObjC that allocates into an auto release pool then this should result in a "no active autorelease pool available" error/crash, no?
If you autorelease an object on a thread that doesn't have any autorelease pools, it just leaks (docs, see Autorelease Pools section). I believe it will log a warning at runtime, but it doesn't cause a fatal error, and I don't think we can detect it on our end1.
Even if there is an autorelease pool, if pools are not created and destroyed periodically, you get a pseudo memory leak anyway. So from a memory management perspective, the distinction between no pool, or a thread level pool that lives too long, doesn't matter too much.
This is particularly problematic because in modern ObjC, under ARC, autoreleasing is extremely common, with most methods that return an object autoreleasing it. It would be difficult to write ObjC interoping code that doesn't create a bunch of autoreleased references.
autorelease pools exist in a stack, and it's best practice to push and pop pools often enough that these objects are all cleaned up in a reasonable amount of time. Flutter does this by wrapping their message loop tasks in a pool push/pop, and UIKit does so every iteration of the main run loop.
It delays freeing memory, possibly indefinitely & therefore leading to effective memory leaks: I can imagine use cases where one spawns a isolate or shared isolate that just stays alive and does synchronous work all the time (never yields back to event loop) - with our move to shared memory multithreading, we're going to get more and more into a world where threads can synchronously coordinate (see e.g. package:native_synchronization)
It can accumulate lots of memory before yielding to event loop: Imagine a while (true) { ... <something allocates into top-level autorelease pool> ... }
If users are doing this sort of thing, I think it's fair to expect them to add their own autorelease pools to the pool stack. ObjC/Swift devs will already be used to doing this. For Dart devs we'll need to document this pattern.
But I still think we need a message loop level pool (on any isolate that interacts with ObjC code) to handle the common case where users are doing short lived tasks in between async gaps. At the moment autoreleased objects are leaking since there's no pool on these VM managed threads (or perhaps pseudo leaking depending on what thread API the VM is using). Otherwise users will have to manually wrap all their code that interacts with ObjC on a background isolate in an autorelease pool.
A more extreme solution would be to push/pop an autorelease pool on every ObjC call. We could do this in code gen to hide it from users. We retain the returned references in our wrapper objects, so this would work. I think this solution is excessive though, and I wouldn't want to impose that overhead.
That being said, I think it may make sense to have an API in package:objective_c to run code with an active auto release pool - similar to package:ffis using((arena) { ... }).
Yeah, this would be a good API to add. I have a bug tracking that already. I'll bump the priority.
Footnotes
From my reading there's a function that is called in this flow called objc_autoreleaseNoPool, and people will often set lldb to break on calls to this function. Theoretically we might be able to do some linker shenanigans to replace this function with our own implementation and FATAL or something, but this would be a lot more trouble than its worth imo. ↩
Users are reporting memory leaks due to the lack of autorelease pools (eg dart-lang/http#1791). @brianquinlan has a workaround that switches various API calls in package:objective_c from autoreleasing invocations to similar methods that return an owned reference. But that's not a general solution, and won't always be possible.
I'm going to go ahead with adding autorelease pools to the message loop, like Flutter does. I'll write a small benchmark to see if there's any performance impact, and if there is then I'll make conditional on whether the user is doing ObjC interop.
Chatted to @dcharkes about this offline. From a layering perspective, pushing and popping autorelease pools on every ObjC call is probably cleaner. Though the performance overhead might be noticable in some use cases (eg deep converting a Java list to Dart, would push/pop O(n) pools).
Not opposed to landing cl/551420 per se, but most of what I mentioned above in #61129 (comment) is still valid I think:
long running synchronous code may not yield to event loop in timely manner (leading to accumulating large amounts of objects in the event loop pool)
it wouldn't cover callback from C (if we supported 3rd party threads calling Dart callbacks which may invoke ObjC code)
it puts ObjC knowledge into core Dart VM (which otherwise doesn't know about it)
We do have autoReleasePool<>() API for Dart code, but it wouldn't be ergonomic for users to call this around each ObjC call (and some would certainly forget to do so anyway)
As a last resort (to avoid crashes) the ObjC runtime seemingly lazily creates autorelease pools for threads that don't have one automatically (see here). But this isn't something that normal code should rely on, it's more of a last resort for bad code.
There's some questions
What's the overhead of having a pool per ObjC call?
How Swift->ObjC does it?
For the layering problem: Theoretically we could expose a mechanism that allows package:objective_c to hook into pre/post event loop processing to run logic there. Then we could avoid the ObjC autopool logic in core VM message handling code and instead let the package do the same: lazily create the pool and in post-event hook free the pool (or possibly even using something smarter to allocate/release pools after N ObjC calls -- if we can guarantee to maintain the pool nesting).
Why do objects land in auto release pool
Though fundamentally the question is why objects get put into the auto release pool in the first place. It may be that it's mostly returned objects are put into the inner most release pool?
In normal Swift/ObjC this would be horrendously high overhead and would artificially delay object reclamation.
We (@mraleph and @mkustermann ) have been looking at that today and it seems for normal ARC using code there's a ARC calling convention protocol (e.g. described here) the caller & callee must adhere to. If they adhere to the protocol (which the callee verifies), the returned value is stored in a TLS slot. The caller takes it from there and owns it and eventually frees it. No interaction with the auto release pool.
But if the caller doesn't adhere to the ARC protocol (probably our FFI calls don't I assume) then the callee will put it into autorelease pool, which would then cause accumulation of every single object returned from ObjC (in a Dart->ObjC calls).
@liamappelbe Could it be that we end up in lots of autorelease pool owned objects because we don't adhere to this ARC protocol?
long running synchronous code may not yield to event loop in timely manner (leading to accumulating large amounts of objects in the event loop pool)
Yeah, this is true in pure ObjC code too. In certain use cases it may be necessary to wrap parts of your code in an autorelease pool to clean up these references faster. That's what the autoReleasePool<>() API is for. We already require ObjC interop users to think about native memory management to some extent, so this is not an unreasonable API imo.
We do have autoReleasePool<>() API for Dart code, but it wouldn't be ergonomic for users to call this around each ObjC call (and some would certainly forget to do so anyway)
Yeah, it wouldn't be reasonable to expect users to do this on every call. The two options we're considering are:
Wrap the Dart event loop in an autorelease pool
Wrap every ObjC call in an autorelease pool (automatically in FFIgen)
As a last resort (to avoid crashes) the ObjC runtime seemingly lazily creates autorelease pools for threads that don't have one automatically (see here). But this isn't something that normal code should rely on, it's more of a last resort for bad code.
Yeah, in Dart's case this would create an autorelease pool on long running thread pool threads that might last the entire lifetime of the app.
What's the overhead of having a pool per ObjC call?
I'll benchmark this soon.
How Swift->ObjC does it?
Swift uses essentially the same memory management model as ObjC, though due to improvements in ARC they lean on autorelease a lot less. But the language still has autoreleasepool { ... } blocks for the same reason we do. If you're writing an iOS app in ObjC or swift, you get an autorelease pool in the event loop (eg per frame), but if you're calling many ObjC APIs before yielding to the event loop, you might want to wrap the code in an autorelease pool. Ie the exact same story that the Dart event loop pool solution would have.
Why do objects land in auto release pool
Though fundamentally the question is why objects get put into the auto release pool in the first place. It may be that it's mostly returned objects are put into the inner most release pool?
In normal Swift/ObjC this would be horrendously high overhead and would artificially delay object reclamation.
It's not all that bad. In a typical app autorelease clean up happens (at least) every frame on the main thread, and at event loop boundaries on helper threads. For longer running stuff, developers are used to using autorelease pools.
We (@mraleph and @mkustermann ) have been looking at that today and it seems for normal ARC using code there's a ARC calling convention protocol (e.g. described here) the caller & callee must adhere to. If they adhere to the protocol (which the callee verifies), the returned value is stored in a TLS slot. The caller takes it from there and owns it and eventually frees it. No interaction with the auto release pool.
But if the caller doesn't adhere to the ARC protocol (probably our FFI calls don't I assume) then the callee will put it into autorelease pool, which would then cause accumulation of every single object returned from ObjC (in a Dart->ObjC calls).
@liamappelbe Could it be that we end up in lots of autorelease pool owned objects because we don't adhere to this ARC protocol?
No. What's you're describing is this proposal, where autoreleased return values can be promoted to a strong reference (removing them from the autorelease pool). I'm planning to implement this, but it's not a complete solution.
Empirically, ObjC APIs still do create a lot of autoreleased references internally. For example, if the callee ignores the return value it will remain in the pool. This edge case won't be an issue in the our interop case (the objc_retainAutoreleasedReturnValue will happen before the user chooses to ignore the result), but I suspect this is why ObjC still creates so many internal autorelease refs.
Daco suggested we look at the git history of flutter's event loop pools:
This PR adds an @autorelease pool to the Darwin concurrent message loop to fix memory leaks.
This PR adds an @autorelease pool to the iOS message handler, also to fix memory leaks. It specifically calls out Dart's internal threads don't use autorelease pools.
FML's message loop has had an @autorelease pool since the initial commit
What's the overhead of having a pool per ObjC call?
I'll benchmark this soon.
Autorelease pools are cheap. Adds about 10-20ns per call on my laptop, which is about the same as the objc_msgSend overhead. So it doubles the time it takes to invoke an empty method, but would be negligible for a method doing real work.
Daco suggested we look at the git history of flutter's event loop pools:
This PR adds an @autorelease pool to the Darwin concurrent message loop to fix memory leaks.
This PR adds an @autorelease pool to the iOS message handler, also to fix memory leaks. It specifically calls out Dart's internal threads don't use autorelease pools.
FML's message loop has had an @autorelease pool since the initial commit
@gaaclarke Could you elaborate on why the auto release pools are covering the message loop in Flutter (including executing Dart code) rather than the scope where you're invoking iOS/MacOS APIs that internally make use of auto-released references?
We are considering doing the latter, wrapping FFI interop calls to Objective-C/Swift with an auto-release pool, rather than the message loop from Dart. Either approach would fix interop problems. Doing it on the interop boundary seems cleaner layering-wise (as converting an auto-release reference to a strong reference for return values also happens on the interop boundary). But maybe we're missing something.
It's worth considering similarities between isolate event loops and Objective-C DispatchQueue's. These queues have configurable autorelease behavior with the following defaults:
main dispatch queue wraps each work item into autorelease
background queues by default release when the thread goes idle (though it is not documented).
I don't think it is layering violation to have ARC related stuff in the VM internals. ARC is a part of the platform which we at least partially abstract away - so it is natural we have to deal with it in our internals.
So I think we should do both things:
Update our event loops with some logic for poping autorelease pools (granularity TBD)
Make sure that objects do not end up in autorelease pools unnecessary (match ARC protocol for claiming autoreleased objects).
For example, if the callee ignores the return value it will remain in the pool.
We did some tests of this yesterday with @mraleph and IIRC in both scenarios (caller uses the value and caller ignores the value) the object gets pulled out of the pool in an ARC world. So in an ARC world it's not very clear to me how objects would commonly end up in autorelease pools (**). -- It happens with our ObjC calls because we don't adhere to the protocol, so all returned objects we get end up in auto release pools. We should probably fix that.
But I agree that we can have a pool on message loop boundaries as a last resort.
(**) Legacy non ARC ObjC code is a different story where reliance on these pools may be much more common.
@gaaclarke Could you elaborate on why the auto release pools are covering the message loop in Flutter (including executing Dart code) rather than the scope where you're invoking iOS/MacOS APIs that internally make use of auto-released references?
It's just convenience. It captures a large swath of usage and performs at a cadence that doesn't allow the junk to overflow. Clearing out the autorelease pool at the end of events is the expected norm on apple platforms.
Apple's APIs, especially before ARC, didn't really provide any mechanism for you to know if you are ever accumulating any data in the autorelease pool. So that really pushed casting the widest net.
The only concern with N autorelease pools per event is just overhead. You are kind of playing the same game you do in garbage collected languages, trying to manage the most efficient cleanup heuristic.
There are other examples I expect will not be removed from the autorelease pool. Blocks have their own retain function, obcj_retainBlock (the main difference is that it copies the block to the heap if it's a stack allocated block), but there's no equivalent of objc_retainAutoreleasedReturnValue for blocks. Using objc_retainAutoreleasedReturnValue on a block would internally objc_retain it, which would crash if it's a stack allocated block. So presumably autoreleased blocks are ending up in the pool.
Empirically, Brian tested switching to objc_retainAutoreleasedReturnValue, and still saw elements ending up in the pool in his use case. Not sure if those are blocks or if they come from legacy non-ARC code as you suggest.
It happens with our ObjC calls because we don't adhere to the protocol, so all returned objects we get end up in auto release pools. We should probably fix that.
I'll go ahead and land the event loop pool too. I suspect that, plus objc_retainAutoreleasedReturnValue, will solve the problem in all but the most extreme use cases, and we have the Dart autoreleasePool() API to cover those edge cases (though I need to document this better). So I think adding an autorelease pool to every FFI ObjC call would be unnecessary overkill. We can add it later if users report issues, because it's a fairly small overhead.
Activity
a-siva commentedon Jul 23, 2025
@liamappelbe there is a question about whether this code belongs in the standalone embedder (does the default VM has objective C interop specifics)? Maybe automatically detect if we are running FFI code involving object-C and do it in the FFI generated code.
liamappelbe commentedon Jul 23, 2025
It would make sense for it to live in the embedder, but I don't know of a way for the embedder to run things in the VM's message loop. The issue is these VM managed background isolates. The embedder doesn't really have any control over them.
There's no distinction between C FFI code and ObjC FFI code. Some other possibilities:
objc_msgSendetc, so maybe we could detect if one of those functions is called then enable the autorelease pool?I guess the concern is the performance impact? Do any golem benchmarks run on iOS/macOS?
liamappelbe commentedon Jul 29, 2025
My goal is for this to only affect VM managed isolates. Flutter has its own message loop implementation that already uses autorelease pools before calling into the VM, so I don't want to duplicate that work by adding another autorelease pool to that code path. There's probably a layer I can insert this into that is part of the VM's message loop but not Flutter's.
It's going to be hard to asses the performance impact of this. Golem doesn't run any iOS or macOS benchmarks. I'd have to rely on Flutter's benchmarks, but it's unlikely that their benchmarks would touch this case. We'd need a benchmark that does a lot of small chunks of work, separated by async gaps, on a background isolate. Best bet is probably to write my own microbenchmark. I expect autorelease pools to be super cheap, but it'd be good to confirm this.
If it turns out they're not negligibly cheap, the easiest way to only enable this when it's needed would be to add a function to the
dart:ffilibrary, egset enableObjCAutoreleasePool(bool value). This would simply set a flag that the VM can check before creating the pool. In fact, the flag should probably be per-isolate. Users wouldn't need to call the setter, it would be called automatically bypackage:objective_c.Note to self: Make sure to guard against the flag being modified inside the task. If we implement this naively, and the flag is set to true by the task being run in the message loop, then we would try to release a non-existent pool, causing a crash. Can either store the flag value before running the task, or write the control flow in such a way that we only check the flag once.
mkustermann commentedon Sep 17, 2025
My gut feeling is that we shouldn't be doing this. It's a bad smell to rely on a top-level autorelease pool to free memory.
package:native_synchronization)while (true) { ... <something allocates into top-level autorelease pool> ... }It doesn't seem like there's a guarantee that each OS thread will have an autorelease pool. Only some certain specific ones do.
Assuming we run code on a thread without an autorelease pool and make it call ObjC that allocates into an auto release pool then this should result in a "no active autorelease pool available" error/crash, no?
That being said, I think it may make sense to have an API in
package:objective_cto run code with an active auto release pool - similar topackage:ffisusing((arena) { ... }).Is there a way we can detect if an allocation goes into a top-level/event-loop-level autorelease pool and error?
liamappelbe commentedon Sep 17, 2025
Yeah, depending on what thread creation API you use, there may or may not be a top level pool.
If you autorelease an object on a thread that doesn't have any autorelease pools, it just leaks (docs, see Autorelease Pools section). I believe it will log a warning at runtime, but it doesn't cause a fatal error, and I don't think we can detect it on our end1.
Even if there is an autorelease pool, if pools are not created and destroyed periodically, you get a pseudo memory leak anyway. So from a memory management perspective, the distinction between no pool, or a thread level pool that lives too long, doesn't matter too much.
This is particularly problematic because in modern ObjC, under ARC, autoreleasing is extremely common, with most methods that return an object autoreleasing it. It would be difficult to write ObjC interoping code that doesn't create a bunch of autoreleased references.
autorelease pools exist in a stack, and it's best practice to push and pop pools often enough that these objects are all cleaned up in a reasonable amount of time. Flutter does this by wrapping their message loop tasks in a pool push/pop, and UIKit does so every iteration of the main run loop.
If users are doing this sort of thing, I think it's fair to expect them to add their own autorelease pools to the pool stack. ObjC/Swift devs will already be used to doing this. For Dart devs we'll need to document this pattern.
But I still think we need a message loop level pool (on any isolate that interacts with ObjC code) to handle the common case where users are doing short lived tasks in between async gaps. At the moment autoreleased objects are leaking since there's no pool on these VM managed threads (or perhaps pseudo leaking depending on what thread API the VM is using). Otherwise users will have to manually wrap all their code that interacts with ObjC on a background isolate in an autorelease pool.
A more extreme solution would be to push/pop an autorelease pool on every ObjC call. We could do this in code gen to hide it from users. We retain the returned references in our wrapper objects, so this would work. I think this solution is excessive though, and I wouldn't want to impose that overhead.
Yeah, this would be a good API to add. I have a bug tracking that already. I'll bump the priority.
Footnotes
From my reading there's a function that is called in this flow called
objc_autoreleaseNoPool, and people will often set lldb to break on calls to this function. Theoretically we might be able to do some linker shenanigans to replace this function with our own implementation andFATALor something, but this would be a lot more trouble than its worth imo. ↩liamappelbe commentedon Sep 16, 2026
Users are reporting memory leaks due to the lack of autorelease pools (eg dart-lang/http#1791). @brianquinlan has a workaround that switches various API calls in package:objective_c from autoreleasing invocations to similar methods that return an owned reference. But that's not a general solution, and won't always be possible.
I'm going to go ahead with adding autorelease pools to the message loop, like Flutter does. I'll write a small benchmark to see if there's any performance impact, and if there is then I'll make conditional on whether the user is doing ObjC interop.
liamappelbe commentedon Sep 16, 2026
Wrote a quick benchmark, and there's no detectable performance difference. So I'll add the pool unconditionally on mac/iOS.
brianquinlan commentedon Sep 16, 2026
Do we need to apply this change to Flutter as well for background isolates?
liamappelbe commentedon Sep 16, 2026
No. This change would cover any VM managed isolates, including Flutter background isolates.
brianquinlan commentedon Sep 16, 2026
That's the part that I was wondering about, so +1
liamappelbe commentedon Sep 17, 2026
Chatted to @dcharkes about this offline. From a layering perspective, pushing and popping autorelease pools on every ObjC call is probably cleaner. Though the performance overhead might be noticable in some use cases (eg deep converting a Java list to Dart, would push/pop O(n) pools).
cc @mkustermann
mkustermann commentedon Sep 17, 2026
Not opposed to landing cl/551420 per se, but most of what I mentioned above in #61129 (comment) is still valid I think:
We do have
autoReleasePool<>()API for Dart code, but it wouldn't be ergonomic for users to call this around each ObjC call (and some would certainly forget to do so anyway)As a last resort (to avoid crashes) the ObjC runtime seemingly lazily creates autorelease pools for threads that don't have one automatically (see here). But this isn't something that normal code should rely on, it's more of a last resort for bad code.
There's some questions
For the layering problem: Theoretically we could expose a mechanism that allows
package:objective_cto hook into pre/post event loop processing to run logic there. Then we could avoid the ObjC autopool logic in core VM message handling code and instead let the package do the same: lazily create the pool and in post-event hook free the pool (or possibly even using something smarter to allocate/release pools after N ObjC calls -- if we can guarantee to maintain the pool nesting).Why do objects land in auto release pool
Though fundamentally the question is why objects get put into the auto release pool in the first place. It may be that it's mostly returned objects are put into the inner most release pool?
In normal Swift/ObjC this would be horrendously high overhead and would artificially delay object reclamation.
We (@mraleph and @mkustermann ) have been looking at that today and it seems for normal ARC using code there's a ARC calling convention protocol (e.g. described here) the caller & callee must adhere to. If they adhere to the protocol (which the callee verifies), the returned value is stored in a TLS slot. The caller takes it from there and owns it and eventually frees it. No interaction with the auto release pool.
But if the caller doesn't adhere to the ARC protocol (probably our FFI calls don't I assume) then the callee will put it into autorelease pool, which would then cause accumulation of every single object returned from ObjC (in a Dart->ObjC calls).
@liamappelbe Could it be that we end up in lots of autorelease pool owned objects because we don't adhere to this ARC protocol?
liamappelbe commentedon Sep 18, 2026
Yeah, this is true in pure ObjC code too. In certain use cases it may be necessary to wrap parts of your code in an autorelease pool to clean up these references faster. That's what the
autoReleasePool<>()API is for. We already require ObjC interop users to think about native memory management to some extent, so this is not an unreasonable API imo.Yeah, it wouldn't be reasonable to expect users to do this on every call. The two options we're considering are:
Yeah, in Dart's case this would create an autorelease pool on long running thread pool threads that might last the entire lifetime of the app.
I'll benchmark this soon.
Swift uses essentially the same memory management model as ObjC, though due to improvements in ARC they lean on autorelease a lot less. But the language still has
autoreleasepool { ... }blocks for the same reason we do. If you're writing an iOS app in ObjC or swift, you get an autorelease pool in the event loop (eg per frame), but if you're calling many ObjC APIs before yielding to the event loop, you might want to wrap the code in an autorelease pool. Ie the exact same story that the Dart event loop pool solution would have.It's not all that bad. In a typical app autorelease clean up happens (at least) every frame on the main thread, and at event loop boundaries on helper threads. For longer running stuff, developers are used to using autorelease pools.
No. What's you're describing is this proposal, where autoreleased return values can be promoted to a strong reference (removing them from the autorelease pool). I'm planning to implement this, but it's not a complete solution.
Empirically, ObjC APIs still do create a lot of autoreleased references internally. For example, if the callee ignores the return value it will remain in the pool. This edge case won't be an issue in the our interop case (the
objc_retainAutoreleasedReturnValuewill happen before the user chooses to ignore the result), but I suspect this is why ObjC still creates so many internal autorelease refs.liamappelbe commentedon Sep 18, 2026
Daco suggested we look at the git history of flutter's event loop pools:
@autoreleasepool to the Darwin concurrent message loop to fix memory leaks.@autoreleasepool to the iOS message handler, also to fix memory leaks. It specifically calls out Dart's internal threads don't use autorelease pools.@autoreleasepool since the initial commitliamappelbe commentedon Sep 18, 2026
Autorelease pools are cheap. Adds about 10-20ns per call on my laptop, which is about the same as the
objc_msgSendoverhead. So it doubles the time it takes to invoke an empty method, but would be negligible for a method doing real work.dcharkes commentedon Sep 18, 2026
@gaaclarke Could you elaborate on why the auto release pools are covering the message loop in Flutter (including executing Dart code) rather than the scope where you're invoking iOS/MacOS APIs that internally make use of auto-released references?
We are considering doing the latter, wrapping FFI interop calls to Objective-C/Swift with an auto-release pool, rather than the message loop from Dart. Either approach would fix interop problems. Doing it on the interop boundary seems cleaner layering-wise (as converting an auto-release reference to a strong reference for return values also happens on the interop boundary). But maybe we're missing something.
mraleph commentedon Sep 18, 2026
It's worth considering similarities between isolate event loops and Objective-C
DispatchQueue's. These queues have configurable autorelease behavior with the following defaults:maindispatch queue wraps each work item into autoreleaseI don't think it is layering violation to have ARC related stuff in the VM internals. ARC is a part of the platform which we at least partially abstract away - so it is natural we have to deal with it in our internals.
So I think we should do both things:
mkustermann commentedon Sep 18, 2026
We did some tests of this yesterday with @mraleph and IIRC in both scenarios (caller uses the value and caller ignores the value) the object gets pulled out of the pool in an ARC world. So in an ARC world it's not very clear to me how objects would commonly end up in autorelease pools (**). -- It happens with our ObjC calls because we don't adhere to the protocol, so all returned objects we get end up in auto release pools. We should probably fix that.
But I agree that we can have a pool on message loop boundaries as a last resort.
(**) Legacy non ARC ObjC code is a different story where reliance on these pools may be much more common.
gaaclarke commentedon Sep 18, 2026
It's just convenience. It captures a large swath of usage and performs at a cadence that doesn't allow the junk to overflow. Clearing out the autorelease pool at the end of events is the expected norm on apple platforms.
Apple's APIs, especially before ARC, didn't really provide any mechanism for you to know if you are ever accumulating any data in the autorelease pool. So that really pushed casting the widest net.
The only concern with N autorelease pools per event is just overhead. You are kind of playing the same game you do in garbage collected languages, trying to manage the most efficient cleanup heuristic.
liamappelbe commentedon Sep 21, 2026
There are other examples I expect will not be removed from the autorelease pool. Blocks have their own retain function,
obcj_retainBlock(the main difference is that it copies the block to the heap if it's a stack allocated block), but there's no equivalent ofobjc_retainAutoreleasedReturnValuefor blocks. Usingobjc_retainAutoreleasedReturnValueon a block would internallyobjc_retainit, which would crash if it's a stack allocated block. So presumably autoreleased blocks are ending up in the pool.Empirically, Brian tested switching to
objc_retainAutoreleasedReturnValue, and still saw elements ending up in the pool in his use case. Not sure if those are blocks or if they come from legacy non-ARC code as you suggest.Done.
I'll go ahead and land the event loop pool too. I suspect that, plus
objc_retainAutoreleasedReturnValue, will solve the problem in all but the most extreme use cases, and we have the DartautoreleasePool()API to cover those edge cases (though I need to document this better). So I think adding an autorelease pool to every FFI ObjC call would be unnecessary overkill. We can add it later if users report issues, because it's a fairly small overhead.[vm] Wrap message loop handler in an autorelease pool