We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# How to calculate heap size?

on
Medium Priority
1,044 Views
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.

Thanks!!!
Comment
Watch Question

## View Solutions Only

CERTIFIED EXPERT
Top Expert 2009
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Data Warehouse / Database Architect
CERTIFIED EXPERT
Commented:
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.

Kent

Commented:
>>   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.
CERTIFIED EXPERT
Top Expert 2009

Commented:
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.
Data Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
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).

Kent
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile