绑定内存时,内存泄漏
我遇到了Mac上Swift中的记忆泄漏。我正在使用金属在GPU上创建一个缓冲区。除非我将内容绑定到内存,否则为这些创建的存储将自动删除。
在这种情况下,即使缓冲区和绑定指针都超出范围,也不会删除内存。
我尝试手动处理缓冲区,但是由于没有使用Malloc分配内存,但这会失败。
有没有办法管理此内存以避免泄漏,还是Mac上的Swift中的错误?
还有其他想法吗?
非常感谢,
科林
let intensityBuff = myGPUData.device?.makeBuffer(length: MemoryLayout<Float>.stride * Int(myStars.nstars * myStars.npatch * myStars.npatch, options: .storageModeShared)
let intensityPointer = intensityBuff?.contents().bindMemory(to: Float.self, capacity: MemoryLayout<Float>.stride * Int(myStars.nstars * myStars.npatch * myStars.npatch))
I've come across a memory leak in Swift on the Mac. I'm creating a buffer for a calculation on the GPU using Metal. The storage created for these is automatically deleted when they are out of scope UNLESS I bind the contents to memory.
In this case, the memory is not deleted even when both the buffer and the bound pointer are out of scope.
I tried manually deallocating the buffer, but this fails since the memory was not allocated using malloc.
Is there a way to manage this memory to avoid a leak, or is this a bug in Swift on the Mac?
Any other thoughts?
Thank you very much,
Colin
let intensityBuff = myGPUData.device?.makeBuffer(length: MemoryLayout<Float>.stride * Int(myStars.nstars * myStars.npatch * myStars.npatch, options: .storageModeShared)
let intensityPointer = intensityBuff?.contents().bindMemory(to: Float.self, capacity: MemoryLayout<Float>.stride * Int(myStars.nstars * myStars.npatch * myStars.npatch))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
金属缓冲区需要具有标志
mtlpurgeablestate.empty
设置的设置,以表明使用它们后可以清除它们。例如:Metal buffers need to have the flag
MTLPurgeableState.empty
set to indicate they can be cleared out of memory after you are done using them. For example:事实证明,问题在于,并非所有苹果的代码都已移植到Swift -BindMemory命令实际上是用Objective -C编写的。
对于这些功能,ARC无法自动工作 - 相反,您需要在AutoreLeAsepool块中包含代码,例如,
我尚未测试MTLPURGEABLESTATE。 。
当然,Apple不会告诉您哪些功能是基于客观-C的 - 至少我无法找到!
It turns out that the issue is that not all of Apple's code has been ported to Swift - the bindMemory command is actually written in Objective-C.
For these functions, ARC doesn't work automatically - instead you need to include the code within an autoreleasepool block e.g.
I haven't tested out the MTLPurgeableState.empty suggested above by Nate (thank you for that suggestion), since the autorelease works fine.
And of course, Apple doesn't tell you which of the functions are Objective-C based - at least not that I could find!