为什么垃圾收集不适用于线程?

发布于 2024-10-18 07:38:08 字数 591 浏览 3 评论 0原文

在我的应用程序中,一切都工作正常,直到我分离线程以从文件中读取音频数据并对其进行处理。

无论我做什么,垃圾收集似乎都不知道该线程上发生的情况。我尝试过[NSThread self]、[NSThread currentThread]、[[NSGarbageCollector defaultCollector]collectIfNeeded]和collectExhaustivly]。 我从 NSThread 切换到 NSOperation 和 NSOperationQueue。

这些建议的解决方案均无效。最近,我从 NSMutableArrays 切换到

Float32* pfArray= calloc(numFloats, sizeof(Float32));

来保存我的数据,并使用 免费(pfArray); 释放该内存。这工作得更好,但仍然泄漏了相当多的量。

在活动监视器中指示的“真实内存”达到某个任意数字后,垃圾收集似乎确实在某种程度上开始工作,但当它确实开始工作时,它不会释放所有正在使用的内存。它只是不让它远高于任意阈值。

我读过 GC 是可行的方法,但现在我不确定并且有一个几乎完全编写的程序。任何建议都会非常有帮助。谢谢你!

Every thing works fine in my app up until I detach a thread to read audio data out of a file and process it.

Garbage Collection seems to not know anything about what happens on this thread no matter what I do. I've tried [NSThread self], [NSThread currentThread], [[NSGarbageCollector defaultCollector]collectIfNeeded] and collectExhaustivly].
I switched to NSOperation with NSOperationQueue from NSThread.

None of these suggested solutions have worked. Most recently, I switched from NSMutableArrays to

Float32* pfArray= calloc(numFloats, sizeof(Float32));

to hold my data, and used
free(pfArray);
to free that memory. This is working better, but still leaking a decent amount.

Garbage Collection does seem to start working to some extent after the "real mem" indicated in Activity Monitor hits some arbitrary number, but when it does appear to be working, it does NOT free all the memory being used. It just doesn't let it go much higher than the arbitrary threshold.

I've read that GC is the way to go, but now I'm unsure and have an almost fully written program. Any suggestions would be very helpful. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

三生路 2024-10-25 07:38:08

垃圾收集确实适用于应用程序的所有线程。您可能无意中保留了对某个根对象的引用,从而泄漏了子图;然而,如果没有原始代码,就没什么可说的了。

Garbage collection indeed works on all threads of the app. You might have unwittingly kept a reference to some object rooted, thus leaking a subgraph; without the original code, however, not much can be said.

毅然前行 2024-10-25 07:38:08

collectIfNeeded”表示如果不需要收集,则不会收集。

使用 Instruments 的 Heapshot 功能(分配工具的一部分)来找出哪些对象仍然处于活动状态,并使用其对象图工具找出仍然保留着您认为不再需要的对象的内容。

collectIfNeeded” implies that it will not collect if collection isn't needed.

Use Instruments's Heapshot feature (part of the Allocations instrument) to find out what objects are remaining alive, and its Object Graph instrument to find out what is still holding on to the objects that you think should no longer be needed.

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