element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Blog Arduino: Dynamically allocating and freeing memory
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: e14 Contributor
  • Date Created: 16 Apr 2014 5:34 PM Date Created
  • Views 5056 views
  • Likes 1 like
  • Comments 13 comments
  • dynamic
  • free
  • malloc
  • sram
  • arduino
Related
Recommended

Arduino: Dynamically allocating and freeing memory

e14 Contributor
e14 Contributor
16 Apr 2014

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 functions. Only later do we discover that our available memory is getting reduced as time goes by -- followed by undetermined and random failures.

 

This is because no microcontroller (the ArduinoArduino included) has the ability to garbage collect.

 

For instance, let's say you malloc A and then malloc B. You then free A. There is now a memory hole (the size of A) that will never be closed again. Essentially a non-existing A is now taking up memory. If you now malloc A again, it will not be stored in that hole -- new memory will be taken up. You can see how this can become a problem.

 

To solve this problem, I developed the library ReMem (Reusable Memory), located in my usertools library, which among other things contains a full featured User Interface.

 

Documentation of ReMem can be found here. Basically ReMem creates an object (with a specified size) that has two functions: rmalloc and free. These work exactly the same way as the conventional malloc and free except when you free data, it will be used again if the same size data is requested. (The disadvantage is that it can be slow, and takes an extra byte of data per malloc).

 

 

Upcomming

The code is currently stable, but I'm going to be adding the ability to reclaim data that has been freed at the end, as well as cannibalize large allocations. Stay tuned.


Update

I have a new, more tested library for this purpose called tinymem. Check it out here: https://github.com/cloudformdesign/tinymem

  • Sign in to reply

Top Comments

  • shabaz
    shabaz over 11 years ago +3
    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…
  • clem57
    clem57 over 11 years ago in reply to e14 Contributor +3
    Large font does not make a good message! Please respect others thank you....
  • johnbeetem
    johnbeetem over 11 years ago +2
    I don't know for sure, but I thought malloc already reuses a block if it's the same size as a freed block. Specifically, if all your allocated storage blocks are the same size then malloc and free will…
  • shabaz
    shabaz over 11 years ago

    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'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'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. Furthermore for embedded systems one usually knows how many processes will run, and their memory needs.

     

    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'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?

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • e14 Contributor
    e14 Contributor over 11 years ago in reply to clem57

    tinymem has very little overhead (50bits per pointer). There are many reasons that you might need dynamic allocation -- many things simply are not possible without it.

     

    Do you have a link to the "stack" implementation you are referring to? FIFIO is often not enough for complex applications.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • e14 Contributor
    e14 Contributor over 11 years ago

    Nics

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 11 years ago in reply to e14 Contributor

    Per your link:

    My general rule for embedded systems is to only malloc() large buffers and only once, at the start of the program, e.g., in setup()

    So any use of library (malloc) is not justified when RAM is under 64K. Why incur the overhead. Right? My stack question is a mechanism for first in last out needs and not just function calls. There are many classical designs that would nicely use this.

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • e14 Contributor
    e14 Contributor over 11 years ago in reply to johnbeetem

    On a linux system with a memory manager it does, but on embedded systems this is most definitely NOT the default behavior. How could it be? Some systems have less than 500 bytes of RAM, where would they store pointers to all the free blocks?

     

    Here is a stack-overflow link for the Arduino in particular: http://arduino.stackexchange.com/questions/682/is-using-malloc-and-free-a-really-bad-idea-on-arduino

     

    I have looked at how linux systems do it, and most of them work by allocating memory to programs in large "chunks", 4kB is pretty standard, and then using compiled indirection like you say. This implementation will also not work for embedded systems for obvious reasons: there are not many "4kB chunks" for a system with between 2kB and 64kB of RAM, and adding compiled indirection for a broad range of microcontrollers would probably be difficult.

     

    Also, it is worth noting that tinymem uses indirection, just not in the manner you suggest -- which as you suggest would lead to huge speed-downs in addition to inefficient memory consumption.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
<>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube