SSDs (solid-state drives) and SD (Secure Digital) cards have a limited number of writes before they wear out. To get the most out of this storage type lets investigate, then make a few adjustments to maximize the life of your SSDs and SD cards. Article refreshed from 3 years ago.
Using iotop to monitor and minimize reads/writes
Use can your Linux distro’s package manager to install iotop which is a top like utility for disk I/O. It monitors disk I/O usage information output by the Linux kernel (2.6.20+) and displays a table of current usage by processes on the system. Use iotop with the following options:
Then let iotop monitor things for a few mins or hours depending on how intense disk I/O is. This will result in a top-like screen which makes it easy to identify processes that are hogging your disk I/O. Have a look at the screenshot below as an example. I used the iotop -oPa command and let it sit for a few minutes in the background:
See MySQL tuning and PHP performance tips. You cannot always eliminate all disk I/O especially for services like MySQL and PHP (pictured above). What you are looking for are processes hogging disk I/O without good reason. Below are general tips for avoiding some common disk overhead.
noatime Mount Flag
Using the noatime mount flag in the /etc/fstab file stops the logging of read access times to the file system. The noatime mount flag eliminates the need for the system to make timestamp writes for files which are simply being read. Since writes are more expensive this often results in measurable performance gains.
Here’s what this line should look like in /etc/fstab:
/dev/sdxxxxxx / ext4 discard,noatime,errors=remount-ro 0 1
Temporary directories as tmpfs
If your system has enough memory, you can mount some temporary directories as a tmpfs. This reduces unnecessary writes to the SSD. Again edit /etc/fstab:
# SSD tweak: temporary directories as tmpfs tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 tmpfs /var/tmp tmpfs defaults,noatime,mode=1777 0 0
Avoid heavy use of Swap Space
This is a recommended tweak for SSDs and SD cards on systems using a swap partition, this will reduce the “swappiness” of your system, thus reducing disk swap I/O. On Debian/Ubuntu (or Red Hat/CentOS) add or modify the following in /etc/sysctl.conf (or the equivalent config file).
# Decrease swap usage vm.swappiness=10
If you have lots of free RAM and understand the risks, you can avoid adding swap completely, or just use this instead.
(However, please read Linux Performance: Why You Should Almost Always Add Swap Space or check if you need more RAM, read: Does your Linux server need a RAM upgrade? Lets check with free, top, vmstat and sar.)
Reduce logging writes
Disable access logs for Apache, Nginx, mail server and other services you have installed. Once your system is stable, you can reduce system log levels from info, to warn or even error OR if you don’t mind losing log files between boots move them to tmpfs by editing /etc/fstab:
tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0
For journalctl you can tweak this in /etc/systemd/journald.conf.
Mount even more directories with heavy I/O to tmpfs
For example mount the WordPress cache directory from disk to tmpfs:
tmpfs /full/path/to/wp-content/cache tmpfs defaults, size=1G 0 0
profile-sync-daemon (for desktop only)
If you’re not optimizing a web server and you use Firefox, Chrome, etc., then install the profile-sync-daemon. The Profile-sync-daemon (psd) is a tiny pseudo-daemon designed to manage your browser’s profile in tmpfs and to periodically sync it back to your physical disc (HDD/SSD).
Disable journaling on an ext4 filesystem You will have to unmount the file system partition first. Then if you know what you are doing (Update: read this first and backup) use the following commands:
tune4fs -O ^has_journal /dev/sdxxx e4fsck –f /dev/sdxxx sudo reboot
Consider switching from the CFQ to NOOP or Deadline. Both offer better performance on SSDs and SD cards.
Check which scheduler you are using with the following command (replace sdX):
TRIM allows Linux to inform the SSD which blocks of data are no longer considered in use. Therefore, when you delete a file, your SSD is now able to write data to blocks as if it they were brand new without having to perform the cumbersome deletion process. In essence, TRIM makes sure that your SSD’s performance doesn’t degrade too much with use.
Check if your SSD or SD card supports TRIM:
sudo hdparm -I /dev/sda | grep "TRIM supported"
If it does, then add TRIM as a daily cron job:
echo -e "#\x21/bin/sh\\nfstrim -v /" | sudo tee /etc/cron.daily/trim
Make the cron job executable using:
sudo chmod +x /etc/cron.daily/trim
This will run every day to avoid slowing down your writes.
EDIT: Using a Linux distribution which supports TRIM by default (ex. Ubuntu 14.04+) is helpful.
Further increase performance and life of SSDs & SD Cards
— Use larger SD cards. Writes are spread based on the size of storage, so the larger the storage, the less it will repeatedly write over the same areas, less wear.
— You get what you pay for. Cheap SSDs and SD cards usually will not last as long or perform as fast.
— Use this command to check for issues and lifespan:
sudo smartctl -a /dev/sdxxx
Last updated: Jan 30th 2018