[erlang-questions] garbage collection questions

Doug Currie <>
Mon Oct 1 23:37:14 CEST 2007

```Hi,

Digging into Erlang a bit, I was dismayed by the use of get & put in
the shootout benchmark nsieve
http://shootout.alioth.debian.org/gp4/benchmark.php?test=nsieve&lang=hipe&id=0

I noted the use of ets and then hipe bytearray in
http://www.nabble.com/shootout---nsieve-bits-benchmark-t1487923.html

But I am running on Windows, so no HiPE :-(

Evaluating the use of integers to replace bytearray just for fun (I
was not expecting it to be competitive), I ran into GC problems.

Here is the code:

main(N) when N >= 2 -> gcst(), ns(N), gcst(), gcst(), ns(N-1), gcst(), ns(N-2), gcst().

gcst() ->
io:fwrite("GC: ~p~n", [statistics(garbage_collection)]).

ns(N) ->
M = (1 bsl N)*10000,
io:fwrite("Primes up to ~8.10B ~8.10B~n", [M, ns(0, 2, M, 0)]).

ns(_, I, M, C) when I > M -> C;
ns(B, I, M, C) ->
case is_prime(B, I) of
true  -> ns(mark(B, I, M, I+I), I+1, M, C+1);
false -> ns(B, I+1, M, C)
end.

mark(B, _, M, K) when K > M -> B;
mark(B, I, M, K) -> mark(not_prime(B, K), I, M, K+I).

is_prime(B, I) ->  0 =:= ((1 bsl I) band B).

not_prime(B, K) -> ((1 bsl K) bor B).

%%%

Here is what happens:

Erlang (BEAM) emulator version 5.5.5 [async-threads:0]

Eshell V5.5.5  (abort with ^G)
1> cd("../../../..").
C:/Dev/cean
ok
2> c(nsieve_e).
{ok,nsieve_e}
3> timer:tc(nsieve_e,main,[6]).
GC: {412,513637,0}
Primes up to   640000    52074
GC: {4540,144952244,0}
GC: {4541,144952601,0}
Primes up to   320000    27608
GC: {6659,4192756888,0}
Primes up to   160000    14683
GC: {7400,4055981814,0}
{858925000,ok}
4> timer:tc(nsieve_e,main,[7]).
GC: {7410,4055985246,0}

Crash dump was written to: erl_crash.dump
eheap_alloc: Cannot allocate 373662860 bytes of memory (of type "heap").

Abnormal termination

%%

The code produces the correct answers (slowly), and should not
accumulate garbage since the loops are all tail recursive.

Two questions:

Why isn't the garbage being collected inside my ns (or mark) loop?

How does the Words_Reclaimed go down between:

GC: {6659,4192756888,0}
Primes up to   160000    14683
GC: {7400,4055981814,0}

?

Thanks.

e

--
Doug Currie
Londonderry, NH, USA

```