Skip to content

Maintenance and optimisation

Practical 5.8 maintenance-and-optimisation-annex.pdf · PDF

Has your phone ever said storage almost full? And have you ever installed a "cleaner" or "booster" app to fix it? Keep that app in mind. By the end of this page you will know what it really did — and why it is one of the most dangerous things you can install.

A machine that works every day gets dirty without anybody doing anything wrong. In Configuring the system by command you set deb-lab up. Now you keep it clean and fast, with the system's own tools — and you learn to judge a tool before you hand it the keys.

What you will learn

  • Measure disk use before and after a clean-up, and find what takes the space with du and ncdu
  • Clean a Debian system with its own tools: apt clean, apt autoremove, journalctl --vacuum-time
  • Read how long the start-up takes, and which unit is slowest, with systemd-analyze
  • Explain why a hard disk is defragmented and an SSD gets TRIM instead
  • Name the Windows tools for the same jobs: Storage Sense, Startup apps, Optimize Drives, DISM and sfc
  • Evaluate a maintenance utility with five questions before trusting it with administrator rights
  • Why a "cleaner" is a perfect target for attackers

What maintenance is

Back in the kitchen. A kitchen that cooks every day collects empty boxes in the pantry, old order tickets on the spike, and tools nobody uses on the countertop.

Maintenance is the regular tidy-up that keeps a system working well: nothing new is cooked, but tomorrow's service is faster. Optimisation goes one step further: arrange things so the system does less work for the same result.

A system collects dirt in three places:

Where What piles up In the kitchen
Disk (pantry) Downloaded packages, old logs, caches, packages nobody needs Empty boxes on the shelves
Start-up Services that start at boot and are never used Tools laid out every morning for dishes off the menu
The drive itself Pieces of files spread over an HDD; blocks an SSD does not know are free One recipe's ingredients on six shelves

Who you let in: the CCleaner story

CCleaner was, for years, the most famous cleaner for Windows. In July 2017 the antivirus company Avast bought its maker, Piriform. On 15 August 2017, version 5.33 was published on the official site, signed with the official certificate. It had a backdoor inside: attackers had broken into Piriform's build machines. About 2.27 million people installed it before it was found a month later.

They had all done the "right" thing: official site, signed installer. Why was a cleaner such a good target? Because it runs as administrator, on millions of machines, and nobody watches what a cleaner deletes.

A maintenance tool is a stranger you give the keys of the whole kitchen to. Before you install one, ask five questions:

  1. What does it do that the system cannot already do?
  2. Who makes it, and where does it come from?
  3. What rights does it ask for?
  4. Can I undo what it does?
  5. Is it still maintained?

Careful: boosters and registry cleaners

"My PC is slow, I need a cleaner / a RAM booster / a registry cleaner." Most of them promise what the system already does. Microsoft's own position is that it does not support registry cleaners (support article KB2563254): they change things Windows needs, and the gain cannot be measured.

Cleaning the disk: measure, find, clean, measure

The golden rule: measure before you touch anything. Otherwise you will never know what the clean-up achieved.

flowchart LR
    M1["<b>1 · Before</b><br>df -h /"] --> F["<b>2 · Find</b><br>sudo ncdu /"]
    F --> C["<b>3 · Clean</b><br>apt clean · apt autoremove<br>journalctl --vacuum-time"]
    C --> M2["<b>4 · After</b><br>df -h /<br>the difference is the result"]

1 · Before. On the 25 GB disk of deb-lab, typically 6–9 GB are used. Write down Size, Used, Avail and Use%.

df -h /

2 · Find the space. du (disk usage) tells you how big a directory is. ncdu is du with a menu: you walk the tree with the arrows, Enter goes into a directory, q quits.

sudo apt install ncdu
sudo ncdu /          # without sudo, many directories cannot be read

/usr is always the biggest: that is the system itself. /var is where the dirt is: caches and logs.

3 · Clean, with Debian's own tools:

sudo du -sh /var/cache/apt/archives   # the .deb files APT downloaded
sudo apt clean                        # deletes them: they are already installed
sudo apt autoremove                   # removes packages nothing needs any more: READ THE LIST
sudo journalctl --disk-usage          # e.g. Archived and active journals take up 8M in the file system.
sudo journalctl --vacuum-time=2weeks  # keep only the last two weeks of logs
Command What it removes Risk
apt clean Downloaded package files (.deb) that are already installed None: you can download them again
apt autoremove Packages installed as dependencies that nothing needs now It removes software: read the list before Y
journalctl --vacuum-time=2weeks Log entries older than two weeks You lose old history (see Logs and journald)

