Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit memory of a OS X program? ulimit -v neither -m are working

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.

like image 883
hectorpal Avatar asked Jul 18 '10 04:07

hectorpal


People also ask

What is Ulimit memory?

On Linux, ulimit allows you to limit the resources that a process can use. Two use cases: You have a program that sometimes runs out of memory, slowing your computer down to a crawl. You can use ulimit -v to limit the amount of memory that processes in a shell can use.


1 Answers

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.

Why? Because the RLIMIT_DATA and RLIMIT_AS options to setrlimit() don't actually do anything in current versions of XNU (the macOS kernel).

like image 101
al45tair Avatar answered Sep 28 '22 16:09

al45tair