Compcache
From CyanogenMod Wiki
Contents |
[edit] What is Swap?
Swap is, in short, virtual RAM. In other words, a computer will set aside a small portion of the hard drive or SD card and use it like it was RAM. Typically, a computer will 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 disk, freeing up RAM for active processes. If one of the pages on disk needs to be accessed again, it will be moved back into RAM, and something in RAM will be moved onto disk, if necessary ('swapped'). Although disks and SD cards are considerably slower than physical RAM, swap is useful when you need or want more memory, but you cannot install more physical RAM.
[edit] What is Compcache?
Compcache (compressed caching) provides the ability to use part of the RAM as compressed swap. In other words, you can take a portion of your RAM (default 25%) and use it as swap, compressing the data before moving it into swap, and decompressing it when moving it out of swap. This is a classic time-space trade-off. You effectively get more memory, but some of it is slower than normal RAM due to the CPU time required to compress and decompress the swapped pages.
[edit] Android Process Management vs. Swap
When Android runs low on free memory, the system tells an inactive process it is about to be killed. The process will write out a small bit of information about its state (for example, Google Maps may write out the map view coordinates; Browser will 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 swap in principle.
The difference between Android's process management and swap is that Android processes will write out very specific information when they are closed according to how the application was designed, while swap will write out any inactive memory pages, regardless of what information is contained in those pages. In theory, this makes Androids's memory manager more efficient than traditional swap/compcache.
In practice, however, some apps will not recover to the same state that they were closed in, and others take longer to recover their previous state from a restart than they would to load the state from swap. Browser, for instance, will re-load the web page in view when restarting, which takes much longer than reading the already loaded web page from swap.
[edit] Should I use Swap and/or Compcache?
Each configuration has its own pros and cons. The performance of any given configuration depends on the usage patterns of the user.
| Configuration | Pros | Cons |
|---|---|---|
| Stock | +Intended operating paramaters +Less complicated = less likely to break |
-Slow poor performance with some configurations (many widgets, for example, or switching between large apps) |
| Compcache | +More available memory +Better performance for some |
-More Complicated = more likely to break -Reduces available RAM by 25% -Tends to perform quickly after booting, but slows down over time |
| Swap | +More available 'RAM' +Better performance for some |
-More Complicated = more likely to break -Requires custom Userinit.sh script -Requires SD card with a swap partition, Class 6 for best performance and reliability |
| Compcache plus Backing Swap | +More available memory +Better performance for some +less RAM pre-allocated compared to compcache |
-Most Complicated = most likely to break -Requires custom Userinit.sh script -Requires SD card with a swap partition, Class 6 for best performance and reliability -Reduces available RAM by 15% |
Anecdotal observation in xda-developer forums is that straight Swap on a Class 6 microSDHC card offers the best performance of these four configurations.
[edit] Memory Card Wear with Swap
Each location on a flash memory card can only be written to so many times before it stops working correctly. Since swap will cause the swap partition to be written to frequently, there is a potential issue of wearing out part of a flash memory card. If a card wears out while using swap, your system will malfunction and crash.
Wear leveling is a mechanism that distributes write wear uniformly over the entire memory card by dynamically remapping logical memory blocks to different physical memory blocks over time. This largely alleviates the issue of card wear when using applications such as swap that tend to write to the same memory blocks over and over.
Anecdotal reports on the 'net indicate that most Class 6 microSDHC include hardware wear leveling, while Class 4 and lower cards typically don't. A-Data and Transcend Class 6 cards were specifically cited as having hardware wear leveling. Many people have used these cards for swap without issue.
[edit] How to create a swap partition on your microSD card
(necessary for swap or compcache backing swap; not necessary for compcache alone)
See Apps2SD Installation.
The Apps2SD page also has instructions for resizing existing partitions (without the need to wipe them) if you need to create more space for a new partition, and instructions on how to back up the contents of partitions and transfer them to a different card.
[edit] How do I enable...
[edit] Compcache
[edit] Built in menu
Go to Spare Parts, and check compcache enabled.
Requirements:
- None
[edit] userinit script
If you instead want to use a userinit.sh script to enable compcache, you will need to create a custom userinit.sh file. You will need to manually copy the file to your /system/sd/ folder. The following is an example that should work:
#!/system/bin/bash # disk_size=24480 #default 24480 (25%). Range: 0 - ~97932, larger number = less 'normal' RAM, more 'swap' RAM swappy=30 #swappiness. Range: 0 - 100, larger number = more likely to swap modprobe ramzswap; rzscontrol /dev/block/ramzswap0 --disksize_kb=$disk_size --init; swapon /dev/block/ramzswap0; sysctl -w vm.swappiness=$swappy; exit 0;
Requirements:
- userinit.sh script
- apps2SD (or manual copy to /system/sd)
[edit] Swap
[edit] Simple method
Install DroidSwap app and turn it On (Swappiness should probably be changed in settings first, though) This does not start at boot, though, so only use this method to see what swap can do, or to turn it on manually only when you need it.
Also could install Swapper instead?
[edit] Traditional method
To enable traditional swap, you will need to create a custom userinit.sh file. You will need to manually copy the file to your /system/sd/ folder,The following is an example that should work:
#!/system/bin/bash # partition=3 #partition number of swap partition. Change if swap is not partition 3. swappy=30 #swappiness. larger number = more likely to swap, smaller number = less likely to swap swapon /dev/block/mmcblk0p$partition; sysctl -w vm.swappiness=$swappy; exit 0;
NOTE: change "partition=?" accordingly. If you formatted your partitions with your recovery, you will most likely keep partition=3 [your setup would most likely be: vfat; ext2/3/4; swap]. If you manually partitioned, and _did not set yourself up an app2sd partition_ (aka, your setup is vfat; swap), then you will substitute partion=2 for partition=3.
Requirements:
- userinit.sh script
- swap partition
- apps2SD (or manual copy to /system/sd)
[edit] Compcache plus Backing Swap
Backing swap combines Compcache and traditional swap. Compcache will be used first. Once Compache is full, the traditional swap will be used. Once Compcache and the traditional swap are full, Android will begin killing processes using its native Memory Manager.
To enable backing swap, you will need to create a custom userinit.sh file. If you use apps2SD, you will need to place the script on the root of your SD card. If you do not use apps2SD, you will need to manually copy the file to your /system/sd/ folder, and change partition=2. The following is an example that should work:
#!/system/bin/bash # partition=3 #partition number of swap partition. Change if swap is not partition 3. mem_limit_kb=14688 #default 14688 (15%). Range 0 - [size of swap partition], larger number = less 'normal' RAM, more 'swap' RAM swappy=30 #swappiness. larger number = more likely to swap, smaller number = less likely to swap modprobe ramzswap; rzscontrol /dev/block/ramzswap0 --backing_swap=/dev/block/mmcblk0p$partition --memlimit_kb=$mem_limit_kb --init; swapon /dev/block/ramzswap0; sysctl -w vm.swappiness=$swappy; exit 0;
Warning:
- Go to Spare Parts, and check to make sure compcache is disabled first or the backing-swap will not work
Requirements:
- userinit.sh script
- swap partition
- apps2SD (or manual copy to /system/sd)
[edit] How do I disable...
[edit] Compcache
[edit] Built in menu
Go to Spare Parts, and uncheck compcache enabled.
[edit] userinit script
If you are using a userinit.sh script, delete or comment out the lines that look like the following:
#modprobe ramzswap; #rzscontrol /dev/block/ramzswap0 --disksize_kb=24480 --init; #swapon /dev/block/ramzswap0; #sysctl -w vm.swappiness=30;
Or, if the script was only being used for compcache, just delete the entire userinit.sh script.
[edit] Swap
If you are using a userinit.sh script, delete or comment out the lines that look like the following:
#swapon /dev/block/mmcblk0p3; #sysctl -w vm.swappiness=30;
[edit] Backing Swap
If you are using a userinit.sh script, delete or comment out the lines that look like the following:
#modprobe ramzswap; #rzscontrol /dev/block/ramzswap0 --backing_swap=/dev/block/mmcblk0p3 --memlimit_kb=14688 --init; #swapon /dev/block/ramzswap0; #sysctl -w vm.swappiness=30;
[edit] 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 for the swap line), you do not have swap/compache:
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 for 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
