How to tweak and optimize SSD for Ubuntu, Linux Mint

I have been using Intel 520 SSD 180GB on my Dell Inspiron 11z-1121 laptop for a while and get great speed. However, if you want to tweak and optimize your Linux SSD for Ubuntu, Linux Mint, Debian or any linux distribution to make your SSD run even faster and live longer, here how I do my ubuntu ssd tweak.

Note: this article will help both Linux SSD and mechanical hard drive performance. If you want to improve and tweak your Ubuntu, Linux Mint, Fedora, Centos or any linux distribution, this article will help you.

SSD benefits

  • Faster than traditional HDD, about 2-3 times faster than SATA2 7200 RPM HDD
  • No moving parts
  • Lower heat during operating
  • Uses less power
  • Higher reliability

Update your SSD firmware

SSD manufactures (Intel, Samsung, SanDisk, Plextor, Corsair, Crucial…) often public new firmwares for their SSD to improve
improve performance, reliability, and system compatibility, fix technical issues, bugs). Check your SSD’s manufacture often to make sure you have the latest firmware for your SSD. *** Always backup before you do anything ***

Use EXT4 file system

Most modern new linux distributions default file system is EXT4. EXT4 gives you good speed and reliable for your SSD, EXT4 also supports TRIM. EXT4 is matured for SSD usage since linux kernel 2.6.28. If you have older linux distributions, you may have an option to choose EXT4 as file system, but newer linux distributions will install with EXT4 as default so you don’t need to worry about.

Enable TRIM

TRIM (Trim command let an OS know which SSD blocks are not being used and can be cleared)

Back up fstab first in case something wrong happen.

Edit fstab file

Add discard to your ssd drives or partitions, after ext4

Adding noatime and nodiratime

noatime and nodiratime are mount options in linux for linux file system. noatime disables atime updates on file system, and nodiratime will disables atime updates on directory system. By adding noatime and nodiratime will greatly reduce ssd load means performance gains.

Edit fstab file

Add noatime,nodiratime to your ssd drives or partitions, after ext4

Disable hibernation

Hibernate and suspend sometimes don’t work depends on the hardware you have for your system. Sometimes they make your system crash and unstable. Plus hibernation will put a lot of writes to your SSD which will shorten your SSD life.

Look for

Change from “yes” to “no”, there are two, one for hibernation, and another one for suspend. If you have to disable them both, make sure to replace them both from yes to no.

Tmpfs

Files and directories store in tmpfs is temporary, tmpfs keeps everything in virtual memory (kernel internal caches), nothing will be saved on your hard drive or SSD. Once your system is restarted, everything in tmpfs will be gone. Normally linux system cache stores in /tmp directory. To reduce writes on SSD, you can mount /tmp to tmpfs.

Edit fstab file

Add the line to the end of fstab file

If logs aren’t important for you (laptop or desktop), you can also mount /var/log to tmpfs. Add the line to the end of fstab file

Preload

Preload is a Linux software developed by Behdad Esfahbod. Preload learns programs that users use often, records statics using Markov chains, analyzes, and predicts what programs will be most used. Preload then will load those programs, binaries, and dependencies into memory or ram. By having programs already in RAM or memory, it will take less time when you actually start that program or programs.

To install preload on Ubuntu, Linux Mint or debian based distributions

To install preload on Fedora, Centos or Redhat based distributions

Swap and Swapiness

Swappiness is a part of Linux kernel that let you control how much swap (virtual memory) file is being used. Swappiness values can be changed from 0 to 100. The higher swappiness values the more Linux kernel will try to use swap space, the lower swappiness values means linux kernel will useless or try not to use swap space depends on our setting. The default swappiness value from linux kernel is 60, if your system have plenty have RAM, you should avoid using swap space which writes and reads will be on your SSD or hard drive. For system with 4 GB or more RAM, I would suggest to reduce the usage of swap by changing swappiness settings to between 10 even 0.

To check your swappiness setting on your system, you should see the value of 60 as default.

The value is up to you to decide. Here is my suggestion

  • 2 GB = 30
  • 4 GB = 10
  • 6 GB or more = 0

