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.

7
  • possible duplicate of How to set a memory limit for a specific process?
    – muru
    Aug 26, 2014 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 Sep 8, 2014 at 10:57
  • reduce the memory usage with out any special application or permissions Sep 8, 2014 at 11:08
  • Uh, optimise the program then. Use efficient data structures. Reuse data. Cleanup unused memory.
    – muru
    Sep 8, 2014 at 11:09
  • I don't have the permission to change the program now unfortunately :( Sep 8, 2014 at 11:10

2 Answers 2

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.

0

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

5
  • Can you give me a example of how to do it exactly :) Aug 26, 2014 at 14:27
  • ulimit is outdated (says my manpage) and refers to setrlimit. @dhanvi: man setrlimit
    – PythoNic
    Aug 26, 2014 at 14:32
  • @PythoNic I need the exact command to run it on the terminal not in the code Aug 26, 2014 at 14:36
  • 1
    ulimit is also a bash builtin, see manpages.ubuntu.com/manpages/trusty/man1/bash.1.html
    – Jan
    Aug 26, 2014 at 16:32
  • Can we do it without any sudo permissions and also any special application installed In other manner, I need to eat away the memory, don't suggest me fork bomb as it doesn't really satisfies my needs Sep 8, 2014 at 10:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .