Chapter 8. Memory Management

Memory is among the most basic, but also most essential, resources available to a process. This chapter covers the management of this resource: the allocation, manipulation, and eventual release of memory.

The verb allocate—the common term for obtaining memory—is a bit misleading, as it conjures up images of rationing a scarce resource for which demand outstrips supply. To be sure, many users would love more memory. On modern systems, however, the problem is not really one of sharing too little among too many, but of properly using and keeping track of the bounty.

In this chapter, you will learn about all of the approaches to allocating memory in various regions of a program, including each method’s advantages and disadvantages. We’ll also go over some ways to set and manipulate the contents of arbitrary memory regions, and look at how to lock memory so it remains in RAM and your program runs no risk of having to wait for the kernel to page in data from swap space.

The Process Address Space

Linux, like any modern operating system, virtualizes its physical resource of memory. Processes do not directly address physical memory. Instead, the kernel associates each process with a unique virtual address space. This address space is linear, with addresses starting at zero, and increasing to some maximum value.

Pages and Paging

The virtual address space is composed of pages. The system architecture and machine type determine the size of a page, which is fixed; typical ...

Get Linux System Programming 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.