[erlang-questions] Tail recursion and memory leak

Peter Lemenkov lemenkov@REDACTED
Fri Mar 16 11:55:47 CET 2007


Hello All!
I write simple example of tail recursion:

==================
-module(test).
-export([go/0]).

go () ->
        case gen_udp:open(3456) of
                {ok, Fd} ->
                        select_calls(Fd,0),
                        gen_udp:close(Fd);
                Error ->
                        io:format("Error creating socket\n", []),
                        exit(Error)
        end.

select_calls(Fd,NumberOfCalls) ->
        io:format("Call number ~w~n~n",[NumberOfCalls]),
        send_msg
(Fd,"Caller","Called","UserName","SessionID","TagTo","TagFrom","Routeto"),
        send_msg
(Fd,"Called","Caller","UserName","SessionID","TagFrom","TagTo","RouteFrom"),
        select_calls(Fd,NumberOfCalls+1).

send_msg (Fd,Caller,Called,UserName,SessionID,TagTo,TagFrom,Route) ->
       ok = gen_udp:send(Fd,"127.0.0.1",5060, "Hello All\n\r").

==================

No memory leaks - all worked as I want.
However then I try to change function send_msg to something more
valuable, for example:

==================
       Msg = lists:append([    "BYE sip:127.0.0.1:5060 SIP/2.0 \r\n",
                               "Via: SIP/2.0/UDP 127.0.0.1:3456\r\n",
                               "From: <", Caller, ">;tag=", TagFrom, "\r\n",
                               "To: <",  Called, ">;tag=", TagTo, "\r\n",
                               "Call-ID: ", SessionID, "\r\n",
                               "User-Agent: My User Agent\r\n",
                               "CSeq: 123 BYE\r\n",
                               "Route: <", Route, ">\r\n",
                               "Content-Length: 0\r\n",
                               "\r\n"]),
%       io:format("~s~n", [Msg]).

        ok = gen_udp:send(Fd,"127.0.0.1",5060, [Msg]).

==================

I've got a huge memory consumption.
Looks like I missed something important about string manipulating but
I can't find out what exactly.

Can anyone explain me what I'm doing wrong?

-- 
With best regards!



More information about the erlang-questions mailing list