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, let’s investigate, then make a few adjustments to maximize the life of your SSDs and SD cards. Article refreshed from 6 years ago.
Using iotop to monitor and minimize reads/writes
Use your Linux distro’s package manager to install iotop, a top-like utility for disk I/O. It monitors disk I/O usage information output by the Linux kernel 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.
See MySQL tuning and PHP performance tips. You cannot 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 additional tips for avoiding some common disk overhead.
noatime Mount Flag
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 that are 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/sdx / ext4 discard,noatime,errors=remount-ro 0 1
Mount 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 adequate free memory and understand the risks, you can avoid adding swap altogether or use this instead.
Also, read Linux Performance: Why You Should Almost Always Add Swap Space, and Does your Linux server need a RAM upgrade?
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.
Storage= SystemMaxUse= SystemKeepFree= SystemMaxFileSize= RuntimeMaxUse= RuntimeKeepFree= RuntimeMaxFileSize= MaxLevelStore=
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 use Firefox, Chrome, etc., 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 periodically sync it back to your physical disc (HDD/SSD).
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):
If not already enabled by default. 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 can now write data to blocks without performing the cumbersome deletion process. In essence, TRIM makes sure that your SSD’s performance doesn’t degrade too much with use.
First, check your
/etc/fstab file, to confirm that you have the
discard option set for your SSD.
To check if your SSD or SD card supports TRIM, use:
sudo hdparm -I /dev/sdx | grep "TRIM supported"
To check if TRIM is enabled, use:
sudo systemctl status fstrim.timer
The result will look like this if enabled:
● fstrim.timer - Discard unused blocks once a week Loaded: loaded (/usr/lib/systemd/system/fstrim.timer; enabled; vendor preset: disabled) Active: active (waiting) since Wed 2021-05-12 14:52:42 AST; 58s ago Trigger: Mon 2021-05-17 00:35:19 AST; 4 days left Triggers: ● fstrim.service Docs: man:fstrim May 12 14:52:42 alien systemd: Started Discard unused blocks once a week.
To enable use:
sudo systemctl enable fstrim.timer sudo systemctl start fstrim.timer
Further increasing performance/life of SSDs & SD Cards
— Use larger SD cards. – Writes are spread based on the storage size, so the larger the storage, the less it will repeatedly rewrite 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/sdxx
Published: Jan 30th, 2018 | Last updated: May 12th, 2021