forked from arangodb/arangodb
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limitMemory.sh
executable file
·33 lines (24 loc) · 714 Bytes
/
limitMemory.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash
# limit memory useage for process
# using control groups https://en.wikipedia.org/wiki/Cgroups
# @author Jan Christoph Uhde
echo "usage: $0 <ram in mb> <swap in mb> command"
# get args
ram="$1"
swap="$2"
group=arango_mem
shift 2
# do not continue if anything goes wrong
set -e
# prepare control groups
sudo -- cgcreate -g "memory:/$group"
#ram
fpath="/sys/fs/cgroup/memory/$group/memory.limit_in_bytes"
sudo bash -c "echo $(( $ram * 1024 * 1024 )) > $fpath "
# swap
fpath="/sys/fs/cgroup/memory/$group/memory.memsw.limit_in_bytes"
if [[ -e $fpath ]]; then
sudo bash -c " echo $(( $swap * 1024 * 1024 )) > $fpath"
fi
#execute
sudo cgexec -g memory:arango_mem su -l -p -c "$@" $USER