可以部分释放一块内存吗?
我正在编写一个内存密集型程序,该程序将有数十个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
如果它们是 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