Dynamic memory allocation, also called heap-based memory allocation, is a type of memory management that is sharply contrasted by static memory allocation. In dynamic memory allocation, the programmer tells the computer exactly how much memory to allocate for each program and how long to hold onto the memory. This solves many of the issues that occur in static memory allocation, such as overuse of memory and stalled programs. While it solves many issues, if the programmer forgets to tell the memory to dump, then the dynamic memory can become an issue.
Computers, by default, use static memory allocation. This means the memory holds onto programs when they are used, even if the program is no longer being used or if the held memory becomes dangerous or an obstacle for performance. For example, if the user stops running a program that takes 2 megabytes (MB) on a 3 MB machine, and a second program needs another 2 MB, the second program will not work. This is because there is not enough memory left in the computer for the other program.
With static memory allocation, the user or programmer cannot specify the amount of memory he or she wants stored, nor can the programmer tell the static memory to dump the memory to free up resources. This is why advanced users often use dynamic memory allocation. The programmer is able to control everything about the memory, including how long to hold onto resources.
For example, the programmer can set dynamic memory allocation to hold onto only 1 MB, or less, which would save enough system memory to allow other programs to open. This can be done because the static memory usually holds onto everything about the program, while dynamic memory allows programmers to set it at a lower value that holds some data, but not everything. At the same time, static memory will still be needed, and it can be set in this allocation schema.
A potential problem with dynamic memory allocation — one that rarely comes into play — is that the dynamic memory may fill up if the programmer forgets to set a time value. If no time value is set, unlike static memory that will eventually release the memory, dynamic memory will hold onto the information. If the program has a garbage dump application, and most do, it will activate after a certain amount of time to clear the memory. It is still best practice to set a time value for the memory allocation.