1

How can I reduce the memory allocated for a given program from the terminal?

what I mean is if I have a a.out executable file, how can I make it run in a less memory from the terminal ( with out using any special applications)

$./a.out fooo

so that my program a.out runs on very less space.

Rmano
  • 31,627
  • 16
  • 118
  • 187
Tummala Dhanvi
  • 1,721
  • 3
  • 20
  • 34
  • possible duplicate of [How to set a memory limit for a specific process?](http://askubuntu.com/questions/510913/how-to-set-a-memory-limit-for-a-specific-process) – muru Aug 26 '14 at 19:21
  • @muru no this is not a duplicate as I need to execute it without any sudo permissions and also any special application installed In other manner I need to eat away the memory – Tummala Dhanvi Sep 08 '14 at 10:57
  • reduce the memory usage with out any special application or permissions – Tummala Dhanvi Sep 08 '14 at 11:08
  • Uh, optimise the program then. Use efficient data structures. Reuse data. Cleanup unused memory. – muru Sep 08 '14 at 11:09
  • I don't have the permission to change the program now unfortunately :( – Tummala Dhanvi Sep 08 '14 at 11:10
  • The program will use what it needs to use. Any means of limiting it, root or otherwise, will just cause it to crash when it needs more than the limit. Is that what you want? – muru Sep 08 '14 at 11:12
  • I want to make it crash by limiting the memory :) as i need to test it on my own virtual machine – Tummala Dhanvi Sep 08 '14 at 11:13

2 Answers2

1

As far as I know, you can't in a strict way (unless using some kind of virtual machine or using cgroups which is not so easy; you can see this answer from @muru.).

You can limit the memory available with ulimit, but this will simply have the effect of telling your program that there is no more memory when doing an allocation, or crashing it with a signal if it doesn't handle the out-of-memory condition. Look:

zcat /var/log/syslog.2.gz 

it works, lot of output

 ulimit -d 100 
 ulimit -m 100

(This is limiting the memory for data and for core to 100 kB)

 [romano:~] 2 % zcat /var/log/syslog.2.gz
 /bin/zcat: xmalloc: .././subst.c:3542: cannot allocate 267 bytes (53248 bytes allocated)

But the shell is still able to see all the memory:

[romano:~] 2 % free                      
             total       used       free     shared    buffers     cached
Mem:      15340736    5148596   10192140     368776     284192    2794848
-/+ buffers/cache:    2069556   13271180
Swap:     31999996          0   31999996

Edit: more info in this unix.se post.

Rmano
  • 31,627
  • 16
  • 118
  • 187
0

You can do this with the ulimit command, here's it's manpage .

Jan
  • 11,856
  • 3
  • 30
  • 38