Reserve resources (memory and processes)

I have a shell script which sets some variables and then calls modules of a program in succession, one by one. Problem is that the script is executed on servers with many users, so sometimes the script starts running, runs for 10 minutes and then breaks due to lack of resources when other users run memory intensive processes.

So, I have an assignment to somehow reserve enough memory when the script is started, so that memory problems are avoided. Also, is there a way to reserve certain number of processes? I don't need a complete solution, just an idea to get started is enough.

From the sounds of it you're on Linux, and running into the OOM-killer when things get tight. Correct me if I'm wrong.

Preallocating memory won't prevent the OOM-killer from hitting you, if anything, hogging more memory and more processes will put you higher in its list. Band-aiding a fix onto your scripts won't stop the server from running out of memory anyway, at best it'll deflect the OOM-kill to something else. What would they rather have die, your scripts, the memory-intensive processes, some daemon, or things more system-critical? They need to add more memory and/or swap, or to start restricting the stupid users that keep bombing the system.

One way to "preallocate" memory in a shell would be to make and clear a large shell variable, I suppose, but that's subject to the max size of a shell variable in your shell and all kinds of hackish and ugly and unlikely to work:

MEMHOG="$(dd if=/dev/zero count=1024 bs=1024 2> /dev/null | tr '\000' 'a')"
MEMHOG=""