当前位置:首页 > Explainer: What is SSD Trimming? >

Explainer: What is SSD Trimming?

来源 泰山压卵网
2024-04-25 20:54:05

Have you ever noticed when you go into Windows to optimize your storage drives, it says retrim for SSDs? Just what exactly is getting trimmed and why is it needed? Is it the same as defragmenting or something entirely different? (it is different).

Such good questions all deserve thorough answers, so in this article we'll cover all of that.

What is SSD trimming?

SSD trimming is a process that helps to maintain the performance of a solid-state drive over time. Trim works by periodically erasing blocks of data that are no longer in use. The trimmed data isn't always removed straight away, as a complex process decides exactly when this takes place. But when it does, not only does it free up space on the drive, it helps the SSD perform better and last longer, too.

Simple, yes? What actually goes on, though, is quite a bit more complex – read on to find out!

Digging into the guts of your SSD

To understand why SSDs don't just delete files when you press the button, we need to take a quick look at how they work. We've taken apart SSDs before and you'll see that there's not much inside of them.

The sample below is of a relatively old SATA model (Samsung 850 Pro), but even the latest SSDs aren't much different in terms of the components that make up the drive.

The chip in the middle is a processor that manages all the instructions, data flow, encryption, and other algorithms. Above it is a small amount of DRAM, which acts as an instruction and data cache, plus it stores a table of data locations on the drive.

To the right and below the processor are two NAND flash modules – these are the chips that store all of the data and it's them that we need to delve into.

Deep inside these chips are billions of tiny components, called charge-trapping floating-gate metal oxide semiconductor field effect transistors. Since that name doesn't exactly roll off the tongue, the technology is usually called charge trap flash(CTF) and is the most commonly used system to store data in today's SSDs.

Each CTF acts as a single unit of storage, known as a memoryor bit cell, that has three electrical traces connected to it. The CTFs are grouped together, first as a long column (a string), with anywhere between 32 and 128 cells.

The cells in a string share a common trace (a bit line), which is used to read the data stored in them. Those that are on the same row as each other (known as a page) are all hooked up to another common trace (word line). The string and ground select lines are used in combination with the word lines to determine whether a read, write, or erase process takes place.

An array of strings and pages forms what is called a block. Pages and block sizes vary enormously, with the former being as small as 4 kB in size, and the latter as large as 512 kB, although a lot depends on the manufacturer and model.

A single NAND flash die will comprise thousands of blocks, and flash modules themselves may contain multiple dies. These vast, complex grids of traces and transistors make up every flash storage device, from USB memory sticks worth a few dollars, to enterprise-level multi-terabyte SSDs.

NAND flash is weird

Pages and blocks are important because all of the memory cells in this structure share the same substrate – the slice of semiconducting material, such as silicon or gallium arsenide, that all of the transistors are built upon.

To erase data from any cell involves the use of a high negative voltage, forcing any electrons stored in the CFT to flow into the substrate. Unfortunately, this means that the erase process wipes clean every cell in the block, not just one of them.

Another oddity with NAND flash is that the memory cells cannot be programmed with new data until all of the cells have been wiped clean – in other words, SSDs never directly write new data over old stuff, which traditional hard drives do. And where erasing has to be done at the block level, writing to them is done at the page level, which means that programming an SSD is much faster than erasing one.

The process of programming and erasing also damages the memory cells each time, wearing away the layer inside the transistor that stores the charge. To improve the longevity of the chips, the processor managing them cycles through all of the blocks, until each one has been used once, before going back to the start (so to speak).

So yes, NAND flash is definitely weird – fast writes, slow erases, damages itself doing either operation!

Throwing around the garbage

Now, let's get back to understanding what SSD trimming is all about. To do this, we'll take an imaginary SSD that has 4 kB pages and 256 kB blocks, so 64 pages per block. What would happen if you wanted to delete a single file that takes up 3056 kB on your SSD?

This file will be taking up 764 pages – 11 full blocks and one with 60 out of the 64 pages used. How do we delete this file without risking affecting those last 4 pages, as they could be containing data for another file? Seems like we're completely stuck!

