[erlang-questions] Memory leak question

Paul Oliver <>
Mon Sep 22 17:06:25 CEST 2008


On Mon, Sep 22, 2008 at 4:01 PM, Sverker Eriksson
<> wrote:
> Do you use any packet-option on the socket. Otherwise you don't have any
> guarantee that you will receive all the echoed data in one message. That is,
> you will send more data than you receive as you have a 1-to-1 relation
> between send and receive in your loop.
>
> /Sverker, Erlang/OTP Ericsson
>
>
>
>

Hi Sverker,

I've used {packet, 0}.  Please find the full source below.

Cheers,
Paul.

-module(load_test).

-export([start/1, init/1]).

-define(RETRY_WAIT, 1000).
-define(MESSAGE_INTERVAL, 1000).
-define(CLIENT_START_WAIT, 2).
-define(SOCK_OPTS, [binary, {packet, 0}, {recbuf, 1000}, {sndbuf, 1000}]).

start(1) ->
	spawn(?MODULE, init, [true]),
	ok;
start(Clients) ->
	spawn(?MODULE, init, [false]),
	receive
	after ?CLIENT_START_WAIT ->
			true
	end,
	start(Clients - 1).

init(Log) ->
	case gen_tcp:connect('127.0.0.1', 8888, ?SOCK_OPTS, 30000) of
		{ok, Sock} ->
			loop(Sock, Log);
		{error, X} ->
			% If we get a connect error, wait and reconnect
			error_logger:info_msg(X),
			receive
			after ?RETRY_WAIT ->
					true
			end,
			init(Log)
	end.

loop(Sock, Log) ->
	Before = if Log -> now();
				true -> 0
			 end,
	ok = gen_tcp:send(Sock,
<<"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789">>),
	receive
		{tcp, Sock, _Data} ->
			case Log of true ->
					[{total, Total}, {processes, Processes} , _, _, _, _, {binary,
Binary}, _, _] = erlang:memory(),
					error_logger:info_msg("tot_mem: ~p, binary: ~p, procs: ~p, time: ~p",
										  [Total, Binary, Processes, timer:now_diff(now(),Before)/1000]);
				_ -> true
			end;
		Error ->
			% Anything other than a data message, print error
			error_logger:error_msg(Error)
	end,
	receive
	after ?MESSAGE_INTERVAL ->
			true
	end,
	loop(Sock, Log).



More information about the erlang-questions mailing list