<< Chapter < Page Chapter >> Page >

Today, even assembly programmers do not have to worry about memory allocation because the assembler will handle that. The memory allocation programs are good enough at solving the problem that it isn't worth a programmer's time to solve this problem. Instead, there are a few different ways that languages solve the problem of memory allocation. In general, it is a simple matter to provide the programmer with the memory that is known to be needed at compile time. This includes primarily global data values and the space for the code itself. The more difficult problem is how to provide flexible data memory that may or may not be needed when the program actually executes.

The approach that C takes is to ask the programmer to call special functions that manage memory allocation. These methods are called malloc(int) and free(void *) The basic idea is that whenever the program needs a specific amount of additional memory, it calls malloc (memory allocate) with the integer being the number of bytes of memory needed. The program will then search for a block of memory of the appropriate size and return a pointer to it. When the program is done with a particular allocation of memory, it calls free to let the memory management library know about the particular block of memory isn't needed anymore. If the programmer is diligent about returning (freeing) memory that isn't needed anymore, then the programmer will enjoy abundant memory without having to count individual bytes. On the other hand, if a program repeatedly requests memory but does not free the memory back to the system, the memory allocator will eventually run out of memory. The program will then crash. Thus, it is essential for passages of code that frequently request memory allocations to free these allocations as they can. Un-freed allocations are not fatal in very infrequently executed parts of code; however, the longer a program runs, the more the problem will accrue. In general, a program that allocates but does not free memory, gradually using unnecessarily more memory over time, is said to have a memory leak .

Other languages handle the problem of memory allocation automatically. Java will allocate the memory for new data on the fly using the keyword new instead of the function malloc, but the more important difference is that freeing takes place automatically. Part of the Java system called the garbage collector detects memory that can be safely freed and does so. In this fashion, Java programs do not suffer memory leaks in the way a C program might.

Memory and the msp

In the MSP430 there is no inherent difference between instruction memory, data memory, and heap memory. The only subdivisions in memory are the blocks of flash and the section of RAM. Any of these sections can hold instructions or other kinds memory. However, because it is problematic to erase and rewrite flash in the middle of program execution, the flash memory is best saved for instructions and constants. The remaining RAM must be shared between the heap, the dynamically allocated memory, and the global variables. On the MSP430F169, there is only 2KB of RAM, so no memory leaks are tolerable.

How memory is wasted or conserved

Memory leaks, the most notable way to waste memory, have already been discussed, but there are several others. While memory leaks abuse the dynamically allocated portion of data memory, many layers of function calls abuse the heap. Above, it was explained that each time a function calls another function, the caller's registers and data are moved onto the heap. If each called function calls another function in turn, then the heap portion of the memory will grow significantly. For high power computing systems, this is not usually a great threat to the overall supply of memory compared to memory leaks. Embedded systems however must avoid deep layers of function calling or risk exhausting the overall supply of memory.

There is also a programming technique called recursion wherein a recursive function calls itself repeatedly on progressively smaller or simpler versions of the data until the answer is trivial. While this technique leads to some very clever solutions to some complex problems, it is uses large amounts of memory to achieve this end. Therefore, recursion is generally a poor choice when memory must be conserved.

Finally, another important way to waste memory is to create too many global variables. Specifically, variables whose scope could be local or who could be allocated dynamically waste memory because they take up space even when they aren't being used. Use malloc and free to avoid using as many global variables.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Microcontroller and embedded systems laboratory. OpenStax CNX. Feb 11, 2006 Download for free at http://cnx.org/content/col10215/1.29
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Microcontroller and embedded systems laboratory' conversation and receive update notifications?

Ask