Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've written a program for a class that my professor will be testing in various low memory environments to see how it behaves when the program runs out of memory. Is there a way I can simulate the execution in a low memory environment without creating a virtual machine?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

You'll want to use ulimit

ulimit can be used to limit memory utilization (among other things)

Here is an example of setting memory usage so low that /bin/ls (which is larger than /bin/cat) no longer works, but /bin/cat still works.

$ ls -lh /bin/ls /bin/cat
-rwxr-xr-x 1 root root 25K May 24 2008 /bin/cat
-rwxr-xr-x 1 root root 88K May 24 2008 /bin/ls
$ date > test.txt
$ ulimit -d 10000 -m 10000 -v 10000
$ /bin/ls date.txt
/bin/ls: error while loading shared libraries: libc.so.6: failed to map segment from shared object: Cannot allocate memory
$ /bin/cat date.txt
Thu Mar 26 11:51:16 PDT 2009
$

Note: If I set the limits to 1000 kilobytes, neither program works, because they load libraries, which increase their size. above 1000 KB.

-d data segment size

-m max memory size

-v virtual memory size

Run ulimit -a to see all the resource caps ulimits can set.

share|improve this answer
    
is there something that does something similar on a single user basis? I don't have the permission to make system wide changes. –  user20222 Apr 24 '10 at 11:51
    
@user20222: You can use ulimit at the command line or in a script in Bash. –  Dennis Williamson Apr 24 '10 at 13:20
    
I'm getting-- ulimit: max memory size: cannot modify limit: Operation not permitted –  user20222 Apr 24 '10 at 13:32
    
What exactly did you try? Post the command line you used –  basszero Apr 25 '10 at 17:12
    
"ulimit -m 1000" and "ulimit -H -m 1000". I also did this: "ulimit -S -m 1000" which didn't error, but it did not enforce the memory limit for the program. –  user20222 Apr 26 '10 at 2:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.