Thanks for the Memory

Memory capacity is a major determinant of your Mac’s performance, and a significant proportion of its cost. But how complex it has become…

When computers were simple, memory was also simple: bulk data storage was slow, so whenever file data (including the code and resources of programs) were required, they were fetched from disk into memory, where the processor worked on and with them. Memory was much faster to access than disks, but was also more limited in its capacity, as it is considerably more costly in terms of £, $, or € per kilobyte, megabyte, or gigabyte.

This simple model started to break down when memory became available in greater capacity, and at more affordable prices. This led to ‘RAM disks’, in which a specified portion of memory is set aside and used as a high-speed disk. At about the same time, operating systems including Mac OS started to support multitasking; to make that work well, hidden scratch files on disks were used to produce ‘virtual memory’: in effect, the exact opposite of a RAM disk.

Cache memory

Memory itself became more complex. Far faster processors need much faster memory, which would be too expensive in the capacities normally required for general-purpose memory.

So smaller pools of extreme-performance ‘cache’ memory have been bound tightly into processors. For example, Intel’s Haswell processor architecture, which includes the Core i7 chips found in the latest iMacs, has three different types of cache memory:

  • level 1 consists of 64 KB that contains the data and instructions required immediately by the processor;
  • level 2 is slightly slower to access, but larger at 256 KB;
  • level 3 ranges from 2 to 20 MB, and is intermediate in access speed between L2 and regular system memory.

Expanding system memory

The only memory that is user-expandable is the regular system memory, these days tightly-specified according to the model of Mac.

About This Mac displays basic information about installed memory, and links to upgrade instructions.
About This Mac displays basic information about installed memory, and links to upgrade instructions.

A plethora of acronyms has made its terminology almost totally opaque, but current models tend to use one of two main types of memory: SO-DIMMs (Small Outline Dual Inline Memory Modules) for laptops, iMacs, and the Mac mini, and SDRAM (Synchronous Dynamic Random Access Memory) for Mac Pros.

Each model then requires further detailed specification, such as 240-pin PC3-14900 1866 MHz DDR3 ECC SDRAM for a current Mac Pro. This is spelled out in full as having 240 electrical connectors on the module, a memory clock rate of 1866 MHz, Double Data-Rate type 3, Error-Correcting Code, SDRAM, with a type designation of PC3-14900.

More detailed information is also available, but this does not test installed memory.
More detailed information is also available, but this does not test installed memory.

Even though memory modules may have a common number of pins and look as if they might be interchangeable, there are many pitfalls for the unwary. For instance, DDR types are not mutually compatible, so even if they have the same form factor, you could not put DDR3 modules into a model that requires DDR2.

Apple gives the detailed memory specification for each model in its product information and other documentation, although for some models its official maximum possible memory may prove conservative.

An excellent and frequently updated source is the free MacTracker application. Third-party memory vendors such as Kingston and Crucial also offer specific modules for different models of Mac.

If you are buying online or from a retailer, you must be assiduously careful to specify the Mac model correctly, for instance using the information provided in the System Information utility. Installing incompatible memory can destroy the memory modules and the Mac’s logic board, a very costly replacement.

Effects of failure

Next to hard disks, memory is the most likely component in your Mac to fail or cause overt hardware problems. Modules can become unseated, suffer electrical failure, or may simply prove incompatible. This incompatibility may only become manifest after you have updated or upgraded OS X: this has been a strange problem that has bedevilled some with the fastest Mac Pro systems, for example.

When memory fails to pass the startup test routines that each Mac goes through, different models signal the problem using special startup sounds instead of the normal chord, or by flashing light sequences by the power button or on the display. MacTracker used to play many of the ‘crash’ startup sounds, but now only plays the normal chords; Apple interprets these warning signals in each model’s manual, and separately for most Intel models and here.

Memory that fails whilst your Mac is running is one of the most common causes of a kernel panic. If you start experiencing repeated panics, particularly after installing additional memory, you should eliminate memory as a possible cause.

This is easy to accomplish by removing additional memory modules, which should stop the panics from occurring, and by testing your whole memory (with the suspect modules installed) with an exercising tool such as free Rember. The less extensive tests offered by general testing suites are usually inadequate to reveal these more subtle ailments.

