<?xml-stylesheet type="text/xsl" href="https://community.element14.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Arduino: Dynamically allocating and freeing memory</title><link>/products/arduino/b/blog/posts/arduino-dynamically-allocating-and-freeing-memory</link><description>Everything that happens on a micro-controller has to be known. This includes dealing with memory. Many people (myself included) have developed a wonderful set of code that can dynamically use and free memory through the standard malloc and free funct</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Arduino: Dynamically allocating and freeing memory</title><link>https://community.element14.com/products/arduino/b/blog/posts/arduino-dynamically-allocating-and-freeing-memory</link><pubDate>Sat, 04 Apr 2015 08:29:24 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:52d70bf7-cd20-45b7-a2fb-4a7e8fdee93c</guid><dc:creator>clem57</dc:creator><slash:comments>1</slash:comments><description>&lt;blockquote class="jive-quote"&gt;
&lt;p&gt;Everything that happens on a micro-controller has to be known.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Why? This is not Linux with multiple users or processes. This is not even an RTOS. BTW, why would I want real time event processing take an extra few milliseconds while garbage collecting goes on? Kind of defeats the advantage of processing hardware in finite intervals. Arduino is meant as simple get it done environment with predictable results. And micro-python or whatever? Better create a python cross environment and avoid interpreter overhead! I have been doing this for too many years to believe that this is a true solution.&lt;/p&gt;&lt;p&gt;Clem&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=17539&amp;AppID=145&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: Arduino: Dynamically allocating and freeing memory</title><link>https://community.element14.com/products/arduino/b/blog/posts/arduino-dynamically-allocating-and-freeing-memory</link><pubDate>Sat, 04 Apr 2015 00:05:28 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:52d70bf7-cd20-45b7-a2fb-4a7e8fdee93c</guid><dc:creator>shabaz</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;I must admit, this is an odd discussion. Ordinarily for an embedded application (e.g. using an Arduino), variables are stored on the stack. How often does one need to use malloc on a constrained microcontroller like Arduino uses? What little memory you have will be used up partitioning it for all the processes that need to run on it (and let&amp;#39;s face it, the usual tiny little kernels that are intended for microcontrollers allocate the same amount of memory for all processes, so there is enough waste there that one really doesn&amp;#39;t want to waste yet more with a flexible malloc as enjoyed with Linux. Instead, the software designer can just think carefully about what global storage may be needed, and what can be placed on the stack safely. &lt;span&gt;Furthermore for embedded systems one usually knows how many processes will run, and their memory needs.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;In other words, the specific restrictions of an embedded system running on a processor with such limited RAM must force one to think carefully anyway about what data is stored and where; I can&amp;#39;t ever imagine using malloc for such a system. Surely it must be a very rare circumstance that an Arduino user would ever need malloc?&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=17539&amp;AppID=145&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: Arduino: Dynamically allocating and freeing memory</title><link>https://community.element14.com/products/arduino/b/blog/posts/arduino-dynamically-allocating-and-freeing-memory</link><pubDate>Fri, 03 Apr 2015 21:21:02 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:52d70bf7-cd20-45b7-a2fb-4a7e8fdee93c</guid><dc:creator>Former Member</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;Nics&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=17539&amp;AppID=145&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: Arduino: Dynamically allocating and freeing memory</title><link>https://community.element14.com/products/arduino/b/blog/posts/arduino-dynamically-allocating-and-freeing-memory</link><pubDate>Fri, 03 Apr 2015 18:40:28 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:52d70bf7-cd20-45b7-a2fb-4a7e8fdee93c</guid><dc:creator>johnbeetem</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;I don&amp;#39;t know for sure, but I thought &lt;strong&gt;malloc&lt;/strong&gt; already reuses a block if it&amp;#39;s the same size as a &lt;strong&gt;freed&lt;/strong&gt; block.&amp;nbsp; Specifically, if all your allocated storage blocks are the same size then &lt;strong&gt;malloc&lt;/strong&gt; and &lt;strong&gt;free&lt;/strong&gt; will keep reusing the same blocks and you&amp;#39;re fine.&amp;nbsp; The problem comes up if you&amp;#39;re using different size blocks.&amp;nbsp; Then you&amp;#39;ll end up with fragmentation, even with automatic garbage collection.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;I would guess that &lt;strong&gt;malloc&lt;/strong&gt; uses &amp;quot;first fit&amp;quot; or a similar algorithm.&amp;nbsp; Feel free to correct me.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Fragmentation must be avoided in embedded systems.&amp;nbsp; The best way is to make all blocks the same size, except for a few variable-length blocks that are allocated at initialization time and never deallocated.&amp;nbsp; Then you manage the list of available blocks yourself and fragmentation cannot occur.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Another approach is to use &amp;quot;David Wheeler&amp;#39;s Law&amp;quot; and access all dynamic storage through an extra level of indirection.&amp;nbsp; This has a big run-time penalty, but you can always defragment and move the blocks since each only has one pointer.&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=17539&amp;AppID=145&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item><item><title>RE: Arduino: Dynamically allocating and freeing memory</title><link>https://community.element14.com/products/arduino/b/blog/posts/arduino-dynamically-allocating-and-freeing-memory</link><pubDate>Fri, 03 Apr 2015 03:38:37 GMT</pubDate><guid isPermaLink="false">93d5dcb4-84c2-446f-b2cb-99731719e767:52d70bf7-cd20-45b7-a2fb-4a7e8fdee93c</guid><dc:creator>clem57</dc:creator><slash:comments>1</slash:comments><description>&lt;p&gt;Like... how about a simple stack implementation for localized variables. Just suggesting.&lt;/p&gt;&lt;p&gt;Clem&lt;/p&gt;&lt;img src="https://community.element14.com/aggbug?PostID=17539&amp;AppID=145&amp;AppType=Weblog&amp;ContentType=0" width="1" height="1"&gt;</description></item></channel></rss>