Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang: How to limit the memory assigned to a process

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.

like image 466
Alex Avatar asked Dec 04 '12 09:12

Alex


People also ask

How do I stop Erlang process?

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.

What does spawn do in Erlang?

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.

What is process dictionary in Erlang?

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.


2 Answers

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.

like image 141
chops Avatar answered Nov 09 '22 23:11

chops


You could use spawn_opt with max_heap_size

like image 33
Maris Orbidans Avatar answered Nov 09 '22 23:11

Maris Orbidans