[erlang-questions] Tail recursion and memory leak

Samuel Rivas <>
Mon Mar 19 08:54:58 CET 2007


Peter Lemenkov wrote:
> 2007/3/16, Christian S <>:
> 
> > You dont need to call lists:append, gen_udp:send will behave as if it
> > calls erlang:iolist_to_binary/1. In fact, we call them iolists because
> > most io functions behaves this way.
> 
> OK, I changed send_message a little:
> 
> ===========================
> send_msg (Fd,Caller,Called,UserName,SessionID,TagTo,TagFrom,Route) ->
>         ok = gen_udp:send(Fd,"127.0.0.1",5060,
>         "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: MyApp\r\n"
>         "CSeq: 123 BYE\r\n"
>         "Route: <" ++ Route ++ ">\r\n"
>         "Content-Length: 0\r\n"
>         "\r\n").

  Just for the record, you are still appending lists (++ an infix
version of lists:append). It is more efficient to create a deep list
(io list) and let the port flatten it:
  gen_udp:send(Fd, "127.0.0.1", 5060,
	       ["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",
		"etc, etc"]).

  Anyway, it must have nothing to do with your huge memory consumption,
two versions worked as expected in my system. i.e. 100% of the CPU, but
not extra memory usage.

Regards
-- 
	Samuel



More information about the erlang-questions mailing list