None of the three touches your own files. autoremove is the only one that removes software, so it is the one to read.

4 · After. df -h / again. On a young install the gain is often small — tens or hundreds of MB.

Careful: nothing freed is still a result

"Nothing was freed, so the commands did not work." A healthy, young system has little to clean. The lesson is the measurement: you only know because you looked before and after. And a cleaner frees disk: speed only changes if the disk was nearly full.

The start-up: a stopwatch on the relay race

Remember the boot relay race from Unit 1: firmware → bootloader → kernel → services. systemd-analyze puts a stopwatch on it.

systemd-analyze                  # Startup finished in ...s (kernel) + ...s (userspace) = ...s
systemd-analyze blame | head -5  # units, slowest first
systemd-analyze critical-chain   # the chain that decided the total time

With UEFI, the first line also shows (firmware) and (loader): the first two runners of the relay. A real sample of blame:

$ systemd-analyze blame | head -3
73ms systemd-localed.service
34ms systemd-hostnamed.service
33ms systemd-logind.service

Careful: blame does not find the guilty one

blame shows how long each unit took. But units start in parallel, so the slowest unit did not necessarily make anyone wait. critical-chain shows the chain of units that really decided the total.

To see everything that starts at boot — services you met in Services and systemd:

systemctl list-unit-files --type=service --state=enabled
systemctl status some-name       # what is this one, and does deb-lab need it?

Investigate before you disable. A service you do not recognise may be the one that brings up the network.

Defragmentation and TRIM

The hard disk: files in pieces

Picture a strip of 12 boxes on a hard disk. File A takes boxes 1–4, file B boxes 5–8. You delete A. Now you write C, six boxes long: it goes into 1–4 and 9–10.

flowchart LR
    subgraph HDD ["Hard disk after deleting A and writing C"]
        direction LR
        C1["C<br>1–4"] --- B["B<br>5–8"] --- C2["C<br>9–10"] --- F["free<br>11–12"]
    end

A file is fragmented when its pieces are stored in places that are not next to each other.

On a hard disk, the head has to travel between the pieces — a seek of about 10 ms each (Unit 1). Defragmentation moves the pieces together again. Linux rarely needs it: the ext4 file system avoids most fragmentation.

Does an SSD care where the pieces are? No: there is no head, no travel. Defragmenting it only adds writes, and flash cells wear with writes.

The SSD: which blocks are free?

An SSD has a different problem. When you delete a file, the file system only crosses its name off a list. The drive still believes those blocks hold data — and flash must be erased before it is written again.

TRIM is the message from the operating system to an SSD saying "these blocks are free": the drive can erase them in advance, so later writes are fast.

Hard disk (HDD) SSD
Problem Files in pieces: the head travels The drive does not know which blocks are free
Cure Defragmentation TRIM
Linux Rarely needed: ext4 avoids most of it fstrim, run every week by fstrim.timer
Windows Optimize Drives, weekly by default The same tool sends a retrim instead

Check it on deb-lab:

lsblk -D                       # zeros in DISC-GRAN and DISC-MAX: the disk does not accept discard
sudo fstrim -v /
systemctl status fstrim.timer  # "Discard unused filesystem blocks once a week"

On a virtual disk created without the discard option, fstrim answers:

fstrim: /: the discard operation is not supported

That is normal. Your VM's disk is a file on the host, so the host is the one that should take care of the real SSD. On a real SSD, fstrim -v / prints how many bytes were trimmed.

Careful: deleting does not empty the blocks

Deleting a file crosses its name off a list. The data stays on the drive until something overwrites it. That is why TRIM exists — and why "deleted" files can sometimes be recovered.

The same jobs on Windows

Job Debian Windows 11
Free disk space apt clean, apt autoremove, journalctl --vacuum-time Settings → System → Storage → Storage Sense; Disk Cleanup (cleanmgr)
What starts at boot systemctl list-unit-files --state=enabled Task Manager (Ctrl+Shift+Esc) → Startup apps
Drive health fstrim, fstrim.timer Optimize Drives (dfrgui)
Repair system files Reinstall the package with apt DISM, then sfc, as administrator

The two repair commands, in the order Microsoft gives, in a terminal opened as administrator:

DISM.exe /Online /Cleanup-image /Restorehealth
sfc /scannow

