Home » Posts tagged 'RAM'

Tag Archives: RAM

Placing Firefox cache in RAM on Ubuntu


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
Run:

# sudo sysctl -p
In Firefox, go to about:config page and search for the following entry:

browser.cache.disk.parent_directory
If it is not there, right click and create a new String value and name it as

browser.cache.disk.parent_directory
Set the value to /tmp (or tmpfs directory of your choice). Restart Firefox.

ZRAM vs ZSWAP vs ZCACHE


Another interesting to “oprex” :

Summary of what to use when:

  1. ZRAM if you have no HDD/SSD swap partition.
  2. ZSWAP if you do have a HDD/SSD swap partition.
  3. 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:

  1. ZRAM is a compressed RAM based swap device
  2. ZSWAP is a compressed Cache if you already have a swap.
  3. 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.

Details:

  • 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:
      1. 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
      2. 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 frontswap system 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 put and get calls. This is unlike normal memory that can be accessed a byte at a time. The frontswap and cleancache systems 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 frontswap frontend with the zcache backend 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:

Tuning Browser Performance dengan Profile-Sync-Daemon


Setelah melakukan tuning di sisi direktori sementara (temporary directory, /tmp, sekarang gw mencoba melakukan tuning sekali lagi pada sisi akses profile browser. Gw pake beberapa browser (Firefox, Google-Chrome, dan Opera), namun gw hanya akan melakukan tuning di Firefox aja. Sebenarnya bisa semua profile browser, tapi hanya akan membebani RAM karena gw sekali lagi akan memanfaatkan tmpfs yang seperti diketahui sangat bergantung pada RAM.

Ada script kecil yaitu profile-sync-daemon yang dapat melakukan itu semua. Cukup mudah instalasinya di Ubuntu dan turunannya.

sudo add-apt-repository ppa:graysky/utils
sudo apt-get update
sudo apt-get install profile-sync-daemon

(more…)

Setting Up a NFS Server on Top of tmpfs /dev/shm


tmpfs has blazing speed. Why not set up a high speed NFS server on top of tmpfs?

A little trick is required for setting NFS server on top of /dev/shm. If we add a normal entry in /etc/export and them run

# exportfs -a

exportfs will give us a warning like this: exportfs: Warning: /dev/shm requires fsid= for NFS export

When we try to mount this NFS server, we may get a error message from mount: mount.nfs: access denied by server while mounting nfsserver:/dev/shm

How to solve this problem? Just add the option fsid=1 to the /dev/shm entry in /etc/exports:

/dev/shm 10.0.0.1/24(rw,fsid=1,sync)

and remember to execute # exportfs -a again.

Then nfsserver:/dev/shm can be mounted in 10.0.0.1/24.

Compressing RAM with zRam


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.

Create file zramswap.conf in /etc/init and put the following content in it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
description "Initializes zram swaping"
 
start on runlevel [2345]
stop on runlevel [!2345]
 
pre-start script
 
# load dependency modules
modprobe zram num_devices=2
 
# initialize the devices
echo 1073741824 > /sys/block/zram0/disksize
echo 1073741824 > /sys/block/zram1/disksize
 
# Creating swap filesystems
mkswap /dev/zram0
mkswap /dev/zram1
 
# Switch the swaps on
swapon -p 5 /dev/zram0
swapon -p 5 /dev/zram1
end script
 
post-stop script
 
# Switching off swap
swapoff /dev/zram0
swapoff /dev/zram1
 
rmmod zram
end script

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.

Add RAM to Ubuntu 13.10+ for free: zRAM


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.

To install:

apt-get install zram-config

Make sure it’s started and running:

cat /proc/swaps

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.

ZRAM on Debian/Ubuntu for Memory Overcommitment


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:
(more…)