To change swappiness setting:

And add this line into sysctl.conf file.

Disk Caching and virtual memory (VM) subsystem

dirty_background_ratio

dirty_background_ratio is the maximum percentage of dirty pages stores in total system memory before need to be written to disk when pdflush/flush/kdmflush or kernel flusher threads kick in. The default value of dirty_background_ratio depends on your linux distribution, but we are going to decrease its value to prevent heavy disk I/O.
To check default setting for dirty_background_ratio

vm.dirty_ratio

vm.dirty_ratio is the percentage of total system memory to store dirty pages. Low vm.dirty_ratio means the kernel will flush dirty data to disk more often with smaller writes, and higher vm.dirty_ratio will allow kernel to flush dirty data to disk less often with bigger writes. We want to set vm.dirty_ratio high.
To check default setting for vm.dirty_ratio

To adjust vm.dirty_ratio and dirty_background_ratio, edit sysctl.conf

Add to your sysctl.conf file

Comments

  1. Nice post – great info. One point though, be careful with tmpfs. By default, tmpfs allocates half of your RAM for the temporary file system. This block is then unavailable to the OS. In my case, I found that 4GB of my 8GB RAM was being used to hold a handful of temp files that collectively were no bigger than 10mb. Naturally, I was a bit annoyed!

    You should specify a limit in fstab as follows:

  2. Good point the one Zapho makes, another way to do that is to specify percentage.
    My system has 16G ram, so i decided to go close to 1.5-2G:

  3. Set to 15% that gives 2.4G swap, sorry for being lazy.

    Great post!

    • I set mine at 1G

  4. Nice and concise. However one note. noatime includes nodiratime. So you don’t need to do #2 if you have #1 in place. Won’t hurt anything, just a note. Otherwise thanks

  5. no comment, but a question. if hibernation and suspend are disabled, why create a swap partition and limit “swapiness”? most seem to “get it”, but to me, a novice (have not run ubuntu at all), it seems like a lot of work when swap could be eliminated if
    enough ddr3 is installed. i am planning on 8gb of ddr3.
    please reply.

    • If you have enough RAM on your system, it’s safe to turn off swap completely. Don’t forget some programs like VM (virtual machines), video editors, LibreOffice uses lots of memory.

      You can turn off swap
      1. If you don’t run any RAM intensive programs
      2. Don’t use hibernation (save contents of RAM to swap partitions)

      Sometimes some bad programs *may* eat your RAM like crazy, swap partition is still like a safety net to prevent your system to crash (not likely to happen though).

      My laptop w/ 8 Gb RAM, I do run VMs sometimes with 2 Gb RAM each VMs, the system never use any swap space (I mount swap partition on RAM to prevent SSD wear/tear), so technically even I do have swap partition it’s on RAM not on my SSD. In your case depends on how you use your system, 8 Gb of RAM is safe to disable swap completely, if you want to.

      • thanks. i will use your optimization tips. let me just keep it simple and keep a small swap
        because the machine will be for editing photos, though not much else.
        how is swap mounted to ddr3? i assume you have no swap partition on the ssd?

        • I just checked my laptop. I do have swap parition on my SSD, but since I set vm.swappiness = 0, my system won’t write anything swap but use RAM space first. You can use swapon command to see if you have swap parition or not

          This is my swap parition
          Filename Type Size Used Priority
          /dev/sda5 partition 8179708 0 -1

          or

          /dev/sda5: UUID=”ab83ccb7-53af-4ef7-aff5-9157ee85dc30″ TYPE=”swap”

          You can completely disable swap parition by add “#” sign in front of your swap parition in fstab file.

  6. I used this line on ubuntu 12.04, and my system crashed. It has to be “size=50M” instead of “size=50mb”. Before rebooting, check out the new fstab with ‘sudo mount -a’, and look for errors. BUT nevertheless, great work here on this side.

    Though it’s good, to set the limit, I don’t think that tmpfs “allocates” all the size given by option. You can check this out with ‘df -h’ and then check your free ram. A good german :-/ article: http://wiki.ubuntuusers.de/SSD/Auslagerung

Leave a Reply