0

In my recently experiment, I need a program that consumes certain amount of memory. I want to implement it in bash script, say, I want this script runs as a daemon and consumes around 200mb physical memory. How to design this script?

If it's possible, I hope it can be run without permission.

hakunami
  • 2,351
  • 4
  • 31
  • 50
  • 2
    did you see [this](http://stackoverflow.com/questions/4964799/write-a-bash-shell-script-that-consumes-a-constant-amount-of-ram-for-a-user-defi) SO? – Ethaan Jan 29 '15 at 07:58
  • 1
    While it's possible to "allocate" a certain amount of memory using shell scripts, it's not possible to force it to be *physical* memory (as in locked to actual RAM) in a shell script. There probably are system calls that can do that but those are not available through normal commands. – Some programmer dude Jan 29 '15 at 08:03

1 Answers1

2

Seems like this is what you looking for

mntroot rw
 cd /dev
 while : 
 do 
        dd > /dev/null 2>&1 if=/dev/zero of=myfile1 count=20000 bs=1024 # use 200MB ram
        usleep 1 
        rm myfile1

 done
Ethaan
  • 11,291
  • 5
  • 35
  • 45
  • You can also use `count=1 bs=200M` –  Jan 29 '15 at 08:43
  • Thanks JID thanks for the advice, @hakunami did you make it work? – Ethaan Jan 29 '15 at 08:44
  • @Ethaan Since I am working on a distributed system, and many workers may run this script on the same node. I am wondering if there is any write conflict possible in my case? – hakunami Jan 30 '15 at 02:29
  • Well i was never in that situation, but it shouldn't, i will be laying if a tell you yes or not, it will be nice if you test it and tell me if some issue appears. – Ethaan Jan 30 '15 at 02:43
  • its to mount the fyle on the system root and make it accessible – Ethaan Jan 30 '15 at 03:55
  • @Ethaan Some questions want to be clear. 1. seems I need `sudo` to run script ? 2. I use `sudo mount -o remount,rw /` to make `/` read and write, am I right? 3. I use `pmap` to check memory usage, but all numbers are 0, something wrong? – hakunami Jan 30 '15 at 04:17