Garbage Collection & TRIM: SSDs Dirty Little Secret

In Backup & Archiving Hardware by Michael GreccoLeave a Comment

SSD Garbage collection and TRIM
The rumblings online and in certain IT circles have proven to be true: SSD speeds can slow down over time. Fortunately, there are things you can do that can prevent this from happening and help you maintain the speedy nature of your SSDs.

What is SSD Garbage Collection

Just like taking out the trash is a necessary task in maintaining your home, garbage collection is also a type of SSD maintenance. It’s somewhat of a misnomer since garbage collection has nothing to do with removing files that are no longer needed. It’s more like getting rid of empty boxes of things you’ve already thrown away. At its core, SSD garbage collection is an automated process that is designed to improve performance.

We all know SSDs are initially very fast but, we are finding out that they get slower over time, especially as the drive begins to fill up. This is exactly the thing that SSD garbage collection is designed to prevent. With proper maintenance, the speed of an SSD can be preserved.

The Big SSD Slowdown

In order to understand why SSDs get slower over time, it’s important to know how file systems work. Every file system has its own way of doing things, but generally speaking, when you delete a file, that file is not really gone. The OS does not typically erase the storage blocks that were previously inhabited by the file. Instead, the OS simply marks the blocks as being available and stores that information in a directory file. This is why recovery software like Hard Disk Warrior is able to recover files that have been ‘erased’. The file still exists in those blocks on the disk, but the directory file lists them as empty so they are invisible to your computer.

With HDDs this method of deleting a file wasn’t an issue. The OS would simply mark the blocks as being available, and the hard disk could write new data over the top of the old data. SSDs can’t do that. For SSDs, storage blocks have to be erased before they can be overwritten. It is this requirement for erasing previously used blocks that causes SSDs to become slower over time.

Taking Out the SSD Garbage

To prevent this undesirable situation from happening, modern SSDs run complex routines called garbage collection in order to always keep as large a reserve as possible of pristine empty blocks ready for writing. Garbage collection involves having the controller search through its inventory of written pages for pages that have been marked as “stale”—that is, they were written to and then the data they contained needed to be modified by the OS; because changing the page’s state is impossible without first erasing it, the changes are always written to new pages and the old pages marked stale. Garbage collection looks for blocks that contain a mix of good and stale pages and then duplicates all the good pages into new blocks and leaves behind only stale pages in the old block. Then it erases the old block and marks it ready for use.

There is one more detail to note about SSD garbage collection, however. SSDs can read and write at the page level—which is usually 8KB for modern drives—but they have a peculiar failing in that they cannot erase at the page level. SSDs can only erase entire blocks, which are usually made up of hundreds of pages. The reason for this is that erasing a page’s contents requires zapping that page with a not-insignificant amount of voltage, and the NAND-style layout of all modern SSDs makes it prohibitively difficult to isolate that voltage to only the pages that need erasing.

And here’s where the ‘Garbage collection’ misnomer comes into play, because it’s actually good pages that are “collected,” with the garbage being left behind. The goal behind this process is to try to optimize performance by having blocks be either completely empty (and then erasing the block) or completely full.

What About TRIM?

On an HDD there’s usually a fixed correlation between the file system’s clusters and the disk’s sectors. But on an SSD, where there’s no fixed correlation and where in-use pages have to be tracked and picked up by garbage collection, it can be a big deal. Pages containing deleted files look like valid pages, and they keep getting collected along with actually good pages.

This is the situation TRIM was introduced to remedy. TRIM (which is properly capitalized but is not an acronym) is an ATA command that the operating system can cause to be sent when it deletes a file. The TRIM command provides that bridge from the file level to the block level, giving the operating system a way to tell the SSD that it’s deleting files and to mark those files’ pages as stale.

With TRIM, an SSD is no longer forced to save pages belonging to deleted files. TRIM isn’t an alternative to garbage collection—it works with garbage collection to more properly mark pages as stale. And you don’t need TRIM for garbage collection to work—but TRIM makes an SSD’s garbage collection more efficient.

That being said, if you’re using a Mac, Apple doesn’t support TRIM on third-party SSDs. It was possible to work around this limitation in previous versions of OS X, but you can’t do it in Yosemite without opening up a security hole —don’t do this, because although TRIM makes a difference in reducing write amplification and extending the life and performance of an SSD, it’s not worth the barn-sized security holes you have to open up in order to get it.

So, always use TRIM if you can. It will make your SSD’s garbage collection work a lot better. But if you find yourself in a situation where TRIM isn’t available, don’t panic—it’s nice to have, but it isn’t a requirement.