What I'm asking is if it's possible to limit memory (heap or stack) assigned to a specific process, so that this process can't exceed it. Maybe something like "process_flag(min_heap_size, MinHeapSize)", but for the maximum heap.
A process can terminate itself by calling one of the BIFs exit(Reason), erlang:error(Reason), erlang:error(Reason, Args), erlang:fault(Reason) or erlang:fault(Reason, Args). The process then terminates with reason Reason for exit/1 or {Reason,Stack} for the others.
spawn() creates a new process and returns the pid. The new process starts executing in Module:Name(Arg1,...,ArgN) where the arguments are the elements of the (possible empty) Args argument list. There exist a number of different spawn BIFs: spawn/1,2,3,4.
Erlang has something called Process Dictionary which is a sort of Key/Value store that belongs to each Erlang process. We can use it to store values in a simple manner inside Erlang programs while avoiding the limitations of Pure Functional Programming. The anti-flames suit is sold separately tho.
You could put together some kind of process tracking gen_server that periodically checks assigned processes for memory footprint and kills them if it exceeds a certain amount.
Using a combination of process_info(Pid, memory).
and exit(Pid, Reason)
calls, this should be quite manageable.
You could use spawn_opt
with max_heap_size
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With