Limit process allocation for Mac OS X 10.8

I would like to manage maximum memory, the process can be used in Mac OS X 10.8. I feel that installing ulimit -v should achieve the goals, but it seems to be wrong. I tried the following simple commands:

ulimit -m 512 java -Xms1024m -Xmx2048m SomeJavaProgram 

I assumed that the second command should fail, since the Java Process will start by storing 1024 MB of memory for itself, but it will pass peacefully. In my Sample program, I am trying to allocate more than 1024 MB using the following code snippet:

 System.out.println("Allocating 1 GB of Memory"); List<byte[]> list = new LinkedList<byte[]>(); list.add(new byte[1073741824]); //1024 MB System.out.println("Done...."); 

Both of these programs run without any problems. How can we control the maximum memory allocation for a program on Mac OS X?

+7
source share
1 answer

I'm not sure if you still need an answer to this question, but here is the answer to the case if someone else has the same question.

ulimit -m severely limits resident memory, not the amount of memory that a process may request from the operating system.

ulimit -v limit the amount of virtual memory that a process can request from the operating system.

eg...

 #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { int size = 1 << 20; void* memory = NULL; memory = malloc(size); printf("allocated %i bytes...\n", size); return 0; } 


 ulimit -m 512 ./memory allocated 1048576 bytes... 


 ulimit -v 512 ./memory Segmentation fault 


If you run ulimit -a , it should provide a summary of all current restrictions for child processes.

As mentioned in the comments below @ bikram990, the java process may not abide by soft restrictions. To force the use of Java memory limits, you can pass arguments to the process (-Xmx, -Xss, etc.).

Attention!

You can also set hard limits with the ulimit -H command which cannot be changed by subprocesses. However, these restrictions also cannot be raised again after reduction, without increased permissions (root).

+5
source

All Articles