Determine whether a server needs CPU, memory, or disk space

Inspect resources with top, free, df, and du, and understand filesystems, mounts, and swap.

🎯 Introduction

A Node.js service may slow down because the CPU is busy or memory is under pressure. Logs may stop writing because the disk is full. Identify the affected resource before deciding what to do: this is more reliable than immediately restarting services or deleting files.

Using an Ubuntu server, this article inspects CPU and processes with top, memory and swap with free, and disk usage with df and du. It also introduces filesystems and mount points. By the end, you should be able to interpret key output and make an initial assessment of CPU, memory, or disk pressure.


🧱 Check CPU usage

The CPU executes program instructions. If it stays busy, programs may respond slowly because they cannot get enough processing time. Start by checking CPU usage for the whole server and individual processes.

top

top refreshes continuously; press q to quit. Here is simplified example output; actual numbers vary by server:

%Cpu(s): 85.0 us, 5.0 sy, 10.0 id
MiB Mem : 3920.0 total, 3400.0 used, 520.0 free

  PID USER      %CPU  %MEM COMMAND
 1234 ubuntu    95.0  12.5 node
 1420 root       2.0   1.0 nginx

Look at two areas first:

  • %Cpu(s): CPU usage across the server. us is time spent running user programs, sy is kernel work, and id is idle time. Only 10% idle in this sample indicates a busy CPU.
  • %CPU in the process list: CPU usage for an individual process. The example node process uses 95%, indicating substantial CPU time. PID is its process ID, and COMMAND is the program name.

Remember: 95% CPU for a process does not mean 95% of the entire multicore server is in use. By default, top usually measures process CPU usage against one logical CPU; a multithreaded process can even exceed 100%. Consider overall usage and duration before concluding that CPU capacity is insufficient.

To inspect the Node.js service managed by systemd earlier, first obtain its main PID:

systemctl show -p MainPID --value linux-notes.service

Find that PID in top and watch whether %CPU remains high. If overall CPU usage stays high and one process consistently uses substantial CPU time, you have evidence to investigate that process further.


🧠 Check memory and swap

Memory holds running programs and their data. Memory pressure can slow programs down; in severe cases, Linux may terminate a process because it has run out of memory.

Run:

free -h

Example output:

               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       2.5Gi       200Mi       60Mi       1.1Gi       1.0Gi
Swap:          2.0Gi       100Mi       1.9Gi

Key fields:

FieldMeaning
totalTotal memory.
usedMemory currently used, which is not the same as memory exclusively used by applications.
freeCompletely unused memory.
buff/cacheMemory used for buffers, caches, and related purposes.
availableEstimated memory available to new programs without substantial swapping.

Here, free is only 200Mi, but available is about 1Gi. Linux caches data in memory and can reclaim some of it when needed. Low free alone does not mean the server needs more memory. Watch whether available remains low and whether service problems occur at the same time.

What is swap for?

Swap supplements memory, using a disk file or partition to hold memory pages temporarily moved out of RAM. Under memory pressure, the system may use swap to free physical memory. Disk access is generally slower than RAM, so frequent swapping can slow programs down.

List enabled swap areas:

swapon --show

No output usually means swap is not enabled. Nonzero swap used in free -h does not necessarily indicate a current memory shortage: it only shows that some data is in swap and does not prove that frequent paging is happening now.

If you suspect that Linux terminated Node.js because of insufficient memory, inspect the kernel logs for related records:

sudo journalctl -k -b --no-pager | grep -Ei 'out of memory|oom-kill|killed process'

Matching records provide more direct evidence. No matches do not completely rule out memory problems at another time.


🛠 Check disk capacity and directory usage

Failed log writes or uploads sometimes mean the filesystem holding the file lacks free space, rather than a code defect.

First inspect the capacity of the filesystem containing your home directory:

df -h "$HOME"

Example output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        50G   48G  2.0G  96% /
  • Size: Total filesystem capacity.
  • Used: Space already used.
  • Avail: Space currently available.
  • Use%: Percentage of space used.
  • Mounted on: The mount point, where the filesystem joins the directory tree.

Usage is 96%, with only 2G available, so further inspection is warranted. df -h "$HOME" checks the entire filesystem containing $HOME, rather than measuring only the home directory.

Which directory uses the space?

df reports filesystem usage but does not identify the largest directories. Use du for that.

If your practice directory is $HOME/linux-lab, run:

du -sh "$HOME/linux-lab"
du -h -d 1 "$HOME/linux-lab" | sort -h

Example output:

20M    /home/ubuntu/linux-lab/src
150M   /home/ubuntu/linux-lab/logs
600M   /home/ubuntu/linux-lab/data
770M   /home/ubuntu/linux-lab

