Memory

Overview

Memory is one of the most important parts of a computer and without it computers would be very limited.

Previous hierarchyUpdated hierarchy
      
Operating System Concepts - 8th Edition Silberschatz, Galvin, Gagne ©2009  
Some Numbers Regarding Memory
Operating System Concepts - 10th Edition Silberschatz, Galvin, Gagne ©2018  
Reading an integer from disk
Operating System Concepts - 8th Edition Silberschatz, Galvin, Gagne ©2009  

The Importance of Cache

CacheCache performance Fun with cache

Data structures performance comparison (access.c)

/* 4 million (222) */
#define SIZE 1024 * 1024 * 4

/* All data together */
struct GRAPHICS_DATA
{
  int red;
  int green;
  int blue;
  int alpha;
};

/* Array of structs containing all data */
struct GRAPHICS_DATA data[SIZE];

/* Colors in separate arrays */
int reds[SIZE];
int greens[SIZE];
int blues[SIZE];
int alphas[SIZE];

int main(void)
{
  int i;
#if 1
  for (i = 0; i < SIZE; i++)
    data[i].red = i % 255;
#else
  for (i = 0; i < SIZE; i++)
    reds[i] = i % 255;
#endif

  return 0;
}
Timings (Your numbers may vary with CPU/memory speed, cache architecture. -O2):
Array of structs: 0.090s
   Array of ints: 0.028s
Array of structures: Only 25% of the pre-fetched data is used, the rest is wasted.

Array of integers: 100% of the pre-fetched data is used.
Suppose we added more data to the structure (not uncommon)
struct GRAPHICS_DATA
{
  int red;
  int green;
  int blue;
  int alpha;
  double x, y, z; /* add more data */
};
Now running it:
Array of structs: 2.408s
Array of bigger structures: Only about 13% of the pre-fetched data is used.
Because of this, structure alignment (from CS120) can interfere with the cache behavior.

Note:

These times are directly related to memory and cache access times. It demonstrates that, if your code requires top performance, you need to understand how your data is structured. You don't just want to blindly lump all data together, unless it's absolutely necessary.

Cache Primer An overview of cache.

Physical vs. Logical Addresses

Physical memory Logical memory Address binding
  1. Compile time binding
  2. Load time binding
  3. Execution time binding

Dynamic Loading

Libraries Statically linked library

Stack and Heap

Implementing a heap using a linked list Strategies for handling memory requests FragmentationFragmentation

Paging

Page tablesPage tables Shared pages Protection Swapping

Virtual Memory

Virtual memory allows us to pretend that we have (almost) unlimited amounts of memory.

Virtual memory space

Freeing physical memory Demand paging Copy-on-write (COW)

Page replacement

Page replacement algorithmsPage replacement algorithms ThrashingThrashing Avoiding page faults Memory Cache (revisited) Security Issues Links: