Content originally posted in LPCWare by jharwood on Fri Jul 15 22:10:24 MST 2011
Maybe not completely accurate, but near enough: is to use memory 'painting'.
In the ResetISR() function within the C run-time startup code, fill the memory from the end of the .bss section up to the top of RAM with a pattern value. Perhaps something like: {0xD, 0xE, 0xA, 0xD, 0xC, 0x0, 0xD, 0xE} if done with 32-bit words.
Then, when you want to check memory usage, start at the top of RAM, working your way down, look for two (or more) contiguous occurrences of the pattern value. That will pin-point the stack "high water mark". (or "low water mark" if you want to think of it that way)
Continue down from there, looking for two (or more) contiguous non-occurrences of the pattern. That would pin-point the top of the heap.
You would probably have to make a point of always initializing heap allocations to all zeroes so that recent allocations would clear the painting pattern.
HTH, james