-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak in C++ API with Core ML backend #14455
Comments
A bot tagged this for "mobile". We are dealing with macOS here, I specified the ARM architecture because the Macbook on which I tested happens to have an M1 processor. I do not know enough about iOS/iPadOS to say whether this is a problem on there, too. I do not know if pure C++ applications are even possible on those platforms, so the situation may be different. |
You are welcome to submit a patch. Thanks for reporting it. |
TBH we don't usually write Objective-C code either so we don't claim any expertise there. Are there any potential issues with adding the I didn't notice any places in model.h that could be wrapped. Would it just be things in the Execution class in model.mm that need to be wrapped if that's the class used to bridge to Objective-C (according to the comment anyway)? |
Thanks for coming back to this. All expertise in Objective C memory handling that I can offer comes from one day of Internet search after locating the memory leak in our program. Doing a bit more reading right now I realise that that is certainly not enough. Also, looking again at The code change from my initial comment is used by us and has been tested to resolve the memory leak with any adverse effect. Of course it would be ideal to have extensive test coverage and run it with both ASAN and a leak checker, but the world is rarely ideal. Looking at |
This issue about a memory leak from CoreML seems relevant. The suggested fix is to wrap the CoreML prediction call in an |
@carsten-schultz-pix4d |
@edgchen1, thank you. I did not do further tests, but we use very similar changes. |
- Fix CoreML API usage memory leak by putting CoreML API prediction call in an `@autoreleasepool` block as suggested in #14455 and [here](https://developer.apple.com/forums/thread/692425). Conservatively wrapping all CoreML API usage. - Use MLModelConfiguration.computeUnits instead of deprecated MLPredictionOptions.usesCPUOnly (originally in #11382).
…all in an `@autoreleasepool` block as suggested in microsoft#14455 and [here](https://developer.apple.com/forums/thread/692425). Conservatively wrapping all CoreML API usage. - Use MLModelConfiguration.computeUnits instead of deprecated MLPredictionOptions.usesCPUOnly (originally in microsoft#11382). Credits to @edgchen1
- Fix CoreML API usage memory leak by putting CoreML API prediction call in an `@autoreleasepool` block as suggested in microsoft#14455 and [here](https://developer.apple.com/forums/thread/692425). Conservatively wrapping all CoreML API usage. - Use MLModelConfiguration.computeUnits instead of deprecated MLPredictionOptions.usesCPUOnly (originally in microsoft#11382).
Describe the issue
Using the C++ API with the CoreML backend we observed memory leaks, in particular each call to
Ort::Session::Run()
leaked a significant amount of memory.We were able to fix the particular leak with following patch, but the problem does not only affect this one call.
This code calls an Objective C method from Apple's Core ML API. These Objective C methods do not always fully release all of the temporary objects that they have created, they may only add them to the current autorelease pool, to be released when that pool is drained. However, the C++ code calling the
Ort
API has no idea of Objective C's autorelease pools, so none may exist or it may never be drained, and the objects are leaked.Therefore the code in
onnxruntime/core/providers/coreml
which interfaces between C++ and Objective C would have to take care of creating and draining autorelease pools, but it currently doesn't.One solution will be to sprinkle
@autoreleaepool
blocks around all calls to CoreML methods in the two Objective C++ files incoreml/model
. I can certainly prepare a PR that does this, but maybe there is a more elegant solution. I do not normally write Objective C code, and someone here may feel ownership for the CoreML interface code.(We are using 1.12.1, but I have not seen any relevant difference to the main branch.)
To reproduce
No example provided, sorry.
Urgency
No response
Platform
Mac
OS Version
12.6.2
ONNX Runtime Installation
Built from Source
ONNX Runtime Version or Commit ID
1.12.1
ONNX Runtime API
C++
Architecture
ARM64
Execution Provider
CoreML
Execution Provider Library Version
No response
The text was updated successfully, but these errors were encountered: