Monitor Process Memory

3 views
Skip to first unread message

Vance Shipley

unread,
Sep 30, 2021, 7:04:07 AM9/30/21
to Questions erlang-questions
I have a function in user_default which I would like to protect
against naive users consuming all the memory.  It runs a loop building
an accumulator which is a list of records.

I could just use a guard of length(Acc) < ?MAXLEN however I don't
really know how big the records are. I'd like to stop when too much
memory is being used by Acc, or maybe the (shell) process.

Any suggestions?

--
     -Vance

Wojtek Surowka

unread,
Sep 30, 2021, 7:09:15 AM9/30/21
to Questions erlang-questions
You could use process_info(self(), memory) to get the amount of memory used by your process, and react accordingly.

Regards,
Wojtek

Lukas Larsson

unread,
Sep 30, 2021, 7:24:47 AM9/30/21
to Vance Shipley, Questions erlang-questions

Vance Shipley

unread,
Sep 30, 2021, 8:12:24 AM9/30/21
to Lukas Larsson, Questions erlang-questions
On Thu, Sep 30, 2021 at 7:24 PM Lukas Larsson <lu...@erlang.org> wrote:
> Maybe http://erlang.org/doc/man/erlang.html#process_flag_max_heap_size is what you want?

Perfect!

> On Thu, Sep 30, 2021 at 1:04 PM Vance Shipley <van...@motivity.ca> wrote:
>> I'd like to stop when too much memory is being used by Acc, or maybe the (shell) process.

--
-Vance

Mikael Pettersson

unread,
Sep 30, 2021, 9:01:38 AM9/30/21
to Vance Shipley, Questions erlang-questions
On Thu, Sep 30, 2021 at 1:04 PM Vance Shipley <van...@motivity.ca> wrote:
>
> I have a function in user_default which I would like to protect
> against naive users consuming all the memory. It runs a loop building
> an accumulator which is a list of records.
>
> I could just use a guard of length(Acc) < ?MAXLEN

Others have already given alternative solutions. I would just like to
warn that the suggestion above if implemented literally would cause
overheads quadratic on the length of Acc. You can avoid that by
maintaining the length yourself.
Reply all
Reply to author
Forward
0 new messages