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,947
Tummala Dhanvi
  • 1,821
  • 3
  • 21
  • 34

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,947
0

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

Jan
  • 12,291
  • 3
  • 32
  • 38