问题描述
是否可以分配大块内存(安装了4Gb的512Mb-1Gb)而不将其丢弃以交换内存?
Is it possible to allocate big chunk (512Mb-1Gb with 4Gb installed) of real memory without dropping it to swap?
我的最终目的是释放系统内存:可用内存接近0时,os x存在一个问题-它不会尝试将未使用的内存进行交换,而尝试获取内存时一切都会变得很慢.因此,我决定,如果某个进程强行抢占了一些内存(我抢占了系统总内存的1/8至1/4),然后释放了内存,那么系统将再次负责任.我写了一些小红宝石脚本来创建和填充内存磁盘(hdiutil
是我发现使用外部工具获取真实内存的唯一方法),现在我决定只用C重写它.
My final intention is to free system memory: os x has a problem when free memory is near 0 — it doesn't try hard to put unused memory to swap and everything becomes very slow trying to get memory. So, I decided that if some process grabs some memory (I grab 1/8 to 1/4 of total system memory) by force, and than frees it, than system will become responsible again.I wrote little ruby script which created and filled memory disk (hdiutil
is the only way I found to grab real memory using external utils) and now I decided to rewrite it using just C.
推荐答案
mlock 函数会将页面锁定到物理内存中.我不确定会有什么限制.
The mlock function will lock pages into physical memory. I'm not sure what the limits are.
您应该非常确定自己确实要这样做.对系统的其余部分而言,这不是一件好事.
You should be extremely sure you really want to do this. It's not a nice thing to do to the rest of the system.
这篇关于强制分配实内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!