[erlang-questions] Increase in heap size
Evans, Matthew
mevans@REDACTED
Fri Dec 18 19:18:40 CET 2009
Hi,
First, don't worry about the number of reductions. That's just telling you that your process is doing something.
As a general coding style, most programmers would frown upon having case statements that are nested more than 2 deep (some even complain about 1 deep). You should think about calling other functions within those case statements, and maybe making use of guards too.
What I would also recommend is removing the tail - recursive call to start() at the end of the start() function, and instead ensure that the recursive call is made at the end of each branch in the case statement(s).
foo(Arg1, Arg2) ->
case Arg1 of
init ->
{Res1,Res2} = do_something(Arg1, Arg2),
foo(Res1,Res2);
_ ->
foo(Arg1,Arg2)
end.
A big no-no is that you have no default case in the receive statement. This WILL cause an explosive growth of the heap since messages that don't match will remain on the message queue.
________________________________
From: maruthavanan s [mailto:maruthavanan_s@REDACTED]
Sent: Friday, December 18, 2009 10:51 AM
To: Evans, Matthew
Subject: RE: [erlang-questions] Increase in heap size
Hi,
Thanks for your reply,
I am developing a TCP client which receives data from a server and processes it in the start module.
I have attached the entire code with this mail.
Help me to optimize because it runs for more than 10 hrs and memory consumption is growing leading to degradation.
Thanks,
Marutha
> From: mevans@REDACTED
> To: maruthavanan_s@REDACTED
> Date: Fri, 18 Dec 2009 08:17:25 -0500
> Subject: RE: [erlang-questions] Increase in heap size
>
> I guess we would need to see what process_data/1 is doing to answer honestly.
>
> My guess is that that function is calling start() within its body. If so then you have something that will almost certainly cause the heap to grow.
>
> What you should do is:
>
> start()->
> receive
> {data,Data}->
> process_data(Data),
> start();
> _ ->
> start()
> end.
>
> And ensure process_data/1 returns rather than calls start() - although with the mods above that isn't so important now.
>
> ________________________________________
> From: erlang-questions@REDACTED [erlang-questions@REDACTED] On Behalf Of maruthavanan s [maruthavanan_s@REDACTED]
> Sent: Friday, December 18, 2009 4:32 AM
> To: erlang-questions@REDACTED
> Subject: [erlang-questions] Increase in heap size
>
> Hi,
>
> I have a old code written by some one else, this is not a OTP,
>
> start()->
> receive
> {data,Data}->
> process_data(Data);
> end,
> start().
>
> After I run my system this process heap size is increase to enourmous extent with bigger values of reductions.
>
> I could not understand reductions. Is this not a good programing practice? What would be the right way? what should I do to optimize this?
>
> Please help.
>
> Regards,
> Marutha
More information about the erlang-questions
mailing list