Virtual memory

More than any other feature, virtual memory has broken down the previously clear distinction between memory and disk storage. This is not only permanently turned on in OS X, but certain applications implement their own schemes, with private scratch files. OS X allocates up to 4 GB of total memory to each 32-bit application running, and what is in practice an unlimited amount (about 18 exabytes) to each 64-bit application.

However, when you are running a few concurrent applications and have less physical memory than they require, the kernel automatically keeps active parts of each application within physical memory, and the rest cached to the swap file on your hard disk. Your system software manages this automatically, to minimise the overhead required to fetch cached data from disk and write data from memory to disk, thus keeping OS X and all your active applications running as quickly as possible.

Activity Monitor can alert you to memory shortage, but do not overinterpret the data: remember that OS X tries to use as much RAM as it can.
Activity Monitor can alert you to memory shortage, but do not overinterpret the data: remember that OS X tries to use as much RAM as it can.

When physical memory is small for the amount that needs to be kept in it, the memory manager in OS X may have to resort to reading in and writing out a lot of data to disk, known as ‘disk thrashing’, which brings a major performance hit.

A small amount of physical memory is dedicated to kernel code and data that must be retained there, so-called ‘wired memory’. The rest of physical memory is then shared between other OS X processes and running applications, with some being kept free to allow existing allocations to grow as required.

This mechanism is sufficient for the great majority of applications, but a few have their own built-in virtual memory management; the best-known of these is Adobe Photoshop, which keeps only small working sections of images in physical memory, the remainder being stored in scratch files on disk. Swap and scratch files work best when they are single, large files stored contiguously on a hard disk, so that access to their contents is as swift as possible.

Virtual hard drives

The final and most recent complication to this account of memory is the growing popularity of Solid-State Disks (SSDs), in which a large array of memory chips functions as if it is a high-speed disk drive. Today’s MacBook Air user will thus have the active part of each running application kept in physical memory, and its remainder cached out in a swap file that is being stored in the memory of an SSD – no hard disk being in sight.

Technique: Files as disks as memory

Open a document, it will be read into your Mac’s memory, and its contents displayed. You can bundle together one or more files and store them as a virtual disk, or disk image, which makes them easy to transfer electronically.

Furthermore one specialised type of disk image can automatically copy its contents out onto your hard disk; others, when mounted into memory, can be treated as if they were disk storage. Several of these types are easily used through Disk Utility, but to get the fullest range of options and types you will need to negotiate the labyrinthine features of the hdiutil command in Terminal. As ever, typing man hdiutil reveals detailed information.

Disk images can be of many different types. UDIF are the most widely used, being of fixed size, and can be read-write or read-only, uncompressed or use ADC, zlib or Bzip2 compression; thus a Bzip2-compressed image is known as ‘UDBZ’.

Special types include ‘UDTO’, which are CD-R or DVD-R masters for export, ‘UDSP’ sparse images that grow in physical size according to their contents, up to 128 petabytes maximum, and ‘UDSB’ sparsebundle images that are similar but backed by a directory bundle, up to just less than 8 exabytes. These maxima are far larger than storage you are likely to access for years to come. Fixed size images can still be resized using the hdiutil command, although if you intend burning it to optical media you should use diskutil resize first.

Another special type is the ‘IDME’ image that is Internet-enabled using the internet-enable verb of hdiutil; when downloaded, this is post-processed to copy the image’s visible contents into the folder into which the image is located, then the image is moved to the Trash with IDME disabled.

Further details on disk images and their use are here.

Technique: Memory as disks

The idea behind a RAM disk is simple: set aside some memory to act as if it is a very high-performance disk. Before the advent of virtual memory, this made a lot of sense.

Now that OS X has sophisticated virtual memory, which automatically and intelligently caches what should be in physical memory to hidden files on your hard disk, RAM disks seem at best naive if not positively disadvantageous. However in the right circumstances a RAM disk can still bring worthwhile performance improvements, for example when accessing many different disk files intensively. Further details are here.

Updated from the original, which was first published in MacUser volume 28 issue 02, 2012.