views:

55

answers:

1

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.

But... ulimit is not working in OSX 10.6. I've tried with -s and -m options, and they are not working.

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.

+1  A: 

setrlimit should do the job. I believe that's the BSD equivalent of ulimit...

Yuji
I looked at the man page for `setrlimit`, and couldn't see how to set a limit on virtual memory, though you can set a limit on physical memory.
JWWalker
Ugh, you're right ...
Yuji
I know bash ulimit is implemented directly calling setrlimit. In Linux man page there is the option RLIMIT_AS, that limits "the maximum size of the process’s virtual memory (address space) in bytes", that is what I want to control. I really don't care about the limit of physical memory. I want to know when the whole program is over 2Gb.Well, on OS X manpage for setrlimit, there is RLIMIT_AS. The nearest is RLIMIT_RSS.Indeed, IMHO OS X seems to allocate a lot of virtual memory, given what I see in the Activity Monitor.
hectorpal
Mmm... SUSv3 demands `RLIMIT_AS`, (see http://www.opengroup.org/onlinepubs/009695399/functions/getrlimit.html) and OS X sells as an SUSv3 UNIX, so it should support `RLIMIT_AS`. Indeed `<sys/resource.h>` defines `RLIMIT_AS`, although it's not mentioned in the man page. Could you try `RLIMIT_AS` to see if it's really implemented?
Yuji
It seems I should try... Thanks.
hectorpal