绑定内存时,内存泄漏

发布于 2025-02-05 12:43:31 字数 669 浏览 3 评论 0原文

我遇到了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤慢歌 2025-02-12 12:43:31

金属缓冲区需要具有标志mtlpurgeablestate.empty设置的设置,以表明使用它们后可以清除它们。例如:

intensityBuff!.setPurgeableState(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:

intensityBuff!.setPurgeableState(MTLPurgeableState.empty)
看轻我的陪伴 2025-02-12 12:43:31

事实证明,问题在于,并非所有苹果的代码都已移植到Swift -BindMemory命令实际上是用Objective -C编写的。

对于这些功能,ARC无法自动工作 - 相反,您需要在AutoreLeAsepool块中包含代码,例如,

autorelease {
code with bindMemory statement in 
}

我尚未测试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.

autorelease {
code with bindMemory statement in 
}

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文