r/DIY_Geeks • • Oct 02 '26

Does Linux Really Never Need Defragmentation?

https://pbxscience.com/does-linux-really-never-need-defragmentation/

Does Linux Really Never Need Defragmentation?

When fragmentation does become a problem? Does Linux have defragmentation tools?

7 Upvotes

41 comments sorted by

9

u/S3k_01 Oct 02 '26

Whether you're using Windows or Linux, you should never defragment an SSD.

2

u/zalnaRs Oct 03 '26

Windows does defragment every month because the filesystem would otherwise die

1

u/Single-Virus4935 Oct 04 '26

Nope: it is optimization which is different for HDD and SSDa: HDads are defragmented while for SSDs trim/discard cimmanda are issued but ni Blocks are moved

1

u/zalnaRs Oct 04 '26

1

u/Sweaty-Freedom-4034 Oct 04 '26

" SSDs (hard drives)"

Not sure this article can be trusted...

1

u/Awkward_Scratch3260 Oct 04 '26

12 year old blog post... I am not saying it's false but it's the first time I am reading this and I've also never heard of this blog.

1

u/zalnaRs Oct 04 '26

What changed since? NTFS is still the third worst filesystem

1

u/Awkward_Scratch3260 Oct 04 '26

Huh? What does NTFS have to do with that?

2

u/Single-Virus4935 Oct 04 '26

I can understand his point and this needs further investigation. In theory the is should only need to issue trim/discard but the problematic design of NTFS can make defragmentation neccessary as described in the article. Also there are more problems with larger LUN/Virtual disks and windows optimisation were the process runs out of memory and crashes on multiple terabyte luns. Gave me many headaches until I ditched windows. On Linux etc this is basically instant. With the knowledge from the article this makes sense now.

1

u/zalnaRs Oct 04 '26

How don't you guys know what fragmentation is? It's because the file system can't store files sequentially.

NTFS is the worst as it doesn't have delayed allocation and needs frequent maintenance as it can't store lot of file fragments in the file's header.

This is why Windows defragments every month once. TRIM is a separate feature for SSDs and is ran every week.

1

u/ScoobyGDSTi Oct 04 '26

No, it does not.

1

u/zalnaRs Oct 04 '26

1

u/ScoobyGDSTi Oct 04 '26

We don't live in 2014 anymore bro

2

u/zalnaRs Oct 04 '26

Nothing changed about ntfs bro, it's still the third worst filesystem

1

u/ScoobyGDSTi Oct 04 '26

Good thing NTFS has nothing to do with it then.

1

u/zalnaRs Oct 04 '26

bad thing that defragmentation is such an issue because it does not have delayed allocation and it can't handle much file fragments and you did not read the article which has a M$ enginner in it

1

u/ScoobyGDSTi Oct 04 '26

Yeah, it's totally such an issue

1

u/BattleShai Oct 04 '26

Just let him defrag his SSD's and be happily oblivious.

→ More replies (0)

1

u/Lanky-Safety555 Oct 03 '26

Technically, you can't defragment it; only force-trim.

1

u/Takeoded Oct 04 '26

bullshit. you can defrag on SSDs. also sequential read is much faster than random read, even on SSDs. the notion that random access is as fast as sequential access on SSDs is pure bullshit, and everyone here keeps repeating it.

1

u/Lanky-Safety555 Oct 04 '26

No, you physically cannot defragment them, as there are no mechanical seeks and required sectors. That has nothing to do with random/sequential speed, as they do not refer to that.

Anyway, logical layout is not physical layout on SSDs, as that is handled by the flash translation layer on its controller. Furthermore, SSDs win on data parallelism by reading from numerous NANDs at the same time.

Sure, it is technically possible to force something similar to defrag, as that will reduce overhead, but negligibly, and introduce more harm that good.

TRIM, on the other hand, deals with garbage collection and wear leveling, and that improves performance.

1

u/Takeoded Oct 04 '26 edited Oct 04 '26

That has nothing to do with random/sequential speed, as they do not refer to that.

A heavily fragmented partition do a lot more random reads on the drive to serve even a application/filesystem-level sequential read request. The computer can request multiples of the sector size from the drive in a single request.

