<html><head><base href="x-msg://121/"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>this make sense for me, especially taking into account that binary:split returns references to subject but I always thought that ets copies data. But here it seems that data is copied but reference counter to parts is not decreased... </div><div><br></div><div>- Dmitry</div><div><br></div><br><div><div>On Oct 12, 2012, at 10:27 PM, Chris Hicks wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><div class="hmmessage" style="font-size: 10pt; font-family: Tahoma; "><div dir="ltr">I could be wrong but I'm going to take a guess and say that in the first implementation the whole binary is being kept around and never destroyed. I think what's happening is you are getting a reference to part of a larger binary and passing that around, but the larger binary is sticking around since part of it is still being used. Copying the part you need, and thus creating an entirely new binary, is probably allowing all references to that large binary to disappear so that it can be GC'd.<div><br></div><div>That's a rather naive guess based on what I know about how binaries work. Can anyone else back that up or tell me I'm wrong?</div><div><br></div><div>Chris<br><br><div><div id="SkyDrivePlaceholder"></div>> From:<span class="Apple-converted-space"> </span><a href="mailto:dmkolesnikov@gmail.com">dmkolesnikov@gmail.com</a><br>> Date: Fri, 12 Oct 2012 22:15:21 +0300<br>> To:<span class="Apple-converted-space"> </span><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>> Subject: [erlang-questions] binary, ets and memory...<br>><span class="Apple-converted-space"> </span><br>> Hello,<br>><span class="Apple-converted-space"> </span><br>> Recently, my system starts to swap. The investigation has indicated that memory consumption was almost twice more then I've expected and to be honest, I've confused why it so...<br>><span class="Apple-converted-space"> </span><br>> I am talking about Erlang R15B (erts-5.9) [source] [64-bit] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]<br>><span class="Apple-converted-space"> </span><br>> So, I do have two processes<span class="Apple-converted-space"> </span><br>> * first process handles tcp/ip socket I/O. received data is pushed to second process<br>> * second process splits binaries binary:split(Buf, [<<$,>>], [global]), parses data and makes list of tuple. When list of tuples is ready, it folds tuples into ets table<br>> ets:new(cache, [named_table, ordered_set, public]),<br>> lists:foldl(<br>> fun({A, B}, Acc) -> ets:insert(cache, {A, B}) end,<br>> true,<br>> List<br>> ).<br>><span class="Apple-converted-space"> </span><br>> I do have about 6M tuples, where first element is SHA1 signature, second element is integer. Receiver process pushes 100 tuples per time. I hope you got rough idea.<br>><span class="Apple-converted-space"> </span><br>> When cache is populated, I do have following memory usage, it looks suspicious for me:<br>> {total,1179235080},<br>> {processes,2373638},<br>> {processes_used,2373570},<br>> {system,1176861442},<br>> {atom,264505},<br>> {atom_used,253241},<br>> {binary,434761000}, <-- this looks strange for me. Why binaries are left in heap and in ets?<br>> {code,6521469},<br>> {ets,732409416}<br>><span class="Apple-converted-space"> </span><br>> If I change my implementation to<span class="Apple-converted-space"> </span><br>> lists:foldl(<br>> fun({A, B}, Acc) -> ets:insert(cache, {binary:copy(A), B}) end,<br>> true,<br>> List<br>> ).<br>> then memory utilization is on the par with my estimates<br>> {total,701448856},<br>> {processes,2405251},<br>> {processes_used,2405170},<br>> {system,699043605},<br>> {atom,264505},<br>> {atom_used,253241},<br>> {binary,2686280},<br>> {code,6521493},<br>> {ets,686663080}<br>><span class="Apple-converted-space"> </span><br>> Best Regards,<span class="Apple-converted-space"> </span><br>> Dmitry<br>><span class="Apple-converted-space"> </span><br>> _______________________________________________<br>> erlang-questions mailing list<br>><span class="Apple-converted-space"> </span><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>><span class="Apple-converted-space"> </span><a href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a><br></div></div></div></div></span></blockquote></div><br></body></html>