Bash Memory Usage (free)

Monitor system memory and swap usage

💾 What is free Command?

The free command displays the amount of free and used memory in your system, including RAM and swap space. It's essential for monitoring system performance and troubleshooting memory issues.


# Check memory usage
free

# Output shows total, used, and free memory
# in kilobytes by default
                                    

Output:

              total        used        free      shared  buff/cache   available
Mem:        8192000     3200000     2500000      100000     2492000     4500000
Swap:       2048000           0     2048000

Common free Options

📊

Human Readable

Display in MB, GB format

free -h
🔄

Continuous Monitor

Update memory stats regularly

free -s 5
📏

Specific Units

Show in megabytes or gigabytes

free -m
📈

Total Summary

Show total of RAM and swap

free -t

🔹 Basic Memory Check

Executing the free command without options provides a snapshot of your system's memory utilization, displaying figures in kilobytes for both physical RAM and swap space. The output is divided into rows for 'Mem' (physical memory) and 'Swap', with columns showing total, used, free, shared, buff/cache, and available memory. This immediate overview is the first step in diagnosing memory-related performance issues, helping you quickly determine if the system is under memory pressure, if swap is being used heavily, or if available RAM is sufficient for current workloads.

# Basic memory information
free

# Human-readable format (recommended)
free -h

# Show in megabytes
free -m

Output:

              total        used        free      shared  buff/cache   available
Mem:           7.8G        3.1G        2.4G         98M        2.4G        4.3G
Swap:          2.0G          0B        2.0G

🔹 Understanding Memory Columns

To accurately interpret the free command's output, you must understand the meaning of each column: 'total' (installed RAM), 'used' (currently consumed, including caches), 'free' (completely unused), 'shared' (memory used by tmpfs), 'buff/cache' (memory used by kernel buffers and page cache), and 'available' (estimate of memory available for starting new applications without swapping). The 'available' metric is often the most important, as it accounts for reclaimable cache. Misinterpreting 'used' (which includes cache) as entirely consumed can lead to incorrect conclusions about memory shortage.

Column Meanings:

  • total: Total installed RAM
  • used: Memory currently in use
  • free: Completely unused memory
  • shared: Memory used by tmpfs
  • buff/cache: Memory used for buffers and cache
  • available: Memory available for new applications
# View detailed memory breakdown
free -h

# The 'available' column is most important
# It shows memory that can be used by applications

🔹 Continuous Memory Monitoring

Use the -s (seconds) option with free to monitor memory usage dynamically over time, which is crucial for spotting trends, leaks, or the impact of specific events. The command free -s 5 will update the memory statistics every 5 seconds, providing a live feed to your terminal. This continuous mode is invaluable for observing how memory consumption changes during application startup, peak load times, or after deploying new services. It helps differentiate between temporary spikes and sustained memory pressure, informing capacity planning and troubleshooting efforts.

# Update every 2 seconds
free -h -s 2

# Update every 5 seconds
free -h -s 5

# Update 10 times with 3 second intervals
free -h -s 3 -c 10

Output (updates continuously):

              total        used        free      shared  buff/cache   available
Mem:           7.8G        3.2G        2.3G         98M        2.4G        4.2G
Swap:          2.0G          0B        2.0G

(updates every 2 seconds...)

🔹 Memory in Different Units

The free command allows you to display memory statistics in the unit that best suits your analysis needs using the -b (bytes), -k (kibibytes, default), -m (mebibytes), -g (gibibytes), or -h (human-readable) flags. The human-readable option (free -h) is particularly user-friendly, automatically scaling numbers to the most appropriate unit (e.g., GB, MB). For scripting and precise calculations, using a fixed unit like megabytes (-m) ensures consistent parsing. Choosing the right unit enhances both readability and the accuracy of your memory analysis.

# Bytes
free -b

# Kilobytes (default)
free -k

# Megabytes
free -m

# Gigabytes
free -g

# Human-readable (auto-selects unit)
free -h

Output (in megabytes):

              total        used        free      shared  buff/cache   available
Mem:           7982        3174        2456          98        2452        4380
Swap:          2048           0        2048

🔹 Total Memory Summary

Adding the -t flag to the free command appends a 'Total' line at the bottom of the output, summing the 'total' columns from both the physical memory (Mem) and swap space rows. This provides a single, comprehensive figure representing all memory resources (RAM + Swap) available to the system. Viewing this total helps in understanding the complete memory envelope for planning virtual machine allocation, assessing if swap space is appropriately sized relative to RAM, and getting a holistic view of system capacity for high-level resource reporting.

# Show total of RAM and swap
free -h -t

# Combine with other options
free -h -t -s 3

Output:

              total        used        free      shared  buff/cache   available
Mem:           7.8G        3.1G        2.4G         98M        2.4G        4.3G
Swap:          2.0G          0B        2.0G
Total:         9.8G        3.1G        4.4G

🔹 Wide Output Format

The -w option tells free to use a wide output format that splits the traditional 'buff/cache' column into two separate columns: 'buffers' and 'cache'. This provides a more granular view of how the kernel is utilizing memory. Buffers are for raw disk blocks, while cache stores pages from files. This detailed breakdown is especially useful for system administrators and performance tuners who need to understand exact memory allocation between different kernel subsystems, diagnose specific caching issues, or optimize system settings for specialized workloads like databases.

# Wide format with separate columns
free -h -w

# Buffers and cache shown separately
# Useful for detailed analysis

Output:

              total        used        free      shared     buffers       cache   available
Mem:           7.8G        3.1G        2.4G         98M        200M        2.2G        4.3G
Swap:          2.0G          0B        2.0G

🔹 Practical Memory Monitoring

Integrating free into scripts and combining it with other tools transforms it from a simple snapshot utility into a powerful monitoring solution. Practical examples include: creating a cron job that runs free -m hourly and logs to a file for trend analysis; piping output to awk to trigger an alert when available memory falls below a threshold (e.g., < 10%); or using watch -n 2 free -h for a real-time, auto-updating dashboard in your terminal. These practices enable proactive memory management and rapid response to potential shortages.

# Quick memory check
free -h

# Monitor memory while running a program
free -h -s 1 -c 60

# Save memory stats to file
free -h > memory_report.txt

# Check if swap is being used
free -h | grep Swap

Output:

Swap:          2.0G        512M        1.5G

(If swap 'used' is high, system may need more RAM)

🧠 Test Your Knowledge

Which option shows memory in human-readable format?