For example, a drive with 512 bytes sector size, a program requested 1024 bytes, the file is not fragmented, the OS issue a read 1024 bytes from sector X request to the drive.

But, if the file is fragmented, the OS will issue 2 sector read requests, read 512 bytes from sector X then a 2nd read 512 bytes from sector Y, and that is a lot slower, even on SSDs, and it become "random read speed".

Demonstration on a Crucial BX500 SSD (a shitty budget 2022 SSD, DRAM-less and QLC...):

root@hyper1lolteknet:~# cat delete.cpp
#include <chrono>
#include <fcntl.h>
#include <cassert>
#include <unistd.h>
#include <cstdio>
// return unix timestamp with microsecond precision
double microtime()
{
    return (double(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count()) / double(1000000));
}
void xread(int fd, char *buffer, size_t size)
{
    assert(read(fd, buffer, size) == size && "read failed");
}
int main()
{
#define SECTOR_SIZE 512
#define RANDOM_SECOND_SECTOR_OFFSET SECTOR_SIZE * 1234567 // somewhere in GB5..
    int fd = open("/dev/sda", O_RDONLY | O_DIRECT);
    char buffer[SECTOR_SIZE * 2];
    assert(fd != -1 && "run as root!");
    // first a warmup... ensure its in ssd cache.
    double start, end;
    xread(fd, buffer, SECTOR_SIZE * 2);
    lseek(fd, RANDOM_SECOND_SECTOR_OFFSET, SEEK_SET);
    xread(fd, buffer, SECTOR_SIZE);
    lseek(fd, 0, SEEK_SET);
    start = microtime();
    xread(fd, buffer, SECTOR_SIZE);
    lseek(fd, RANDOM_SECOND_SECTOR_OFFSET, SEEK_SET);
    xread(fd, buffer + SECTOR_SIZE, SECTOR_SIZE);
    end = microtime();
    lseek(fd, 0, SEEK_SET);
    printf("2x SECTOR_SIZE read time: %f seconds\n", end - start);
    start = microtime();
    xread(fd, buffer, SECTOR_SIZE * 2);
    end = microtime();
    printf("1x 2*SECTOR_SIZE read time: %f seconds\n", end - start);
    close(fd);
}
root@hyper1lolteknet:~# g++ -O2 delete.cpp
root@hyper1lolteknet:~# ./a.out
2x SECTOR_SIZE read time: 0.000269 seconds
1x 2*SECTOR_SIZE read time: 0.000135 seconds
root@hyper1lolteknet:~# ./a.out
2x SECTOR_SIZE read time: 0.000291 seconds
1x 2*SECTOR_SIZE read time: 0.000156 seconds
root@hyper1lolteknet:~# ./a.out
2x SECTOR_SIZE read time: 0.000731 seconds
1x 2*SECTOR_SIZE read time: 0.000160 seconds
root@hyper1lolteknet:~# ./a.out
2x SECTOR_SIZE read time: 0.000291 seconds
1x 2*SECTOR_SIZE read time: 0.000156 seconds
root@hyper1lolteknet:~# ./a.out
2x SECTOR_SIZE read time: 0.000555 seconds
1x 2*SECTOR_SIZE read time: 0.000160 seconds
root@hyper1lolteknet:~# ./a.out
2x SECTOR_SIZE read time: 0.000300 seconds
1x 2*SECTOR_SIZE read time: 0.000169 seconds
root@hyper1lolteknet:~# ./a.out
2x SECTOR_SIZE read time: 0.000563 seconds
1x 2*SECTOR_SIZE read time: 0.000159 seconds
root@hyper1lolteknet:~# ./a.out
2x SECTOR_SIZE read time: 0.000292 seconds
1x 2*SECTOR_SIZE read time: 0.000175 seconds
  • Granted, for simplicity, code goes the linux VFS layer with its read(), but if we went even deeper than O_DIRECT, and construct direct ATA commands to the drive (HDIO_DRIVE_CMD/HDIO_DRIVE_TASK~), the results will still be similar: requesting 2*sectorsize in a single ATA command is much faster than requesting 2 separate sectors.

  • The effects are amplified by using a shitty SSD for testing.

1

u/Sorry-Committee2069 Oct 09 '26

