{"id":2452,"date":"2022-08-30T15:24:59","date_gmt":"2022-08-30T15:24:59","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/12\/erlang-heap-overflow-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:24:59","modified_gmt":"2022-08-30T15:24:59","slug":"erlang-heap-overflow-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/erlang-heap-overflow-collection-of-common-programming-errors\/","title":{"rendered":"ERlang HEAP overflow-Collection of common programming errors"},"content":{"rendered":"<p>Even your edit is <em>not<\/em> tail-recursive:<\/p>\n<pre><code>loop() -&gt; \n  receive\n     {sys, Msg} -&gt;\n         handle_sys_msg(Msg),\n         loop();\n     {From, Msg} -&gt;\n          Reply = handle_msg(Msg),\n          From ! Reply,\n          loop();\n      _ -&gt; continue \n  end,\n  loop().\n<\/code><\/pre>\n<p>The order of execution for one function is: <code>receive ... end, loop()<\/code>. Now, if you get a <code>{sys, _}<\/code> message, <code>loop\/0<\/code> will be called from within the receive, transforming the order of execution above into something equivalent to:<\/p>\n<pre><code> loop() -&gt;\n      receive\n          loop() -&gt;\n               receive\n                  ...\n               end,\n               loop(),\n      end,\n      loop() -&gt;\n         ...\n<\/code><\/pre>\n<p>The problem is that if you call <code>loop()<\/code> from within the receive, the VM still has to store the return point in order to run the <code>loop()<\/code> in place after the <code>receive<\/code>.<\/p>\n<p>To make your function tail-recursive, you would need to do either:<\/p>\n<pre><code>loop() -&gt; \n  receive\n     {sys, Msg} -&gt;\n         handle_sys_msg(Msg);\n     {From, Msg} -&gt;\n          Reply = handle_msg(Msg),\n          From ! Reply;\n      _ -&gt; continue \n  end,\n  loop().\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>loop() -&gt; \n  receive\n     {sys, Msg} -&gt;\n         handle_sys_msg(Msg),\n         loop();\n     {From, Msg} -&gt;\n          Reply = handle_msg(Msg),\n          From ! Reply,\n          loop();\n      _ -&gt; loop()\n  end.\n<\/code><\/pre>\n<p>Where calling <code>loop()<\/code> really is <em>always<\/em> the last thing to be done in the function.<\/p>\n<p id=\"rop\"><small>Originally posted 2014-01-12 20:51:04. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Even your edit is not tail-recursive: loop() -&gt; receive {sys, Msg} -&gt; handle_sys_msg(Msg), loop(); {From, Msg} -&gt; Reply = handle_msg(Msg), From ! Reply, loop(); _ -&gt; continue end, loop(). The order of execution for one function is: receive &#8230; end, loop(). Now, if you get a {sys, _} message, loop\/0 will be called from within [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2452","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2452"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2452\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}