How to calculate heap size?

Posted on 2009-02-17
Last Modified: 2012-05-06
We write Embedded 'C' for Freescale PowerPC based Controllers.  29Kbytes was reserved for heap in the MPC563 Microcontroller internal RAM.  I stole 2Kbytes.  Now 27Kbytes is left for the heap.  
Our code is complex and large in size.  I dont' know how much heap it needs.  How can I find the actual size of heap needed by the program?
I don't think 29Kbytes of space is needed for the heap?  I think 29Kbytes was assigned for heap because that was the remaining amount of space left in internal RAM.  I need to know the actual amount of heap needed by the program.  
For example, if program needs 20Kbytes, then taking 2Kbytes out of 29Kbytes space reserved for heap would not be a problem at all.

Question by:naseeam
    LVL 53

    Accepted Solution

    A lot depends on your heap manager, and how (good) it does its work. You could make an approximation by counting the sizes of the dynamic memory blocks you allocate, but that doesn't give you a real guarantee.

    Alternatively, you can keep track of the memory consumption while the code is running, and keep the max. memory consumption.
    You could also use those 2kB, then run the code and see if you ever get an "out of memory" error.
    LVL 45

    Assisted Solution

    Hi Naseeam,

    Calculating the heap requirements is extremely challenging.  When the program runs, it typically allocates the memory available to it from just two sources.  The stack and the heap.  The stack is what the program needs for its own controls.  When a function is called the variables that are passed to the function are placed on the stack.  So the stack is typically quite small.

    But the heap is an entirely different matter.  Every call to malloc(), calloc(), or realloc() assigns memory from the heap.  Every file that is opened gets a buffer on the heap.  Every open port gets a buffer on the heap.  If you have nested or recursive processes that use heap memory you can use a bunch of it quite quickly.

    You might run the code though a utility such as valgrind and inspect it's analysis.  Otherwise, your knowledge of the code is probably your best tool.


    Author Comment

    >>   Alternatively, you can keep track of the memory consumption while the code is running, and keep the max. memory consumption.

    How do I do that?  Do I need to use the debugger?  The heap in internal RAM.
    LVL 53

    Expert Comment

    Ideal would be if the system provides a process monitor (like the procfs in Linux for example).
    Otherwise, you can use a debugger or memory tool.
    If that doesn't give you the information you need, you can always keep track of the memory consumption yourself, although if you want exact values, it will require some knowledge of the heap manager.
    LVL 45

    Expert Comment

    Hi naseeam,

    Keeping track of the memory used yourself is not as straight forward as you might want as you'll need to keep a table or list of all of the assigned blocks and their sizes.  When malloc() or calloc() is called, you have the block length passed as a parameter.  But when realloc() or free() is called, you only have the block pointer to work with so you don't know the value to decrement.

    The heap is contiguous.  So with the first call to malloc(), calloc(), or realloc() set a variable with the "first block assigned" address.  Then with each additional call to one of those functions, calculate the last byte in the allocated block and keep track of the "high water mark".  The difference in the two addresses (the first block assigned and the high water mark) is the maximum size of your heap, plus or minus the block rounding size (typically between 8 and 32) and the length of the block header (probably 16).


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now