Mac Ulimit doesn't work (memory)

dzwiedziu

macrumors newbie
Original poster
Oct 2, 2008
4
0
I've wrote sample program in C++:

#include<cstdio>
#include<cstdlib>
long long T[5000000];
int main(){
for(int i=0;i<5000000;i++) T=rand()%5000000;
for(int i=0;i<5000000;i++) T=T[rand()%5000000];
printf("%lld\n",T[rand()%5000000]);
return 0;
}

It uses memory: ~40MB

But when I check it in terminal:


dzwiedziu:~/test_ulimit # ulimit -v 1000
dzwiedziu:~/test_ulimit # ./test
1480040
dzwiedziu:~/test_ulimit # ulimit -H -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) 1000
open files (-n) unlimited
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 65532
cpu time (seconds, -t) unlimited
max user processes (-u) 532
virtual memory (kbytes, -v) 1000


This program should be killed. I've worked on linux for 4 years. What's wrong?
 

gnasher729

macrumors P6
Nov 25, 2005
16,948
3,830
I've wrote sample program in C++:

#include<cstdio>
#include<cstdlib>
long long T[5000000];
int main(){
for(int i=0;i<5000000;i++) T=rand()%5000000;
for(int i=0;i<5000000;i++) T=T[rand()%5000000];
printf("%lld\n",T[rand()%5000000]);
return 0;
}

It uses memory: ~40MB

But when I check it in terminal:


dzwiedziu:~/test_ulimit # ulimit -v 1000
dzwiedziu:~/test_ulimit # ./test
1480040
dzwiedziu:~/test_ulimit # ulimit -H -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) 1000
open files (-n) unlimited
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 65532
cpu time (seconds, -t) unlimited
max user processes (-u) 532
virtual memory (kbytes, -v) 1000


This program should be killed. I've worked on linux for 4 years. What's wrong?


Man Page: "ulimit: Control the resources available to a process started by the shell, on systems that allow such control."

Seems to do exactly what the man page says, and seems that MacOS X is not a system that allows such control, or maybe it has a default that you can't go below (like 64 MB) or whatever. Feel free to file a bug report with Apple. They'll probably tell you "works as designed".
 

dzwiedziu

macrumors newbie
Original poster
Oct 2, 2008
4
0
Where are you getting the 40Mb figure from? If it's VSize in Activity Monitor then you're looking at it wrong...
I looked in Activity Moniton on Real Memory. But it's easy to count long long need 8 bytes so this array need 5000000 * 8 bytes.

When I set limit on 1kb I've the same effect:
dzwiedziu:~/test_ulimit # ulimit -v 1
dzwiedziu:~/test_ulimit # ./test
1480040
dzwiedziu:~/test_ulimit # ulimit -H -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) 1
open files (-n) unlimited
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 65532
cpu time (seconds, -t) unlimited
max user processes (-u) 532
virtual memory (kbytes, -v) 1
 

foidulus

macrumors 6502a
Jan 15, 2007
904
1
Man Page: "ulimit: Control the resources available to a process started by the shell, on systems that allow such control."

Seems to do exactly what the man page says, and seems that MacOS X is not a system that allows such control, or maybe it has a default that you can't go below (like 64 MB) or whatever. Feel free to file a bug report with Apple. They'll probably tell you "works as designed".
It worked in Tiger, however it has been removed from Leopard, probably because it was hard to integrate with some of the memory randomization schemes Apple implemented with Leopard.
 

dzwiedziu

macrumors newbie
Original poster
Oct 2, 2008
4
0
It worked in Tiger, however it has been removed from Leopard, probably because it was hard to integrate with some of the memory randomization schemes Apple implemented with Leopard.

Thx. Maybe you know, is any similary program to ulimit on Leopard, or any way to kill procces when it use to much memory.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.