Thursday, April 3, 2008

Managing Memory Consumption

Memory consumption forms a major concern in the design of software for DSP/mobile devices. At the same time, a dynamically linked library is often the smallest unit of software that can be realistically managed when developing software for mobile devices. One particular detail that should be considered is that when managing memory consumption, some of the available memory will necessarily be allocated for implementing management routines.

Memory Limit

Setting explicit limits regarding memory usage for all parts of the system is one way to manifest the importance of controlling memory usage. Therefore, make all dynamically linked libraries (and other development-time entities) as well as their developers responsible for the memory they allocate. This can be achieved, for instance, by monitoring all memory reservations made by a library or a program. This can be achieved, for example, using the following routine, where MyLimit is the maximum amount of memory the library (or subsystem) can allocate and myMemory refers to the amount of allocated memory.

void * myMalloc(size_t size)
{
#ifdef MEMORY_LIMITATION_ACTIVE
if (myMemory + size > myLimit) return 0;
else { // Limit not reached.
void * tmp = malloc(size);
// Update myMemory if allocation succeeded.
if (tmp) myMemory = myMemory + size;
return tmp;
}
#else
return malloc(size);
#endif
}

While the above procedure only monitors memory space used for variables, the same approach is applicable to programs’ memory consumption as well. Then, the role of the approach is to place developers’ focus on memory consumption during the design. Furthermore, designing memory usage in advance creates an option to invest memory to some parts of a system for increased flexibility, and to optimize for small memory footprint on parts that are not to be altered. In order to be able to give realistic estimates for future releases when setting memory limits for them, one should maintain a database of memory consumption of previous releases to monitor the evolution of the consumption. Moreover, more precise estimates of the final results can be obtained by also including estimates made at different phases of the development project into the database, which can be used for evaluating the effect of errors in estimates made in the planning and design phases.


No comments: