Ulimit doesn't work (memory)

Discussion in 'Mac Programming' started by dzwiedziu, Oct 2, 2008.

  1. macrumors newbie

    Joined:
    Oct 2, 2008
    #1
    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?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Where are you getting the 40Mb figure from? If it's VSize in Activity Monitor then you're looking at it wrong...
     
  3. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #3


    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".
     
  4. thread starter macrumors newbie

    Joined:
    Oct 2, 2008
    #4
    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
     
  5. macrumors 6502a

    Joined:
    Jan 15, 2007
    #5
    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.
     
  6. thread starter macrumors newbie

    Joined:
    Oct 2, 2008
    #6

    Thx. Maybe you know, is any similary program to ulimit on Leopard, or any way to kill procces when it use to much memory.
     

Share This Page