I want to limit the memory usage of a child process using rlimit. Currently our code is as follows:
old_rlimit := get_rlimit()
set_rlimit(child_process_rlimit)
cmd.Start()
set_rlimit(old_rlimit)
cmd.Wait()
However, sometimes Golang runtime will report out of memory error at cmd.Start(). It seems that in cmd.Start() current process will allocate some memory, and if current memory usage is higher than child_process_rlimit, an error will be raised.
I want to know is there any way to limit the memory usage of child process without affecting current one?
You need to apply
rlimitto the child process only rather than relying on rlimit inheritance. Your main blocker here is clearly spelled out in thesetrlimitman page:The standard way to do this is through fork/exec, something along the lines of:
If you don't want to do that, you still have a few options:
cmd.Runa small wrapper (eg:bash ulimit -m ...) that calls the child process. note:-mis not honored by many systems.prlimit(linux-specific and no syscall wrapper. see this thread for details)