TRIM and garbage collection: keeping an SSD fast over time

Updated

Solid-state drives use NAND flash, which must erase whole blocks before rewriting. Garbage collection consolidates valid data and erases stale pages, while TRIM tells the SSD which pages are invalid, making GC more efficient. This article explains both mechanisms and how to verify TRIM support on common operating systems.

TRIM and garbage collection: keeping an SSD fast over time
Photo: PantheraLeo1359531, CC BY 4.0

Why SSDs need housekeeping

Solid-state drives store data in NAND flash memory, organized into pages for read and write operations and blocks for erasure. As the SNIA SSD overview explains, NAND is programmed in pages and erased in blocks that contain many pages.

Because a block must be erased before its pages can be rewritten, deleting a file does not immediately free its data on the SSD. The operating system marks the space as available, but the SSD retains the old contents until a block can be reclaimed. Over time, many blocks contain a mix of valid and stale pages, and writes become slower as the controller must move data around.

Garbage collection in action

Garbage collection is the background process that reclaims stale pages. The SSD reads the still-valid pages from a block, writes them to an already-erased block, and then erases the old block, making all its pages free. Samsung's SSD FAQ describes this as reading only the pages containing good data and rewriting them to empty blocks.

This process consumes write cycles, because data is moved even when the host is not writing. It also benefits from additional free space, often called over-provisioning. Many SSDs include a reserved area so that garbage collection always has a target block to write into.

TRIM: keeping the SSD informed

TRIM is a command that lets the operating system tell the SSD which pages are no longer in use. When a file is deleted, the OS marks the space as free but does not automatically inform the SSD. In Windows, this mechanism is referred to as file-delete notification, and it is controlled by a setting in the file system behavior. The Microsoft fsutil behavior documentation describes how to query and set that parameter.

Without TRIM, the SSD would continue to treat deleted pages as valid, forcing garbage collection to move them unnecessarily. With TRIM, the SSD can preemptively ignore those pages, which keeps write performance high and reduces unnecessary write amplification.

Checking and enabling TRIM on your system

On Windows, the defrag command includes a retrim option (defrag /l) that reissues TRIM commands to SSDs. The Microsoft defrag documentation also notes that the scheduled maintenance task for SSDs performs retrim and analysis about once per month.

For Linux systems using the ext4 filesystem, the discard mount option tells the kernel to issue TRIM commands automatically. The Linux kernel ext4 documentation describes this feature among the filesystem's options.

On modern NVMe drives, the Windows StorNVMe driver supports the Dataset Management command, which is used for TRIM (Deallocate). The Microsoft StorNVMe documentation lists this command as supported for I/O operations.

Garbage collection processStep 1: Identify a block with stale pages; Step 2: Read valid pages into a buffer; Step 3: Write valid pages to a new block; Step 4: Erase the old block; Step 5: Mark the block as freeGarbage collection processIdentify a block with stale pagesRead valid pages into a bufferWrite valid pages to a new blockErase the old blockMark the block as free
Garbage collection process
Common ways to check or enable TRIM support
Operating systemCommand or settingWhat it does
Windowsfsutil behavior query disabledeletenotifyShows if TRIM is disabled (1) or enabled (0)
Windowsdefrag /lPerforms retrim on the specified volume
Linux (ext4)discard mount optionIssues TRIM commands automatically
NVMe (Windows)Dataset Management commandUsed by the driver for TRIM (Deallocate)

Frequently asked questions

What is TRIM?

TRIM is a command that tells an SSD which data blocks are no longer in use, allowing the drive to skip them during garbage collection. It is also called file-delete notification in some operating systems.

Does TRIM cause data loss?

No. TRIM only applies to space that the operating system has already marked as deleted. The SSD uses this information to avoid moving invalid pages, but it does not erase valid files.

How can one check if TRIM is enabled on Windows?

Run the command fsutil behavior query disabledeletenotify at a command prompt. A value of 0 means TRIM is enabled, while a value of 1 disables it.

Why does Windows skip traditional defrag on SSDs?

Traditional defragmentation is unnecessary for SSDs and can cause extra writes. Instead, the scheduled maintenance task performs retrim and analysis, as described in the Microsoft defrag documentation.