• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 992
  • Last Modified:

How to calculate heap size?

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!!!
0
naseeam
Asked:
naseeam
  • 2
  • 2
2 Solutions
 
Infinity08Commented:
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.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
0
 
naseeamAuthor 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.
0
 
Infinity08Commented:
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.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now