Operations 7 min read

How to Fix Linux Memory and Disk Space Problems with Swap and File Management

This guide explains why Linux servers run out of memory or disk space, how to create and enable swap files, locate and remove large or numerous small files, use soft links to expand storage, and release space held by deleted files still opened by processes.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Fix Linux Memory and Disk Space Problems with Swap and File Management

Program Causing Memory Shortage

When memory is full, the system automatically kills the process using the most memory to keep the system running. Common reasons include heavy user traffic (which requires more memory), memory leaks in applications, network fluctuations, or internal testing environments.

Using Swap to Alleviate Memory Pressure

Swap uses a disk partition as temporary memory when physical RAM is insufficient.

# free -h
Mem:   1.9G  129M  1.7G  9.5M  104M  1.7G
Swap:  1.0G   0B  1.0G

Steps to create a swap file:

# dd if=/dev/zero of=/tmp/200m bs=1M count=200
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 0.350556 s, 598 MB/s
# ls -lh /tmp/200m
-rw-r--r--. 1 root root 200M Mar 16 11:45 /tmp/200m
# mkswap /tmp/200m
Setting up swapspace version 1, size = 204796 KiB
# swapon /tmp/200m
# free -h
Mem:   1.9G  131M  1.5G  9.5M  310M  1.7G
Swap:  1.2G   0B  1.2G

To view swap usage:

# swapon -s
Filename        Type        Size    Used    Priority
/dev/sda2       partition   1048572 0       -2
/tmp/200m        file        204796  0       -3

To disable the swap file:

# swapoff /tmp/200m
# free -h

Disk Cannot Write Data

2.1 Find Large Files and Move or Delete Them

# du -h /var/log/* | awk '/G/'
# find / -type f -size +1G | xargs rm

2.2 Locate Directories with Many Small Files

# df -i               # check inode usage
# find / -type d -size +70k
/usr/share/man/man3
/oldboy
# du -h /oldboy
400K    /oldboy

2.3 Use Soft Links to Extend Disk Space

Mount a new disk and move large log files to it, then create a symbolic link from the original location.

# mount /dev/sdc /data
# ll -h /var/log/10G
-rw-r--r--. 1 root root 9.8G Mar 16 11:57 /var/log/10G
# ln -s /data/10G /var/log/10g
# ll /var/log/10g
lrwxrwxrwx. 1 root root 9 Mar 16 12:16 /var/log/10g -> /data/10G
# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        19G  2.4G   17G  13% /
/dev/sdc         2.0T 9.8G   2.0T   1% /data

Deleted Files Do Not Free Disk Space

A file is truly deleted only when its hard‑link and soft‑link counts drop to zero and no process holds it open.

Step 1: Simulate a Process Holding the File

# tail /data/10G

Step 2: Delete the File (space remains occupied)

# rm -rf 10G
# df -h

Step 3: Identify the Holding Process

# yum -y install lsof
# lsof | grep 10G
tail 3407 root 3r REG 8,32 10485760000 67 /data/10G (deleted)

Step 4: Terminate the Process to Release Space

# kill -9 3407
# df -h

After killing the process, the disk space previously occupied by the deleted file is reclaimed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Memory ManagementOperationsLinuxdisk space
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.