Swap and Compcache
Swap and Compressed Cache (Compcache) are optional memory management systems. By default, these schemes are disabled due to potential issues with some configurations. When enabled, these options use different strategies to increase the amount of working memory available to applications within Android.
What is RAM
Random Access Memory (RAM) is temporary data storage that the CPU uses during calculations. The more RAM a device has, the more calculated results the CPU can store - which means less time the CPU has to do the same thing over and over again. In other words, the CPU can check RAM to see if it has already made a particular calculation in the recent past. If it has, it can use the pre-computed results instead of wasting processing time recomputing the same calculation. In short, more RAM means a more efficient (and faster) device.
What is Swap?
Swap is, in short, virtual RAM. With swap, a small portion of the hard drive is set aside and used like RAM. The computer will attempt to keep as much information as possible in RAM until the RAM is full. At that point, the computer will begin moving inactive blocks of memory (called pages) to the hard disk, freeing up RAM for active processes. If one of the pages on the hard disk needs to be accessed again, it will be moved back into RAM, and a different inactive page in RAM will be moved onto the hard disk ('swapped'). The trade off is disks and SD cards are considerably slower than physical RAM, so when something needs to be swapped, there is a noticeable performance hit.
Unlike traditional swap, Android's Memory Manager kills inactive processes to free up memory. Android signals to the process, then the process will usually write out a small bit of specific information about its state (for example, Google Maps may write out the map view coordinates; Browser might write the URL of the page being viewed) and then the process exits. When you next access that application, it is restarted: the application is loaded from storage, and retrieves the state information that it saved when it last closed. In some applications, this makes it seem as if the application never closed at all. This is not much different from traditional swap, except that Android apps are specially programed to write out very specific information, making Android's Memory Manager more efficient that swap.
What is Compcache?
Compcache (compressed caching) is, in short, virtual swap, setting aside a certain percentage (usually 25%) of your RAM as 'compressed' swap. Compcache compresses the data that would normally go to swap, then moves it back into RAM, and reverses the process when moving it out of the 'compressed' swap. However, this is a classic space-time tradeoff. You effectively get more RAM from the compression, but it is slower than 'normal' RAM due to the CPU time required to compress and decompress the swapped pages.
Should I use Swap and/or Compcache?
This question is hotly debated, but you almost definitely do not need swap or compcache. The only exception to this may be if the device is a first generation device (i.e. HTC Dream or HTC Magic). If you think swap or compcache may make the device faster, feel free to follow the directions below.
Swap can give more available memory than compcache; however, class 6 SD cards are recommended and SD write wear is increased. Compcache is easier to set up, but has a limited memory increase and performance may slow down over time after boot.
Actual performance depends on user memory use; you'll only see a benefit if you're consistently using up all available memory, due to any combination of inherently low device RAM, using multiple apps simultaneously, or a singularly memory-intensive app. Otherwise, the performance hit will exceed any performance gain.
How do I enable...
Compcache
CyanogenMod does not enable Compcache support on all devices.
If the device comes with Compcache support, you can enable it by going to Settings » CyanogenMod settings » Performance settings » Compcache
Swap
A swap file can automatically be created and maintained by an app:
A swap partition can be manually created by the user. A swap partition has better compatibility than swap files when the phone is connected as USB mass storage. However, you must backup, format, and partition your SD card.
Troubleshooting
How can I tell if swap/compcache is running?
Go to the terminal emulator - or open adb shell - and run 'free'.
If it looks like this (with zeros in the swap line), you do not have swap/compcache:
total used free shared buffers Mem: 97932 96640 1292 0 272 Swap: 0 0 0 Total: 97932 96640 1292
If it looks like this (with anything other than zeros in the swap line), you do have swap/compache:
total used free shared buffers Mem: 97932 96004 1928 0 332 Swap: 24472 0 24472 Total: 122404 96004 26400