免费内存作为命令“清除"的功能 [英] free mem as function of command 'purge'

查看:84
本文介绍了免费内存作为命令“清除"的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个应用程序需要释放非活动/已用/有线内存的功能,就像命令清除"一样. 经常检查和搜索谷歌,但都没有成功

one of my app needs the function that free inactive/used/wired memory just like command 'purge'. Check and google a lot, but can not get any hit

欢迎发表评论

推荐答案

Purge并没有按照您认为的做.它不会释放不活动/已使用/有线的内存".如联机帮助页所述:

Purge doesn't do what you seem to think it does. It doesn't "free inactive/used/wired memory". As the manpage says:

它不会影响通过malloc,vm_allocate等分配的匿名内存.

It does not affect anonymous memory that has been allocated through malloc, vm_allocate, etc.

它所做的就是清除磁盘缓存.仅当您正在运行性能测试并且想要模拟冷启动后首次运行"的效果而不实际进行冷启动时,此功能才有用.再次,从联机帮助页:

All it does is purge the disk cache. This is only useful if you're running performance tests and want to simulate the effects of "first run after cold boot" without actually cold booting. Again, from the manpage:

Purge可用于通过冷盘缓冲区高速缓存来估计初始引导条件,以进行性能分析.

Purge can be used to approximate initial boot conditions with a cold disk buffer cache for performance analysis.

尽管对此符号进行了快速扫描,但似乎没有公共API,但它似乎从CoreProfile私有框架中调用了函数CPOSXPurgeAllDiskBuffers.我相信底层内核和用户区磁盘缓存代码在 http://www.opensource.apple.com上全部或大部分可用,因此您可以根据需要自己实现相同的功能.

There is no public API for this, although a quick scan of the symbols shows that it seems to call a function CPOSXPurgeAllDiskBuffers from the CoreProfile private framework. I believe the underlying kernel and userland disk cache code is all or mostly available on http://www.opensource.apple.com, so you could do probably implement the same thing yourself, if you really want.

如iMysak所说,您可以根据需要执行(或执行NSTask等)该工具.

As iMysak says, you can just exec (or NSTask, etc.) the tool if you want to.

作为一个附带说明,它可以释放已用/有线的内存,大概是内存被某物使用了-即使您自己的数据结构中没有指向它的指针,malloc也可能会使用.您是否要对您的代码进行段验证?

As a side note, it you could free used/wired memory, presumably that memory is used by something—even if you don't have pointers into it in your own data structures, malloc probably does. Are you trying to segfault your code?

释放不活动的内存是另一回事.仅仅释放一些东西给malloc并不一定会使malloc将其返回给OS.而且没有办法可以强制它.如果您考虑一下传统UNIX的工作方式,那是有道理的:当您要求它分配更多的内存时,它使用sbrk扩展您的数据段.如果在顶部释放内存,则可以向下猛击,但是如果在中间释放内存,则无法做到这一点.当然,现代的UNIX系统不能那样工作,但是POSIX和C API都被设计为与可兼容的系统兼容.因此,如果要确保释放内存,则必须直接处理内存分配.

Freeing inactive memory is a different story. Just freeing something up to malloc doesn't necessarily make malloc return it to the OS. And there's no way you can force it to. If you think about the way traditional UNIX works, it makes sense: When you ask it to allocate more memory, it uses sbrk to expand your data segment; if you free up memory at the top, it can sbrk back down, but if you free up memory in the middle, there's no way it can do that. Of course modern UNIX systems don't work that way, but the POSIX and C APIs are all designed to be compatible with systems that do. So, if you want to make sure memory gets freed, you have to handle memory allocation directly.

最简单,最可移植的方法是创建并映射一个临时后备文件(或仅MAP_ANON),并在完成处理后显式取消映射页面. (这适用于所有POSIX系统,并且使用非常简单的包装程序,甚至适用于Windows.)如果您需要更多控制权(例如,手动将刷新页面处理到磁盘等),则可以使用mach/mach_vm.h. API.

The simplest and most portable way to do this is to create and mmap a temporary backing file, or just MAP_ANON, and explicitly unmap pages when you're done with them. (This works on all POSIX systems—and, with a pretty simple wrapper, even Windows.) If you need even more control (e.g., to manually handle flushing pages to disk, etc.), you can use the mach/mach_vm.h APIs.

这篇关于免费内存作为命令“清除"的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