industrialNETworXnetx

Daniel Trautmann

Daniel Trautmann

IBHsoftec

| 14.11.2008 | 09:03 | 1 reply

Getting available memory size

Hi,

I need to determine the available free ram size in my program. Is there a function included in the rcX BSP?

Regards
Daniel

M T

M T

Hilscher Gesellschaft für Systemautomation mbH

| 17.11.2008 | 09:14

There is no such function in the rcX BSP. But as the memory allocation relies on standard libc malloc (newlib-1.14 included in Hitex Cross-Compiler Toolchain), there is a possibility to query the underlying functions about memory usage. The total size of the Heap is defined in the LD file and can be calculated manually.

The code below shows how to query the malloc information and check how many heap is left:

extern char  _heap_start; /* Defined by the linker */
extern char  _heap_end;   /* Defined by the linker */

#include

unsigned long GetFreeMemory()
{
/* Total size of the Heap (defined through ld-file) */
unsigned long ulTotalHeapSize = (unsigned long)(&_heap_end - &_heap_start);
struct mallinfo tMemInfo = mallinfo();

/* not yet used heap (max sbrk'able mem) */
unsigned long ulUnallocatedHeap = ulTotalHeapSize - tMemInfo.arena;

/* currently allocated memory inside arena */
unsigned long ulAllocatedMem = tMemInfo.uordblks;

/* Total free memory (ATTENTION: may be partially fragmented inside arena) */
unsigned long ulTotalFree = tMemInfo.fordblks + ulUnallocatedHeap;

return ulTotalFree;
}

Regards

MT

Login