Determining Memory Use on BG/Q

Help Desk

Updated: 01/25/2019

Determining the amount of memory available during the execution of the program requires the use of system-specific function calls. Using the mallinfo function available from the C library will not return complete accurate information on the BG/Q.

The following code demonstrates how to retrieve the amount of available memory:

   #include <stdio.h>
   #include <spi/include/kernel/memory.h>

   int main(int argc, char** argv)
   uint64_t shared, persist, heapavail, stackavail, stack, heap, guard, mmap;

   Kernel_GetMemorySize(KERNEL_MEMSIZE_SHARED, &shared);
   Kernel_GetMemorySize(KERNEL_MEMSIZE_PERSIST, &persist);
   Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAPAVAIL, &heapavail);
   Kernel_GetMemorySize(KERNEL_MEMSIZE_STACKAVAIL, &stackavail);
   Kernel_GetMemorySize(KERNEL_MEMSIZE_STACK, &stack);
   Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAP, &heap);
   Kernel_GetMemorySize(KERNEL_MEMSIZE_GUARD, &guard);
   Kernel_GetMemorySize(KERNEL_MEMSIZE_MMAP, &mmap);

   printf("Allocated heap: %.2f MB, avail. heap: %.2f MB\n", (double)heap/(1024*1024),(double)heapavail/(1024*1024));
   printf("Allocated stack: %.2f MB, avail. stack: %.2f MB\n", (double)stack/(1024*1024), (double)stackavail/(1024*1024));
   printf("Memory: shared: %.2f MB, persist: %.2f MB, guard: %.2f MB, mmap: %.2f MB\n", (double)shared/(1024*1024), (double)persist/(1024*1024), (double)guard/(1024*1024), (double)mmap/(1024*1024));

   return 0;

NOTE: Some unallocated memory may be available for heap allocation and the main program stack. As a result, the total available memory should not be calculated by summing the available stack memory with the available heap memory. Instead, use only the available heap memory to estimate the amount of memory used by the program.