Efficient heap memory management is crucial in embedded systems. This blog explores how to monitor and manage heap memory using IAR Embedded Workbench. We’ll cover two main approaches for heap monitoring: Live Watch and stdout for runtime checks, including the use of __iar_dlmallinfo
and __iar_dlmalloc_stats
functions.
1. Introduction to Heap Memory
Heap memory management involves dynamic allocation and deallocation of memory, which needs to be carefully monitored to avoid issues like fragmentation and memory leaks. IAR Embedded Workbench offers tools like Live Watch and functions such as __iar_dlmallinfo()
and __iar_dlmalloc_stats()
to help you monitor and analyze heap memory usage.
2. Using Live Watch for Real-Time Monitoring
Live Watch in IAR Embedded Workbench allows you to monitor variables and memory regions in real-time while debugging.
2.1 Setting Up Live Watch
- Open Live Watch Window
- Go to
View
->Live Watch
in IAR Embedded Workbench.
- Go to
- Add Heap Information Variables
- In the Live Watch window, add the
IARHeapInfo
structure and its fields. - This allows you to observe real-time changes in heap statistics as your application runs.
- In the Live Watch window, add the
2.2 Example Code
Here’s how to set up and use Live Watch to monitor heap information dynamically:
#include <iar_dlmalloc.h>
#include "main.h"
struct mallinfo IARHeapInfo;
void retrieveHeapInfo() {
IARHeapInfo = __iar_dlmallinfo(); // Retrieve heap info
}
int main() {
// Perform some memory operations
retrieveHeapInfo(); // Update heap info
// Main application code
while (1) {
// Continuously update and monitor heap info
retrieveHeapInfo();
// Optionally, print or analyze heap info here
// Example delay or operations
// ...
}
return 0;
}
In the Live Watch window, you will see IARHeapInfo
and its fields such as arena
, ordblks
, hblks
, etc. This lets you track memory usage dynamically during debugging.
Understanding struct mallinfo
The struct mallinfo
is used to retrieve detailed information about heap memory usage. Here’s a breakdown of the structure and its fields:
struct mallinfo {
MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */
MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */
MALLINFO_FIELD_TYPE smblks; /* always 0 */
MALLINFO_FIELD_TYPE hblks; /* always 0 */
MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */
MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */
MALLINFO_FIELD_TYPE fsmblks; /* always 0 */
MALLINFO_FIELD_TYPE uordblks; /* total allocated space */
MALLINFO_FIELD_TYPE fordblks; /* total free space */
MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */
};
- arena: Total non-mmapped bytes allocated from the system.
- ordblks: Number of free chunks in the heap.
- smblks: Always zero; reserved for future use.
- hblks: Always zero; reserved for future use.
- hblkhd: Total space in mmapped regions.
- usmblks: Maximum total allocated space (including potential wastage).
- fsmblks: Always zero; reserved for future use.
- uordblks: Current total allocated space (space used by user).
- fordblks: Total free space available in the heap.
- keepcost: The maximum number of bytes that could ideally be released back to the system via
malloc_trim
.
3. Using __iar_dlmalloc_stats()
for Summary Statistics
The __iar_dlmalloc_stats()
function provides a summary of heap memory usage by printing statistics to stderr
.
3.1 Example Code
Here’s how to use __iar_dlmalloc_stats()
to print heap memory statistics:
#include <iar_dlmalloc.h>
#include "main.h"
int main() {
// Perform some memory operations
// Print heap memory statistics
__iar_dlmalloc_stats();
return 0;
}
3.2 Sample Output
The output from __iar_dlmalloc_stats()
might look like this:
max system bytes = 61440
system bytes = 61440
in use bytes = 23584
- max system bytes: Maximum amount of system memory allocated (may include memory returned to the system).
- system bytes: Current amount of system memory allocated.
- in use bytes: Bytes currently allocated but not yet freed.
This output provides a high-level summary of memory usage, helping you quickly understand the overall memory state.
Leave a comment