Organizing and Allocating Dynamic Memory

The choice of a discipline for component memory allocation is deeply tied to component lifetimes. Control structures, such as the CLI’s threads and application domains, offer simple and efficient disciplines for managing component lifetimes and memory and, as we have seen, are used by the CLI as locations for storing both static and stack-based information. As already pointed out, however, storage strategies based on these mechanisms will work onlywhen the lifetimes of the components and resources being allocated are in sync with the lifetimes of the control structures. There are many cases when this is not the case. There are also many times when storing large amounts of data in runtime control structures might cause resource exhaustion problems or bad locality of reference.

Heaps, of course, solve these problems by using regions of memory that are managed by allocating subblocks to clients and tracking these so that the clients can later release their subblocks in arbitrary order, at any time. When used manually, a programmer “checks out” subblocks of heap memory with a function like malloc, holds them as long as necessary, and then frees them explicitly, which makes the memory available for recycling. Heaps that are managed using garbage collection, on the other hand, permit clients to release their subblocks by simply abandoning references to them. When quantities of heap memory run low, the garbage collection service can take ...

Get Shared Source CLI Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.