sfc (System File Checker) compares the protected system files with a known good copy and replaces the damaged ones. DISM first repairs the copy that sfc uses. sfc /scannow takes minutes; the usual final message is Windows Resource Protection did not find any integrity violations.

Judging two tools

In Part C of the Exercise sheet (Practical 5.8) you apply the five questions to two utilities from Debian's own repository: ncdu, and BleachBit, a graphical cleaner (sudo apt install bleachbit). Open BleachBit as a normal user, tick two or three items and press Preview — never Clean.

Question ncdu BleachBit
1 · What the system lacks du gives the same numbers; ncdu lets you walk the tree Cleans the caches of many programs at once; apt clean only does APT
2 · Origin Debian repository Debian repository
3 · Rights Reads only; sudo to see everything Deletes files; as root it can delete system files
4 · Undo Nothing to undo, unless you press d (delete) No: deleted is deleted. That is why Preview exists
5 · Verdict Keep: small, read-only by default Either answer, if argued. On a server: no

A verdict without a reason is worth nothing. "Remove it, the system already does it" is a complete answer. Then act on it: sudo apt purge name and sudo apt autoremove.

Key takeaways

  • Maintenance keeps a system working well; optimisation makes it do less work. Dirt piles up on the disk, in the start-up and in the drive itself.
  • Always measure before and after (df -h /), find the space with du or ncdu, and clean with the system's own tools: apt clean, apt autoremove (read the list), journalctl --vacuum-time.
  • systemd-analyze times the boot; blame lists durations, critical-chain shows what really decided the total.
  • An HDD is defragmented because its head travels; an SSD gets TRIM (fstrim, weekly by fstrim.timer) and is never defragmented.
  • On Windows: Storage Sense, Startup apps, Optimize Drives, and DISM then sfc /scannow to repair system files.
  • A maintenance tool runs with administrator rights: ask the five questions before installing it. CCleaner 5.33 came from the official site.

Quick check

1. What does sudo apt clean delete?
  • Downloaded package files that are already installed
  • Programs that you have not used for a month
  • Log files older than two weeks
  • The files in your desktop trash

apt clean empties /var/cache/apt/archives, where APT keeps the .deb files it downloaded. They are already installed and can be downloaded again, so it is risk-free.

2. Why is an SSD not defragmented?
  • It has no head to move, and extra writes wear it
  • It is too fast for fragmentation to be measured
  • It stores every file in a single block
  • It is read-only once the system is installed

Defragmentation saves the travel of a hard disk's head. An SSD has no moving parts, so there is nothing to gain, and its flash cells wear with every extra write.

3. What does TRIM tell a drive?
  • Which blocks are free and can be erased in advance
  • Which files are the biggest on the disk
  • That it can spin down to save energy
  • That it should compress the data it stores

Deleting a file only crosses its name off a list. TRIM tells the SSD which blocks are free, so it can erase them before they are needed and keep writes fast.

4. On Windows, which command checks protected system files and repairs them?
  • sfc /scannow
  • cleanmgr
  • dfrgui
  • Get-Counter

System File Checker compares the protected files with a known good copy and replaces the damaged ones. Run DISM first so that copy is healthy. cleanmgr frees disk space and dfrgui opens Optimize Drives.

5. What does the CCleaner 5.33 story teach about maintenance tools?
  • They are risky because they run with administrator rights on many machines
  • They are risky because free software is never safe
  • They are only risky when downloaded from unofficial sites
  • They are risky because they use too much RAM

CCleaner 5.33 came from the official site, signed. It was a perfect target because it runs as administrator on millions of machines. Fewer tools with admin rights means less risk.

6. Why should you run df -h / before cleaning the disk?
  • Without a number from before, you cannot know what the clean-up freed
  • The cleaning commands fail unless df has been run first
  • df prepares the disk so that apt clean is faster
  • df deletes the cache before the real clean-up starts

df only measures. The result of a clean-up is the difference between before and after; on a young system it may be small, and that is a valid result too.

7. systemd-analyze blame on Marta's workstation shows one unit at the top with 4 seconds. Select the two that are true.
  • That unit took 4 seconds, but it may not have delayed the others
  • systemd-analyze critical-chain shows what really decided the total boot time
  • Disabling that unit will make the boot exactly 4 seconds shorter
  • The unit is broken and must be disabled at once

blame lists durations, but units start in parallel, so a slow unit may run while others carry on. critical-chain shows the chain that set the total. Investigate with systemctl status before you disable anything.

Next: fill box H of your cheat sheet — What a development machine needs.