D is a language that does not require explicit memory management. However, it is important for a system programmer to know how to manage memory when needed for special cases.
Memory management is a very broad topic. This chapter will introduce only the garbage collector (GC), allocating memory from it, and constructing objects at specific memory locations. We encourage you to research various memory management methods as well as the
As in some of the previous chapters, when we write variable, we mean any type of variable including struct and class objects.
Memory is a more significant resource than other system resources because both the running program and its data are located in the memory. The memory belongs ultimately to the operating system, which makes it available to programs to satisfy their needs. The amount of memory that a program uses may increase or decrease according to the immediate needs of a program. When a program terminates, the memory areas that it has been using are automatically returned back to the operating system.
The memory can be imagined like a large sheet of paper where the values of variables are noted. Each variable is kept at a specific location where its value is written to and read from as needed. Once the lifetime of a variable ends, its place is used for another variable.
& (address-of) operator is useful when experimenting with memory. For example, the following program prints the addresses of two variables that are defined next to each other: