Den 13 januari 2012 23:11 skrev Max Lapshin <span dir="ltr"><<a href="mailto:max.lapshin@gmail.com">max.lapshin@gmail.com</a>></span>:<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>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>
<div><br></div><div>// Björn-Egil</div>