When migrating a MacBook from HDD to SSD, I did a block-level copy using dd
. I believe for optimal flash lifetime and performance, it would be the best to issue a TRIM for every unused block in the new filesystem to inform the SSD controller (so it won't confuse my dd
copy with a large file that occupies the entire disk).
Is it possible to TRIM the free space manually for an APFS volume on macOS (like FreeBSD's trim
or Linux's fstrim
)? I've read that on HFS+, macOS's fsck
has a TRIM pass, so TRIM can be triggered manually by running fsck -fy
in single-user mode, and one should see a "trimming unused blocks" message. But it doesn't support APFS (I tried it and I didn't see any message about TRIM).
Best Answer
On APFS, macOS automatically performs a TRIM operation on the free disk space on boot via the AFPS Space Manager (spaceman). Therefore, as long as TRIM is enabled on the system (via
trimforce
for 3rd-party SSDs, see karolus's answer), you don't have to worry about it. There is no command and no need to manually trim the unused blocks.Verification
If you want to personally verify the operation of boot time TRIM, you can search for spaceman-related messages in your boot log.
Execute the following command as root. It lists all messages from the kernel (
--predicate "processID == 0"
) since the beginning of this month (--start $(date "+%Y-%m-01"
) and search for messages that contain "spaceman" (grep spaceman
).And you can see messages like these
The lines are too long to read, I slightly edited it to make it shorter. You should see logs lines that mentions "TRIM",
As you can see, macOS just trimmed 28059445 unused blocks today. Assuming APFS uses a 4 KiB block size, it means the SSD has discarded 107 GiB of free space. And yes, it roughly matches the free space on my disk. Works as expected!