Slashdot Mirror


Dynamic Memory Allocation in Embedded Apps?

shootTheMessenger asks: "My company is porting our C++ Windows app to C in an embedded device and the question of whether to use dynamic memory allocation continues to come up. So far I have resisted malloc/free use but it gets tedious having the same argument with the next set of managers to take an interest in the project. Is there a definitive answer on the subject, especially one to counter the 'we have plenty of RAM - 16MB - so why not use dynamic allocation' argument? A quick google search finds that some sites frown on allocations within embedded applications, while others say it is OK in some contexts and yet others hack around it with pseudo-static allocations. How do you feel about this particular subject?"

9 of 102 comments (clear)

  1. Tradeoff by addaon · · Score: 3, Insightful

    If you have dynamic memory allocation, some day you're going to run out. If you're prepared to handle that case, either by (a) crashing or (b) recovering, then doing dynamic memory allocation will save you a good amount of development effort for a cost you've already decided to pay. If you're not going to allow (a), the question is whether (b) is more or less effort than just doing static allocation. That depends heavily on exactly what you're doing... but 16MB of memory is gargantuan, for many things.

    --

    I've had this sig for three days.
    1. Re:Tradeoff by ksheff · · Score: 2, Insightful

      I'm sure anyone doing DOS application development a couple decades ago would have loved a flat address space with 16MB of memory. Many applications used dynamic memory allocation then, so what's the problem with modern embedded applications? If you're getting lots of memory fragmentation, either the implementation of free() on the dev kit is broke (like it was with a version of Microsoft C in the late 80s), and/or you need to re-think the design of your application. I guess many programmers don't care much about memory usage anymore.

      --
      the good ground has been paved over by suicidal maniacs
    2. Re:Tradeoff by bunratty · · Score: 2, Insightful
      One problem is that it's often desirable for embedded applications to behave predictably. If you're using malloc, it can sometimes take more time than others, and the real-time behavior is not deterministic. Another problem is that embedded systems need to be very reliable over long periods of time with no user intervention. The smallest memory leak could lead to crashes or other erratic behavior.

      When I was programming embedded systems and was lucky enough to have actual dynamic memory allocation, I reserved using it for allocating memory at initialization and the few cases where it would have been tricky to use static allocation and deterministic behavior was not important.

      --
      What a fool believes, he sees, no wise man has the power to reason away.
  2. About this particular subject? I feel funny! by hackwrench · · Score: 3, Insightful

    Yeah, I always love it when a person comes to a public forum, being purposely vague for whatever reason and asks a question that really depends on the unmentioned.
    For example, if your embedded device runs embedded Windows, I don't really see the problem. On the other hand, a Windows GUI app really can't be ported to the vast majority of embedded devices out there.

    Speaking of embedded Windows, the subsystem is going to affect whatever it is you write.

    Given that you are talking about a "next set of managers", I figure it isn't really a flying leap to consider the possibility that you don't really have a specific embedded device in mind, or perhaps your people have looked at a WinCE device and said "Hey these things pretty much come with 16MB standard nowadays, wouldn't it be cool if we could get our application onto one of these babies!"

    The fact is that your app isn't going to be occupying that device alone, so look at how the rest of the programming treats the device.

    I'm bored now, so bye!

  3. Take exactly what you need by try_anything · · Score: 2, Insightful

    Do you know exactly how much memory you need? If so, allocate it statically.

    If you don't, there's no good way to avoid dynamic allocation. The best you can do is allocate a certain amount statically and try to get by with that. If you need more, you have to throw an error. If you need less, you're a memory hog - not a good thing in an embedded program.

    Dynamic memory management isn't so scary in C++. If you absolutely have to use C, God be with you.

  4. what i would do by fred+fleenblat · · Score: 3, Insightful

    It's fine to malloc/free in 16M, just keep a grasp on the situation.

    * install a malloc debug library on all test boxes to find all unfreed, double freed, or overwritten chunks. don't ship until all mem is accounted for.

    * don't allow, or at least discourage, "little" mallocs like strdup() or allocation of singleton structs.

    * do something sensible when malloc starts returning NULL. don't just seg fault or abort.

    * destruction test: malloc 15.5MB (and incr until failure) of space at the start just to establish the limit, then back it down 50K and see how long your embedded app can live in that space and watch how it dies. as you fix things, expect it to die in different places each time.

    * nm (or elfdump these days) your c lib and look for library functions that link to malloc or calloc. if your embedded OS ships with source, you can just grep for it.

    * if your app has a log file, put a printf(sbrk(0)) in every few minutes (or less as appropriate) so you can watch for unexpected growth over time and spikes related to usage.

    just some ideas.

  5. Re:Why? by try_anything · · Score: 2, Insightful

    C++ imposes hardly any run-time cost, and in most cases none at all, if you turn off exception handling and RTTI. This has been an explicit design goal of C++ since the beginning. Any problem that forces you into the situation you describe (which I think is inheritance with virtual functions) would require an equally complex C solution which would be harder to read and would probably compile to slower code since C++ compilers use implementation tricks that can't be expressed in C.

    The proof of this is that (almost) any standard C program can be turned into a standard C++ program with trivial changes (modulo some C99 stuff like complex numbers). If you can write C code that is faster or more predictable than C++ code, simply do so. Then rename your .c files to .cpp ;-)

  6. Re:Gee Only 16MB? by NullProg · · Score: 1, Insightful

    Moderators shouldn't moderate if they don't understand the topic. Parent is not offtopic.

    Meta-Moderators do your job.
    Enjoy.

    --
    It's just the normal noises in here.
  7. All about the frags by mcgroarty · · Score: 4, Insightful

    Memory fragmentation is murder on embedded devices. Do your best to avoid dynamic allocation unless static pools results in huge memory waste because your app is highly modal. Where dynamic allocation is a must, set up multiple heaps and find points where you can add code to free everything on that heap when switching modes to guarantee a fresh slate. If you have tons of small allocations, also consider setting up special heaps that handle fixed-size allocations. Having these parcelled off into their own area can go a long way toward fragmenting the space where the bigger allocations reside and you might find it's easier to find points where you can wipe heaps without the small allocations in the way.