Rescue initially comes in the form of the TRIM command. All data stays on the storage drive until it is explicitly instructed to do something about it. Files and folders that have been deleted by the operating system are flagged as no longer being required, and when the TRIM command is issued, the table stored in the SSD's DRAM (or the NAND flash itself, if the drive doesn't have any DRAM) is then updated to reflect this.

Note that not every SSD manufacturer uses the term TRIM but Windows does, so we'll stick to using this term. Data isn't erased immediately after the TRIM command has been sent – it either takes place when the drive is idle or when it next writes some data to a block. Which method gets used depends on the manufacturer, with consumer-grade models handling erases when idle, and enterprise-level ones generally doing it when writing.

Data that are flagged for removal are erased when the NAND flash's firmware commences a process called garbage collection. This involves reading a block and any pages that need to be kept are copied to the cache, then written to a fully empty block. The previous one, along with the pages flagged for deletion, is then erased.

In some ways, this process is to SSDs what disk defragmentation is to traditional hard drives, but it's not the same thing.

Is trim the same as defragmenting?

SSD trimming and defragmenting are not the same thing. Defragmenting is a process that is used to optimize the performance of hard disk drives (HDDs) by rearranging the data on the disk so that it is stored in a contiguous manner. This improves the efficiency of the drive by reducing the amount of time it takes to read and write data.

On the other hand, Trimming is specific to SSDs and is used to maintain the performance of the drive over time. SSDs use of flash memory means they have a limited number of write cycles. When data is deleted from an SSD, the space that it occupied is not immediately made available for reuse. Instead, the drive's firmware marks the space as "invalid" and it is not overwritten until the Trimming process is performed. This process helps to prevent the drive from becoming fragmented and slowing down.

Garbage collection is beneficial to the lifespan of the SSD and its overall performance, and TRIM just makes it better (sometimes the two terms are used interchangeably). This is because withoutthat command, garbage collection will just constantly move all pages about, condensing partially filled blocks, to keep freshly erased blocks available for programming – but this means unwanted pages will get moved about too, wasting time and increasing the wear on the memory cells. Because TRIM explicitly points out which pages are now junk, they can be left alone during garbage collection and erased as required.

Sending out the clippers

TRIM is automatically issued by Windows when you permanently delete a file (i.e. remove it from the Recycling Bin) but it doesn't instantly take place. It's added to a queue and gets processed when the SSD is ready for it.

However, this queue has a maximum size to it, and if it gets filled up, some of those TRIM requests will get dropped. By default, Windows schedules a re-issue of TRIM commands on a regular basis (calling it a retrim).

You can force this to take place but we don't recommend you do so. But if you insist, go to File Explorer, right-click on a drive, click on Properties, and then on the Tool tab. Finally, click on the button that says Optimize. If you don't have any SSDs in your PC, optimizing just runs normal disk defragmentation, but for NAND flash devices, clicking this re-issues a TRIM command. You can't actually make the drive to do anything, so don't worry about running it too frequently; the drive will look after itself perfectly well!

See how one of the SSDs above can't be optimized? That's because it's using a dynamic volume, which doesn't support the use of trimming. Basic volumes do recognize the TRIM command and for most users, it's best to use that type of drive volume anyway.

If your operating system or SSD configuration doesn't support TRIM, it's not really bad news – garbage collection still takes place, but the process isn't anywhere near as effective. Old data eventually gets removed simply because the drive will eventually overwrite unwanted pages, at some point in time.

For PCs using Windows 7, TRIM is only supported on SATA SSDs; for machines with NVMe ones, the command is only available in Windows 8, 10, and 11. TRIM was added to MacOS in the last update to Snow Leopard back in 2011. Most Linux distributions also support the operation, but not every file system is compatible – speaking of which, RAID systems generally don't support TRIM, although that situation is slowly improving.

Now you know what TRIM is and how it's beneficial to your SSD.

Keep Reading. Explainers at TechSpot

  • Explainer: What is Chip Binning?
  • We Cannot Live Without Cryptography!
  • Explainer: What is a File System?
  • Path Tracing vs. Ray Tracing, Explained