如何限制 OS X 程序的内存?ulimit -v 都没有 -m 工作 [英] How to limit memory of a OS X program? ulimit -v neither -m are working
问题描述
我的程序有一半的时间会耗尽内存.在 Linux 下,我可以使用 ulimit -v mem-in-kbytes 对可用内存设置硬限制.实际上,我使用了 ulimit -S -v mem-in-kbytes,所以我在程序中遇到了正确的内存分配问题,我可以中止.
My programs run out of memory like half of the time I run them. Under Linux I can set a hard limit to the available memory using ulimit -v mem-in-kbytes. Actually, I use ulimit -S -v mem-in-kbytes, so I get a proper memory allocation problem in the program and I can abort.
但是... ulimit 在 OSX 10.6 中不起作用.我试过 -s 和 -m 选项,但它们不起作用.
But... ulimit is not working in OSX 10.6. I've tried with -s and -m options, and they are not working.
2008 年,在 MacRumors 中有一些关于同一问题的讨论,但是没有人提出一个好的替代方案.这应该是程序可以了解它消耗太多内存或通过操作系统设置限制的一种方式.
In 2008 there was some discussion about the same issue in MacRumors, but nobody proposed a good alternative. The should be a way a program can learn it's spending too much memory, or setting a limit through the OS.
推荐答案
你不能.Apple 可以(使用 ledger()
系统调用,这是私有的),但你不能.我不完全确定 launchd 的选项是否有效 - 当然如果它仍在使用上一个开源版本(从 10.9.5 开始)中可见的代码,它不会,因为它调用 setrlimit()
,但从那时起它已经被大量重写,尽管我看不到它调用 ledger()
,如果这应该起作用,我希望它会调用.
You can't. Apple can (using the ledger()
system call, which is private), but you can't. I'm not entirely sure whether launchd's options work or not - certainly if it was still using the code visible in the last open source version (from 10.9.5), it wouldn't, because it calls setrlimit()
, but it's been substantially rewritten since then, though I can't see it calling ledger()
, which I'd expect it to if this was supposed to work.
为什么?因为setrlimit()
的RLIMIT_DATA
和RLIMIT_AS
选项实际上在当前版本的XNU 中没有做任何事情(macOS 内核).
Why? Because the RLIMIT_DATA
and RLIMIT_AS
options to setrlimit()
don't actually do anything in current versions of XNU (the macOS kernel).
这篇关于如何限制 OS X 程序的内存?ulimit -v 都没有 -m 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!