可以部分释放一块内存吗?

发布于 2024-11-23 15:21:42 字数 199 浏览 6 评论 0原文

我正在编写一个内存密集型程序,该程序将有数十个 malloc 数组。当应用程序收到内存不足警告时,我想转储每个数组的下半部分。有什么办法可以做到这一点吗?

我需要某种方法来保留每个数组中一半的内存。显然,如果应用程序内存不足,我无法分配一个较小的数组,将一半的数据复制到其中,然后释放旧数组。是否有任何函数可以释放从指针 A 开始到指针 B 结束的内存块或类似的东西?

I'm writing a very memory intensive program that will have dozens of malloc'd arrays. When the app receives a low memory warning, I want to dump the lower half of each of these arrays. Is there any way to do this?

I need some way that I can preserve half of the memory in each array. Obviously, if the app has low memory, I can't allocate a smaller array, copy half of my data into it, and then free the old array. Is there any function that can free a block of memory starting at pointer A and ending at pointer B or something like that?

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

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

发布评论

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

评论(2

恋你朝朝暮暮 2024-11-30 15:21:42

Realloc() 可以将 malloc 内存的尾部部分返回到分配池,但不能将其返回到操作系统。

Realloc() 也无助于解决内存碎片问题,这在内存不足的情况下可能是一个问题。

Realloc() can return the trailing portion of the memory of a malloc back to the allocation pool, but can't return it to the OS.

Realloc() also won't help with memory fragmentation, which is likely a problem in a low-memory situation.

眼眸 2024-11-30 15:21:42

如果它们是 NSMutableArrays,您可以用 [NSNull null] 的单个实例替换低端的对象,从而释放所有这些对象。

NSNull 类参考

If they are NSMutableArrays, you can replace the objects in the lower end with a single instance of [NSNull null], thereby releasing all of those objects.

NSNull Class Reference

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