Dynamic Memory Allocation

From COMP15212 Wiki
On path: Pointers 1: Memory • 2: Arrays • 3: Pointers • 4: Pointer Exercise • 5: Structures • 6: Dynamic Memory Allocation • 7: Malloc Exercise • 8: Structs Exercise
Depends on Memory ManagementPointers

Note: do not confuse this with the hardware technology “Dynamic RAM (DRAM)”; it is unrelated! Also note, this is not

the usual meaning of “DMA”.

Application

The memory (RAM) requirement of a process is often not known statically; it therefore has to ask for more memory (space) at run time.

For example, in (e.g.) Java this is hidden in an object creation new(). In other languages the programmer may need to make a more explicit library call – e.g. malloc() in C.

Mechanism

Initially there may be a ‘large’ area of free memory for an application to use; it is simple to ‘slice off’ part of this as required.

Later, allocating address space is usually complicated by fragmentation. This is where the space has various regions occupied or free, rather than being filled ‘up to a certain point’, like a tank of water. Because memory is widely referenced by pointers, once an item is placed it is inordinately expensive to try and change its (virtual, if applicable) address, so variables do not move. Over time, processes starting and stopping, objects being allocated and discarded etc. lead to a fragmented structure.

Fragmentation can be alleviated by allocating in fixed sized blocks but this:

  • wastes space if the programmer wants something smaller
  • is inconvenient if the programmer wants something bigger

There are numerous ploys which may be used; it’s all a trade-off of speed and efficiency. An example approach to is to keep one or more linked lists of the memory areas. Dynamic lists are useful because the number of fragments is uncertain, typically increasing (and sometimes decreasing) as a process runs.

Given a request for some memory of a particular size, the management software has an allocation problem. There are several possible approaches to finding something appropriate. Example algorithms include:

  • Cut the appropriate amount from the largest available block.
    • quick to find
    • leads to greater fragmentation
  • Find any ‘hole’ which is big enough
    • can leave large blocks intact, in case needed later
    • may require more searching
  • Find the smallest free block which is ‘large enough’
    • lessens fragmentation initially
    • may leave many ‘tiny’, useless holes
    • extensive searching may be slow

Randomization

Some modern operating systems deliberately include some randomness into the addresses they return to users. This is a security feature to make hacking more difficult, not (specifically) to confuse you further.

Recycling

Memory is a limited resource, and should be ‘freed’ when it is no longer in use. Java will detect automatically that memory has been abandoned by the programmer, and will return it automatically to the OS (see garbage collection). Other systems – such as the C environment – rely on the programmer deliberately returning excess allocations.

Failure to free() unwanted memory results in what has become known as a “memory leak”; this can eventually ‘leak’ all the machine’s resources and crash the program. It is therefore definitely a BadThing™ and you must not allow it to happen.


Here’s a little practical exercise to explore the topic further.


Also refer to: Operating System Concepts, 10th Edition: Chapter 9.2.2, pages 358-359


Articles on User
"Everything is a File" • Application Binary Interface (ABI) • Arrays • Boot • Buffer Overflow • Containers • Daemons • Disk Partition • Dynamic Memory Allocation • Emulator traps • Environment Variables • Errors • Exceptions • File Attributes • File Locking • File Permissions • Introduction to Operating Systems • Journalling File System • Links • Locks • Man(ual pages in Unix) • Memory Mapped Files • Monitoring • Network File System (NFS) • PATH • Pipes • Pointers • Relocatable Code • Reset • SETUID • Shell • Sockets • Spooling and Buffering • Streams • Structures • Superuser • System Calls • Unix Signals • User • Using Peripherals