Main Page | See live article | Alphabetical index

Mac OS memory management

Historically, the Mac OS used a form of memory management that has fallen out of favour in modern systems. Criticism of this approach was one of the key areas addressed by the change to Mac OS X.

The original problem for the designers of the Macintosh was how to make optimum use of the 128K of RAM that the machine was equipped with. Since at that time the machine could only run one application program at a time, and there was no permanent secondary storage, the designers implemented a simple scheme which worked well with those particular constraints. Unfortunately that design choice did not scale well with the development of the machine, creating various difficulties for both programmers and users.

Table of contents
1 Fragmentation
2 MultiFinder
3 Object orientation

Fragmentation

The chief worry of the original designers appears to have been fragmentation - that is, repeated allocation and deallocation of memory through pointers leads to many small isolated areas of memory which cannot be used because they are too small, even though the total free memory may be sufficient to satisfy a particular request for memory. To solve this, Apple designers used the concept of a relocatable Handle, a reference to memory which allowed the actual data referred to to be moved without invalidating the handle. Apple's scheme was simple - a handle was simply a pointer into a (non relocatable) table of further pointers, which in turn pointed to the data. If a memory request required compaction of memory, this was done and the table, called the master pointer block, was updated. The machine itself implemented two areas in the machine available for this scheme - the system heap (used for the OS), and the application heap. As long as only one application at a time was run, the system worked well. Since the entire application heap was dissolved when the app quit, fragmentation was minimized.

Unfortunately in addressing the fragmentation problem, all other issues were overlooked. The system heap was not protected from errant applications, and this was frequently the cause of major system problems and crashes. In addition, the handle-based approach also opened up a source of programming errors, where pointers to data within such relocatable blocks could not be guaranteed to remain valid across calls that might cause memory to move. In reality this was almost every system API that existed. Thus the onus was on the programmer not to create such pointers, or at least manage them very carefully. Since many programmers were not generally familiar with this approach, early Mac programs suffered frequently from faults arising from this - faults that very often went undetected until long after shipment.

MultiFinder

The situation worsened with the advent of MultiFinder, which was a way for the Mac to run multiple applications at once. This was a necessary step forward for users, who found the one-app-at-a-time approach very limiting. However, because Apple were now committed to their memory management model, as well as compatibility with existing apps, they were forced to adopt a scheme where each application was allocated its own heap from the available RAM. The amount of actual RAM allocated to each heap was set by a value coded into each application, set by the programmer. Invariably this value wasn't enough for particular kinds of work, so the value setting had to be exposed to the user to allow them to tweak the heap size to suit their own requirements. This exposure of a technical implementation detail was very much against the grain of the Mac user philosophy, but unfortunately Apple were too stubborn to fix this and so this approach stuck until the advent of OS X. Apart from exposing users to esoteric technicalities, it was inefficient, since an application would grab all of its alloted RAM, even if it left most of it subsequently unused. Another application might be memory starved, but was unable to utilise the free memory "owned" by another application.

MultiFinder became the Finder in System 7, and by then the scheme was utterly entrenched. Apple made some attempts to work around the obvious limitations - temporary memory was one, where an app could "borrow" free RAM that lay outside of its heap for short periods, but this was unpopular with programmers so largely failed to solve the problem. There was also an early virtual memory scheme, which attempted to solve the issue by making more memory available by paging unused portions to disk, but for most users, this did nothing but slow everything down without solving the memory problems themselves. By this time all machines had permanent hard drives, MMU chips, and so were equipped to adopt a far better approach. For some reason, Apple never made this a priority, even though several schemes were suggested by outside developers that would retain compatibility while solving the overall memory management problem. Third party replacements for the Mac OS memory manager, such as Optimem, showed it could be done.

Object orientation

The rise of object-oriented languages for programming the Mac - at first Object pascal and later C++, also caused problems for the memory model adopted. At first, it would seem natural that objects would be implementred via handles, to gain the advantage of being relocatable. Unfortunately, these languages as they were used pointers for objects, which would lead to fragmentation issues. A solution, implemented by the THINK (later Symantec) compilers, was to use Handles internally for objects, but use a pointer syntax to access them. This seemed a good idea at first, but soon deep problems emerged, since programmers could not tell whether they were dealing with a relocatable or fixed block, and so had no way to know whether to take on the task of locking objects or not. Needless to say this led to huge numbers of bugs and problems with these early object implementations. Later compilers did not attempt to do this, but used real pointers, often implementing their own memory allocation schemes to workaround the Mac OS memory model.

While the Mac OS memory model, with all its inherent problems, remained this way right through to Mac OS 9, the increasing availability of cheap RAM meant that by and large most users could upgrade their way out of a corner. The memory wasn't used efficiently, but it was abundant enough that the issue never became critical. Mac OS X finally does away with the whole scheme, implementing a modern sparse virtual memory scheme. The older memory model APIs still exist for compatibility as part of Carbon, but map to the modern memory manager underneath. In fact this demonstrates conclusively that it was possible to change the model all along without a major compatibility issue, which was always Apple's defence for keeping the original scheme.