Home » Posts tagged 'zram'
Tag Archives: zram
To enhance Firefox performance on Ubuntu (or Linux), we can place the cache in RAM. This is especially helpful if you are used to hours of continuous browsing sessions.
I have mounted some directories with temporary files using the tmpfs, which is RAM based filesystem. These files will be discarded at reboot, which also helps to keep my system clean.
The entries in /etc/fstab are like:
tmpfs /tmp tmpfs noexec,defaults,noatime,nodiratime 0 0
tmpfs /var/log tmpfs noexec,defaults,noatime,nodiratime 0 0
Add the following line at the end of /etc/sysctl.conf file:
vm.swappiness = 0
# sudo sysctl -p
In Firefox, go to about:config page and search for the following entry:
If it is not there, right click and create a new String value and name it as
Set the value to /tmp (or tmpfs directory of your choice). Restart Firefox.
Another interesting to “oprex” :
Summary of what to use when:
- ZRAM if you have no HDD/SSD swap partition.
- ZSWAP if you do have a HDD/SSD swap partition.
- ZCACHE: It does what ZSWAP does and ALSO compresses and speeds the filesystem page cache. (It is internally much more complicated and is not in the mainline kernel as it is still under development).
Summary of their implementations:
- ZRAM is a compressed RAM based swap device
- ZSWAP is a compressed Cache if you already have a swap.
- ZCache is a backend for a special type of Virtual RAM thingy (Transcendent memory) that can be used to cache filesystem pages or swap data.
- ZRAM: Makes a swap device in the RAM. Pages sent here are compressed as they are stored. It has a higher priority than other swap devices: pages that are swapped out are preferentially sent to the zram device till it is full, only then are any other swap devices used.
- Benefits: Independent of other (physical) swap devices. It can be used when there is no swap partition to expand the available memory.
- Disadvantages: If other swap devices (HDD/SSD) are present they are not used optimally. As the zram device is an independent swap device, once it is full, any new pages that need to be swapped out are sent to next swap device directly, hence:
- There is a real chance of LRU (least recently used) inversion: It will be the most recently swapped data that goes to the slow disk, while inactive pages that were swapped out long ago will remain in the fast ZRAM
- The data sent to and read from the disk will consume a lot of bandwidth as it is uncompressed.
- Status: Merged into the mainline kernel 3.14. Once enabled on a system, it requires some userspace configuration to set up the swap devices and use them.
- ZSWAP: The
frontswapsystem hooks attempts to swap out pages and uses zswap as write-back-cache for a HDD/SSD swap device: An attempt is made to compress the page and if it contains poorly compressible data it is directly written two the disk. If the data is compressed, it is stored in the pool of zswap memory. If pages are swapped out of memory when the total compressed pages in RAM exceeds a certain size, the Least Recently Used (LRU) compressed page is written to the disk as it is unlikely to be required soon.
- Benefits: Very efficient use RAM and disk based swap. Minimizes Disk I/O by both reducing the number of writes and reads required (data is compressed and held in RAM) and by reducing the bandwidth of these I/O operaions as the data is in a compressed form.
- Limitations: It is an enhancement of disk based swap systems and hence depends on a swap partition on the hard disk.
- Status: Merged into the 3.11 mainline linux kernel.
- ZCache: It is a backend for the Transcendent memory system. Transcendent memory provides a RAM-like memory that can only be accessed a page at a time by using
getcalls. This is unlike normal memory that can be accessed a byte at a time. The
cleancachesystems hook attempts to swap and reclaim file-system page caches respectively and send them to the transcendent memory backends. When zcache is used as a backend, the data is compressed and stored in the RAM. When it fills up, compressed pages are evicted to the swap. (an alternate backend is RAMster which shares a pool of RAM across networked computers). Using only the
frontswapfrontend with the
zcachebackend works just like
zswap. (In fact zswap is a simplified subset of zcache)
- Benefits Provides compressed caching both for swap and for filesystem caches.
- Status: Still not mainlined as it is very complicated and is being worked on.
The best resources I found were:
Setelah gw menggunakan fitur zram dalam host linux di notebook yang gw gunakan untuk bekerja sehari-hari dan menggunakan btrfs sebagai file system untuk data, kali ini gw mencoba untuk melakukan tuning performance sekali lagi. Kali ini gw memanfaatkan file system virtual, tmpfs yang secara default sudah disupport oleh kernel Linux.
Sebagaimana kita tahu bahwa pada beberapa direktori seperti /tmp dan /var/tmp/ adalah direktori temporari yang digunakan oleh sistem Linux untuk menyimpan file dan direktori secara temporary. Pada saat Linux akan di-shutdown, maka direktori temporari tersebut akan di-clear dan dihapus termasuk isi-nya. Nah, berangkat dari pengertian itu, maka akan lebih baik jika direktori dan isi-nya tersebut ditempatkan di RAM. Akses RAM lebih cepat daripada akses ke disk.
Disclaimer : Gw hanya menyarankan untuk digunakan di notebook ataupun desktop. Tidak disarankan digunakan di server karena tuning kali ini belum menyertakan skrip untuk mem-flush / menuliskan isi direktori tersebut ke disk.
First answer :
ZRAM is a module of the Linux kernel, previously called “compcache”. ZRAM increases performance by avoiding paging on disk and instead uses a compressed block device in RAM in which paging takes place until it is necessary to use the swap space on the hard disk drive. Since using RAM is faster than using disks, zram allows Linux to make more use of RAM when swapping/paging is required, especially on older computers with less RAM installed.
ZSWAP is a lightweight compressed cache for swap pages. It takes pages that are in the process of being swapped out and attempts to compress them into a dynamically allocated RAM-based memory pool. zswap basically trades CPU cycles for potentially reduced swap I/O. This trade-off can also result in a significant performance improvement if reads from the compressed cache are faster than reads from a swap device.
Second answer :
- Status: In staging tree (as of 3.7) and looking to move into mainline
- Implementation: compressed block device, memory is dynamically allocated as data is stored
- Usage: Configure zram block device as a swap device to eliminate need for physical swap defice or swap file
- Eliminates need for physical swap device. This beame popular when netbooks first showed up. Zram (then compcache) allowed users to avoid swap shortening the lifespan of SSDs in these memory constrained systems.
- A zram block device can be used for other applications other than swap, anything you might use a block device for conceivably.
- Once a page is stored in zram it will remain there until paged in or invalidated. The first pages to be paged out will be the oldest pages (LRU list), these are ‘cold’ pages that are infrequently access. As the system continues to swap it will move on to pages that are warmer (more frequently accessed), these may not be able to be stored because of the swap slots consumed by the cold pages. What zram can not do (compcache had the option to configure a block backing device) is to evict pages out to physical disk. Ideally you want to age data out of the in-kernel compressed swap space out to disk so that you can use kernel memory for caching warm swap pages or free it for more productive use.
- Status: Posted to LKML on Dec 11th, 2012
- Implementation: compressed in-kernel cache for swap pages. In-kernel cache is compressed, the compression algorithm is pluggable using the CryptoAPI and the storage for pages is dynamically allocated. Older pages can be evicted to disk making this a sort of write-behind cache.
- Usage: Cache swap pages destined for regular swap devices (or swap files).
- Integration with swap code (using Frontswap API) allows zswap to choose to store only pages that compress well and handle memory allocation failures, in those cases pages are sent to the backing swap device.
- Oldest pages in the cache are pushed out to backing swap device to make room for newer pages, this solves the LRU inversion problem that a lack of page eviction would present.
- Needs a physical swap device (or swapfile).
A successor to compcache, zram, has been already integrated in the Linux kernel for a while now. This means that no additional compilation nor tweaking is required to benefit from compressing memory on the fly and massively reduced swapping.
As with compache, I wanted to nicely integrate the solution into the Ubuntu Upstart deamon – hence this short article. After a couple of minutes of playing the configuration was ready.
/etc/init and put the following content in it.
Now you can start the service with
sudo start zramswap (it will be automatically started on after the reboot as well).
You will benefit from 2x1GB swap files, which will be compressed and stored in the RAM. Tested on Ubuntu 13.10.
In the mainline generic kernel of Ubuntu, there’s a module called zram. This is a pretty good trick to add additional “free” RAM to your machine without any change: it creates in-memory compressed block for swap, meaning it eats a bit of your CPU but gives you literally more RAM.
If you’re on a VPS for example, having 512 MB RAM, this would actually give you access to 750 MB RAM and would eat just a little CPU from you – I don’t even notice it on the Munin graphs.
apt-get install zram-config
Make sure it’s started and running:
If you see something like this
# cat /proc/swaps Filename Type Size Used Priority /dev/zram0 partition 62712 6804 5 /dev/zram1 partition 62712 6768 5 /dev/zram2 partition 62712 6744 5 /dev/zram3 partition 62712 6768 5
then it’s already running.
Reboot your machine, and voilá. You might even turn your regulat disk-swap off.
In recent Linux releases, it’s available a tiny module called zram, that permits us to create RAM based block devices (named /dev/zramX), which will be kept in memory as compressed data. These ram-based block devices allow very fast I/O, and compression provides a reasonable amounts of memory saving.
We can use it as a drop-in replacement for the well-known tmpfs (used for speeding up compilation tasks or for /tmp), or better as a primary swap device, that will lead to virtually increase memory capacity, at the expense of a slightly increased CPU usage to compress/decompress the swapped data.
Nowadays RAM is very cheap, so why bother with compression? Because there are some situations where you can’t upgrade memory (netbooks) or you want to over-commit real resources (virtualization hosts).
For Ubuntu Precise and later:
Starting with Ubuntu Precise, there is an official upstart script for Ubuntu by Adam Conrad to configure zram in the main repository: