Alex. Smith
Alex. Smith

Reputation: 61

Erlang message queue clutter

I'm not quite sure how to go about finding the answer to this type of question. In Erlang each process has a message queue. There is the possibility that a message sent to a process may not match any pattern of that specific process. If that's the case the process leaves the message in the queue and goes on to check the other messages. My question is:

Doesn't this create a minor memory leak?

Hypothetically a process could continue to receive messages it can't match and grow and grow and grow eventually causing an issue. How does Erlang deal with this situation? I know Erlang has an implementation for timeouts, but if these messages didn't have a timeout would this cause a problem? Is there any sort of default garbage collection?

Upvotes: 2

Views: 939

Answers (1)

7stud
7stud

Reputation: 48649

Doesn't this create a minor memory leak?

Yes, a selective receive can cause a process to run out of memory.

How does Erlang deal with this situation?

receive 
    Pattern1 -> %% do something;
    Pattern2 -> %% do something else;
    Other -> ok
end

Or, after some time passes:

myflush() ->
    receive 
        _Any -> myflush()
    after 0 ->  % If at any time there are no messages in the mailbox, this executes.
        ok
    end.

Upvotes: 4

Related Questions