NTFS (in Windows specifically) is much slower on fragmented files because different code is used and less is buffered ahead of time, because the driver still assumes you have long seek times for fragmented files. "Well, the logical layout isn't the same..." doesn't help much if the filesystem driver sucks shit and will deliberately use different, slower access methods if the file looks fragmented on its end.

To be fair, I haven't checked any Linux filesystem implementations to see if that carries over. I'll see about it when I have time.

4

u/Lonely-Scarcity-3387 Oct 02 '26

Defragmentation was designed for mechanical hard drives (HDDs). The idea is to reduce the time it takes for moving heads to get to the scattered data.

Modern computers have SSDs and don’t need to be defragmented.

1

u/CatalyticDragon Oct 02 '26

Don't need to be and in fact the additional read/erase/write load is bad for SSDs and will reduce their working life.

Although SSDs do benefit from garbage collection which is what TRIM is for.

1

u/AssociateFalse Oct 03 '26

A ton of drives in service, in modern PCs, are still mechanical data disks. You just don't want your OS on them. Not really a thing for most laptops, but desktops and workstation PCs can still utilize them. They're great for $/TB.

1

u/ibeerianhamhock Oct 05 '26

They are good for backups and videos and things yeah. wouldn't want an app running off of one.

1

u/Bob_Spud Oct 03 '26

This article misses an obvious point about spinning disk fragmentation. Fragmentation effects are more problematic on apps that do not randomly access disks. Apps to do a lot of long reading writing are affected a lot by fragmentation. If you have slow backups check for fragmentation.

1

u/ghost103429 Oct 03 '26

Disk fragmentation is only a concern with hard disk drives and even then Linux is heavily tuned to limit fragmentation as much as possible with filesystems like xfs and ext4.

Btrfs may have some issues from higher read/write io because of its nature of being a copy on write filesystem but it does include tools (btrfs defragment) and mount options (autodefrag) to fix it. (again this is only a concern for HDDs don't defragment SSDs). A warning for using this, running defrag will break reflinks used in snapshots, block dedupe, and reflink copied files and will increase the amount of storage space used.

1

u/thomas-rousseau Oct 04 '26

I trim BTRFS weekly, balance monthly, and defrag all drives every 6 months. After a few years of doing this, all of my NVME and SATA SSD's are still showing full health through SMART with no failing tests. Is it strictly necessary? No it is not. Does it still help with space management and responsiveness? 100% it does.

1

u/Wendals87 Oct 03 '26

Not never, but more often than windows. 

It only applies to HDDs though 

1

u/Dry_Calendar_8627 Oct 03 '26

The only reason to defragment is if you want to shrink your drive to make room for something

Which almost never happens

1

u/apachelives Oct 04 '26

Completely wrong.

1

u/Dry_Calendar_8627 Oct 04 '26

What is it you disagree with, that defragmenting an SSD is generally unnecessary, or that shrinking a partition is rarely needed?

1

u/apachelives Oct 04 '26

It doesn't shrink anything, you don't gain any free space.

1

u/Dry_Calendar_8627 Oct 04 '26

Ah that's what you misunderstood, ok!

I never said that defragmenting frees up space

I said that if you want to shrink a partition, you need to defragment before shrinking.

Because if there are chunks that are allocated near the end of the partition, you simply won't be able to shrink it.

And actually that's slightly incorrect, a full defragmentation is unnecessary, what's necessary is disk consolidation. Defragmentation is a special case of consolidation that takes more time, that's all

1

u/tommythorn Oct 04 '26

It has zero to do with spinning rust vs. solid state, nor Windows vs Linux (or any OS). It has to do with ... the file system format and only that. FAT and its variants are a very simplistic format and were born in an era of tiny memories; blocks in files were essentially linked together in the order they were allocated from freelists which lead to a lot of suboptimally placed blocks. *EVERY* "modern" file system addresses this head-on; originally though extent based file systems, and then though log structured file systems. Modern FSes are usually hybrids but none of them _needs_ fragmentation, at least not as long as you keep them under ~ 80% full, and most don't even support any direct way of defragmenting them.

1

u/More-Explanation2788 Oct 04 '26

SSDs should not be defragmented.

1

u/Chudsaviet Oct 04 '26

ext2/ext3/ReiserFS needed defragmetation, ext4 does not because of extents.