The first command totals the directory's disk usage. The second lists its total and immediate subdirectory sizes, then sort -h sorts by human-readable size units. Here, data is largest, making it the next place to inspect.

How do df and du differ? df inspects space across a filesystem; du measures files below a specified directory. A filesystem using 48G and a log directory using only 150M are compatible results: other directories may use substantial space.

If logs or service data under /var may be filling the disk, run:

sudo du -x -h -d 1 /var | sort -h

-x prevents crossing filesystem boundaries. Finding a large directory does not make it safe to delete: logs, databases, and container data have different management requirements. Identify the data first, then follow the service's cleanup or retention rules.

Why can file creation fail when disk space remains?

Some filesystems also have a limited supply of inodes. An inode is a structure used to record file information. Large numbers of small files can exhaust inodes before disk space runs out.

df -i "$HOME"

Check IUse% for inode usage and IFree for remaining inodes. Exhausted inodes can prevent new files even when df -h reports free space.

Note: If df reports substantially more used space than files measured with du on the same filesystem, possible causes include different measurement scopes, filesystem overhead, or deleted files still held open by processes. If lsof is installed, sudo lsof +L1 can help identify the last case. For now, recognizing that possibility is enough; you do not need to resolve it here.


🧰 Understand filesystems and mount points

Why does free space under $HOME not guarantee free space under /var/log? Different Linux directories can reside on different filesystems.

Think of the terms this way:

  • Disk: A storage device that can be divided into partitions.
  • Filesystem: Organizes and manages files on storage, such as ext4.
  • Mounting: Attaches a filesystem to the Linux directory tree.
  • Mount point: The attachment location, such as / or /srv/data.

If the system disk is mounted at / and a separate data disk at /srv/data, files written under /srv/data generally use the data disk. Even if it is nearly empty, /var/log on the system disk may still be unable to accept logs.

Inspect devices, filesystems, and mounts:

lsblk -f
findmnt -T "$HOME"

lsblk -f shows block devices and filesystem information. findmnt -T "$HOME" identifies the mount containing the given path. Simplified output might be:

TARGET SOURCE    FSTYPE OPTIONS
/      /dev/vda1 ext4   rw,relatime

This means the home directory resides on an ext4 filesystem mounted at /. Device names and results differ between servers.

To list current mounts, you can also run:

mount | head -n 5

When troubleshooting, identify the affected file's path first, then inspect its filesystem. For failed log writes, start with df -h /var/log instead of checking only $HOME.


🔍 Identify the resource under pressure

Now combine these commands. Suppose a Node.js endpoint becomes slow while the server still accepts logins.

Step 1: Check CPU usage.

top

If overall CPU usage stays near capacity, identify processes that consistently consume CPU. If it is not busy, a slow response alone does not establish a CPU shortage.

Step 2: Check memory.

free -h
swapon --show

Persistently low available alongside service problems may indicate memory pressure. For sudden process exits, check OOM logs too. Swap usage alone is insufficient evidence.

Step 3: For failed log writes or disk errors, inspect the actual destination path.

df -h /var/log
df -i /var/log

Use% near 100% indicates low filesystem space; IUse% near 100% calls for inode inspection. Once you confirm a capacity problem, use du to find large directories.

These commands narrow the investigation; they do not automatically prove that you need more CPU, memory, or disk capacity. Networking, application logic, and databases can also slow applications. If resource metrics look normal, investigate other causes.


❓ Knowledge check

Q1: Does low free mean insufficient memory?

Not necessarily. Linux caches data in unused memory, and some cache is reclaimable. Start with available, then consider service behavior and trends over time.

Q2: Does 100% CPU for one Node.js process mean the server's CPU is fully used?

Not necessarily. Process CPU percentages in top usually use one logical CPU as the baseline. Especially on multicore servers, also inspect overall CPU usage.

Q3: Why do df and du report different usage?

df measures a filesystem; du measures files under a path. Scope, filesystem overhead, and deleted files still held open can all explain differences.


🧪 Exercise

Perform a resource inspection in your Ubuntu practice environment, observing without deleting files:

top
free -h
df -h "$HOME"
df -i "$HOME"
findmnt -T "$HOME"
du -h -d 1 "$HOME/linux-lab" | sort -h

If linux-lab does not exist yet, substitute an existing practice directory in the last command. Press q to quit top.

Record and explain:

  1. Which process has relatively high %CPU in top? Is overall CPU usage also high?
  2. What are free and available in free -h? Why is free alone insufficient?
  3. What are Use%, IUse%, and the mount point for the filesystem containing $HOME?
  4. Which practice subdirectory uses the most space? Did df or du provide that answer?

Success criteria: Explain each key number and the initial conclusion it supports, rather than merely running commands.


🎉 You can now inspect CPU, memory, and disk metrics to narrow down slow services, process exits, or failed writes before restarting anything blindly.