3.2.4. Rehearsal

By now, you should understand what a memory layout of a typical process looks like. You should be able to describe how executable code, static data, heap and stack are stored in memory and what are their specific requirements with respect to process memory management.

Concerning the stack, you should be able to explain how return addresses, function arguments and local variables are stored on stack and how the contents of the stack can be elegantly accessed using relative addressing.

Concerning the heap, you should be able to outline the criteria of efficient heap management and relate them to typical heap usage patterns. You should be able to explain the working of common heap management algorithms in the light of these criteria and outline heap usage patterns for which these algorithms excel and fail.

You should be able to explain the principal approach to identifying garbage in garbage collecting algorithms and to discuss the principal differences between process memory management that relies on explicit garbage disposal and implicit garbage collection. You should understand the working of basic reference counting and reference tracing algorithms and see how the typical heap usage patterns lead to optimizations of the algorithms.

Based on your knowledge of how process memory management is used, you should be able to design an intelligent API that not only allows to allocate and free blocks of memory, but also helps to debug common errors in allocating and freeing memory.

Questions. 

  1. Identify where the following function relies on the virtual address space to store executable code, static data, heap and stack.

    void *SafeAlloc (size_t iSize)
    {
      void *pResult = malloc (iSize);
      if (pResult == NULL)
      {
        printf ("Failed to allocate %zi bytes.\n", iSize);
        exit (ENOMEM);
      }
      return (pResult);
    }
    
  2. List four distinct types of content that reside in a virtual address space of a typical process .

  3. Explain the advantages of segmentation over flat virtual memory address space.

  4. Explain why random placement of allocated blocks in the virtual address space of a process can contribute to improved security.

  5. Explain what the processor stack is used for with typical compiled procedural programming languages.

  6. For a contemporary processor, explain how the same machine instructions with the same arguments can access local variables and arguments of a procedure regardless of their absolute address in the virtual address space. Explain why this is important.

  7. Explain what is the function of a heap allocator.

  8. Explain why the implementation of the heap allocator for user processes usually resides in user space rather than kernel space.

  9. Design an interface of a heap allocator.

  10. Explain the problems a heap allocator implementation must solve on multiprocessor hardware and sketch appropriate solutions.

  11. Explain the rationale behind the Buddy Allocator and describe the function of the allocation algorithm.

  12. Explain the rationale behind the Slab Allocator and describe the function of the allocation algorithm.

  13. Describe what a heap allocator can do to reduce the overhead of the virtual memory manager.

  14. Explain the function of a garbage collector.

  15. Define precisely the conditions under which a memory block can be freed by a reference tracing garbage collector.

  16. Describe the algorithm of a copying garbage collector.

  17. Describe the algorithm of a mark and sweep garbage collector.

  18. Describe the algorithm of a generational garbage collector. Assume knowledge of a basic copying garbage collector and a basic mark and sweep garbage collector.

    Hint

    Essential to the generational garbage collector is the ability to collect only part of the heap. How is this possible without the collector missing some references ?