<br><br><div class="gmail_quote">Den 16 januari 2012 18:24 skrev Jon Watte <span dir="ltr"><<a href="mailto:jwatte@gmail.com">jwatte@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Is this an artifact of the current implementation?<div>Semantically, I don't see why a sufficiently smart VM wouldn't be able to collect the binary at that point.</div></blockquote><div><br></div><div>Hmm, yes, I did not answer correctly to the example. (Bad teacher) "A" isn't used in the above example so it will be garbed. Let me show you by example what I mean.</div>
<div><br></div><div><div><font face="'courier new', monospace">-module(test).</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">-compile([export_all]).</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">go1() -></font></div><div><font face="'courier new', monospace">    {ok, A} = file:read_file("test.erl"),</font></div>
<div><font face="'courier new', monospace">    go1(A).</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">go1(A) -></font></div><div>
<font face="'courier new', monospace">    ok = nothing(A),</font></div><div><font face="'courier new', monospace">    erlang:garbage_collect(),</font></div><div><font face="'courier new', monospace">    io:format("~p~n", [erlang:process_info(self(), binary)]),</font></div>
<div><font face="'courier new', monospace">    ok.</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">go2() -></font></div><div><font face="'courier new', monospace">    {ok, A} = file:read_file("test.erl"),</font></div>
<div><font face="'courier new', monospace">    go2(A).</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">go2(A) -></font></div><div>
<font face="'courier new', monospace">    erlang:garbage_collect(),</font></div><div><font face="'courier new', monospace">    ok = nothing(A),</font></div><div><font face="'courier new', monospace">    io:format("~p~n", [erlang:process_info(self(), binary)]),</font></div>
<div><font face="'courier new', monospace">    ok.</font></div><div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">nothing(_) -> ok.</font></div>
</div><div><br></div><div><br></div><div>What will the two functions go1 and go2 produce?</div><div><br></div><div><div><font face="'courier new', monospace">Eshell V5.9.1  (abort with ^G) </font></div><div><font face="'courier new', monospace">1> test:go1().</font></div>
<div><font face="'courier new', monospace">{binary,[]}</font></div><div><font face="'courier new', monospace">ok</font></div><div><font face="'courier new', monospace">2> test:go2().</font></div>
<div><font face="'courier new', monospace">{binary,[{11960880,575,2}]}</font></div><div><font face="'courier new', monospace">ok</font></div></div><div><br></div><div><br></div><div>// Björn-Egil </div><div>
<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><br></div><div>Sincerely,</div><div><br></div>
<div>jw</div><div><span class="HOEnZb"><font color="#888888"><br clear="all"><br>--<br>Americans might object: there is no way we would sacrifice our living standards for the benefit of people in the rest of the world. Nevertheless, whether we get there willingly or not, we shall soon have lower consumption rates, because our present rates are unsustainable. <br>

<br>
<br><br></font></span><div class="gmail_quote"><span class="HOEnZb"><font color="#888888">2012/1/13 Björn-Egil Dahlberg <span dir="ltr"><<a href="mailto:wallentin.dahlberg@gmail.com" target="_blank">wallentin.dahlberg@gmail.com</a>></span><br>
</font></span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
Den 13 januari 2012 23:11 skrev Max Lapshin <span dir="ltr"><<a href="mailto:max.lapshin@gmail.com" target="_blank">max.lapshin@gmail.com</a>></span>:<div><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


I thought, that calling gc will be a problem:<br>
<br>
<br>
A = crypto:rand_bytes(1024*1024*1024),<br>
erlang:garbagecollect(),<br>
{reply, ok, State}<br>
<br>
<br>
Will gc free A? I think that no, because A is still used in the place,<br>
where gc is called. Am I right?<br></blockquote><div> </div></div></div>Correct.<div><br></div><div>To be a little technical (and don't take this as an absolute truth in the details),</div><div>What we see here is "A is referencing a large binary", which is true. What happens internally is that Eterm A is a boxed pointer from the stack or a register which points to a ProcBin on the heap. The ProcBin in turn holds an increment of a Reference counted binary off heap. Both the stack and the registers will become part of the root set during the garbage collection hence letting the binary be reachable and therefore "live".  I believe that you need leave the function (release the stack) in order to free your binary. </div>


<div><br></div><div>A gc without the A in the stack will not let the ProcBin survive during the copy phase of the gc. When the ProcBin is removed the RefcBinary will decrement and if it reaches zero, i.e. the last ProcBin has died, that too will be removed.</div>

<span><font color="#888888">
<div><br></div><div>// Björn-Egil</div>
</font></span><br></div></div><div class="im">_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></div></blockquote></div><br></div>
</blockquote></div><br>