Introduction
Memory management is a critical aspect of Linux system administration that directly affects system performance and stability. Understanding how memory is allocated, used, and reported in Linux systems enables administrators to optimize performance, troubleshoot issues, and ensure sufficient resources for applications.
In this lab, you will learn how to monitor and analyze memory usage in Linux systems using command-line tools. You will explore the free command, which provides essential information about system memory allocation and usage. By mastering these tools, you will gain the skills needed to effectively monitor system memory resources and make informed decisions about system optimization.
By the end of this lab, you will be able to check system memory status, interpret memory statistics, and create basic memory usage reports, which are fundamental skills for any Linux user or system administrator.
Using the Basic free Command
The free command is a fundamental Linux utility that displays the amount of free and used memory in your system. This information helps you understand the current memory state of your system and determine if you have enough resources for your applications.
Running the Basic free Command
Open your terminal in the default workspace at /home/labex/project and execute the basic free command:
free
You should see output similar to this:
total used free shared buff/cache available
Mem: 8000000 3000000 1000000 25000 4000000 4700000
Swap: 0 0 0
The output shows memory values in bytes, which can be difficult to read. For a more readable format, use the -h option (human-readable):
free -h
Now the output will look more like this:
total used free shared buff/cache available
Mem: 7.6Gi 2.9Gi 954Mi 24Mi 3.8Gi 4.5Gi
Swap: 0B 0B 0B
This format displays memory sizes with appropriate units (KB, MB, GB), making it much easier to interpret.
Creating a Directory for Your Work
Let's create a directory to store the files you'll be creating during this lab:
mkdir -p ~/project/memory_data
Saving Memory Statistics to a File
Now, let's save the output of the free -h command to a file for future reference:
free -h > ~/project/memory_data/memory_stats.txt
To verify that the file was created and contains the expected information, use the cat command:
cat ~/project/memory_data/memory_stats.txt
You should see the same output that was previously displayed in your terminal.
Understanding Memory Output Columns
Now that you have the basic memory statistics, let's understand what each column in the free command output means. This knowledge will help you interpret system memory status more effectively.
Memory Output Columns Explained
Open the memory statistics file you created in the previous step:
cat ~/project/memory_data/memory_stats.txt
The output contains several columns:
total: The total amount of memory (RAM) installed on the system.
used: The amount of memory currently being used by the system.
free: The amount of memory not currently being used for anything.
shared: Memory shared by multiple processes, often used by tmpfs (temporary file systems) and shared memory segments.
buff/cache: Memory used by kernel buffers and page cache:
- buffers: Memory used by the kernel to temporarily store data before writing to disk
- caches: Memory used to store data read from disk for faster access
available: An estimate of how much memory is available for starting new applications without using swap.
Creating a Memory Report
Now, let's create a detailed memory report explaining what each column means and recording the current values. Create a new file named memory_report.txt:
nano ~/project/memory_data/memory_report.txt
In the nano editor, add the following content (replace the placeholder values with the actual values from your system):
Memory Report - Created on $(date)
total: [Your value here]
The total amount of physical RAM installed on the system.
used: [Your value here]
The amount of memory currently being used by applications and the operating system.
free: [Your value here]
The amount of memory that is completely unused and available.
shared: [Your value here]
Memory used by tmpfs and shared memory segments that can be accessed by multiple processes.
buff/cache: [Your value here]
Memory used for kernel buffers and disk cache to improve system performance.
available: [Your value here]
An estimate of how much memory is available for starting new applications without swapping.
To get the current values, you can refer to your previously saved memory_stats.txt file.
After adding the content, save the file by pressing Ctrl+O followed by Enter, then exit nano by pressing Ctrl+X.
Viewing Memory in Different Units
The free command allows you to display memory in different units. Let's create a file showing memory in various units:
free -b | head -2 > ~/project/memory_data/memory_units.txt
echo "Memory in bytes (above)" >> ~/project/memory_data/memory_units.txt
echo "" >> ~/project/memory_data/memory_units.txt
free -k | head -2 >> ~/project/memory_data/memory_units.txt
echo "Memory in kilobytes (above)" >> ~/project/memory_data/memory_units.txt
echo "" >> ~/project/memory_data/memory_units.txt
free -m | head -2 >> ~/project/memory_data/memory_units.txt
echo "Memory in megabytes (above)" >> ~/project/memory_data/memory_units.txt
echo "" >> ~/project/memory_data/memory_units.txt
free -g | head -2 >> ~/project/memory_data/memory_units.txt
echo "Memory in gigabytes (above)" >> ~/project/memory_data/memory_units.txt
View the file to see memory represented in different units:
cat ~/project/memory_data/memory_units.txt
This helps you understand how to display memory information in the most appropriate unit for your needs.
Monitoring Memory in Real-time
System administrators often need to monitor memory usage over time to detect trends or troubleshoot issues. In this step, you will learn how to monitor memory usage in real-time and create a simple script to log memory usage at regular intervals.
Using the watch Command with free
The watch command allows you to run a command periodically and display its output. This is very useful for monitoring memory usage in real-time:
watch -n 2 free -h
This command runs free -h every 2 seconds and shows the updated output. You should see the memory statistics updating in real-time.
To exit the watch command, press Ctrl+C.
Using free with Interval Option
The free command itself can display memory usage at regular intervals. The syntax is:
free -s [seconds] -c [count]
Where:
-sspecifies the interval in seconds between updates-cspecifies how many updates to display
Let's monitor memory usage every 3 seconds for a total of 4 updates:
free -h -s 3 -c 4
This will display the memory statistics 4 times, with a 3-second pause between each update.
Creating a Memory Monitoring Script
Let's create a script that logs memory usage information at regular intervals. First, create a new script file:
nano ~/project/memory_data/memory_monitor.sh
Add the following content to the file:
#!/bin/bash
## Create a log file with the current date and time
log_file=~/project/memory_data/memory_log_$(date +%Y%m%d_%H%M%S).txt
## Write header to the log file
echo "Memory Usage Log - Started at $(date)" > $log_file
echo "Monitoring every 5 seconds for 5 readings" >> $log_file
echo "----------------------------------------" >> $log_file
## Loop 5 times, capturing memory info every 5 seconds
for i in {1..5}; do
echo "Reading $i - $(date)" >> $log_file
free -h >> $log_file
echo "----------------------------------------" >> $log_file
sleep 5
done
echo "Monitoring completed at $(date)" >> $log_file
echo "Log saved to $log_file"
Save the file by pressing Ctrl+O, then Enter, and exit with Ctrl+X.
Now, make the script executable:
chmod +x ~/project/memory_data/memory_monitor.sh
Run the script:
~/project/memory_data/memory_monitor.sh
The script will run for about 25 seconds (5 seconds × 5 readings) and create a log file with memory usage information. After it completes, you'll see a message indicating where the log file is saved.
To view the log file:
cat ~/project/memory_data/memory_log_*
This log file shows how memory usage changes over time, which is valuable for troubleshooting memory-related issues or understanding memory usage patterns.
Exploring Advanced Memory Information
Linux provides more detailed memory information beyond what the basic free command shows. In this step, you will explore additional sources of memory information and create a comprehensive memory summary.
Exploring /proc/meminfo
The /proc/meminfo file contains detailed memory information about your system. Let's examine it:
head -20 /proc/meminfo
This file contains dozens of memory-related values, including:
MemTotal: Total usable RAMMemFree: Free memoryMemAvailable: Available memoryBuffers: Memory used by kernel buffersCached: Memory used for file cachingSwapTotal: Total swap spaceSwapFree: Free swap space
Let's extract some key information from this file:
grep -E "MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapTotal|SwapFree" /proc/meminfo > ~/project/memory_data/meminfo_excerpt.txt
View the extracted information:
cat ~/project/memory_data/meminfo_excerpt.txt
Creating a Comprehensive Memory Summary
Now, let's create a script that generates a comprehensive memory summary report. Create a new script file:
nano ~/project/memory_data/create_summary.sh
Add the following content:
#!/bin/bash
## Set output file with timestamp
output_file=~/project/memory_data/memory_summary_$(date +%Y%m%d_%H%M%S).txt
## Create header
echo "LINUX MEMORY SUMMARY REPORT" > $output_file
echo "===========================" >> $output_file
echo "Date: $(date)" >> $output_file
echo "" >> $output_file
## Basic memory statistics
echo "BASIC MEMORY STATISTICS:" >> $output_file
free -h >> $output_file
echo "" >> $output_file
## Detailed memory information
echo "DETAILED MEMORY INFORMATION:" >> $output_file
echo "Total RAM: $(grep MemTotal /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "Free RAM: $(grep MemFree /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "Available RAM: $(grep MemAvailable /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "Buffer memory: $(grep Buffers /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "Cache memory: $(grep "^Cached:" /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "" >> $output_file
## Swap information
echo "SWAP INFORMATION:" >> $output_file
echo "Total Swap: $(grep SwapTotal /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "Free Swap: $(grep SwapFree /proc/meminfo | awk '{print $2 " " $3}')" >> $output_file
echo "" >> $output_file
## Memory usage percentage calculation
total_mem=$(grep MemTotal /proc/meminfo | awk '{print $2}')
used_mem=$(grep MemTotal /proc/meminfo | awk '{print $2}')
used_mem=$((used_mem - $(grep MemFree /proc/meminfo | awk '{print $2}')))
used_mem=$((used_mem - $(grep Buffers /proc/meminfo | awk '{print $2}')))
used_mem=$((used_mem - $(grep "^Cached:" /proc/meminfo | awk '{print $2}')))
mem_percentage=$((used_mem * 100 / total_mem))
echo "MEMORY USAGE SUMMARY:" >> $output_file
echo "Memory usage percentage: ${mem_percentage}%" >> $output_file
echo "" >> $output_file
echo "Memory summary report generated at $output_file"
Save the file by pressing Ctrl+O, then Enter, and exit with Ctrl+X.
Make the script executable:
chmod +x ~/project/memory_data/create_summary.sh
Run the script to generate the summary report:
~/project/memory_data/create_summary.sh
After the script completes, view the generated summary report:
cat ~/project/memory_data/memory_summary_*
This comprehensive report gives you a detailed view of your system's memory status by combining data from multiple sources.
Creating a Simple Memory Status Command
Finally, let's create a simple one-line command that shows the current memory status in a concise format:
echo "Memory status: $(free -h | grep Mem | awk '{print "Total:"$2, "Used:"$3, "Free:"$4, "Available:"$7}')" > ~/project/memory_data/memory_status.txt
View the memory status:
cat ~/project/memory_data/memory_status.txt
This command extracts the most important information from the free command output and presents it in a compact format.
Summary
In this lab, you have explored essential techniques for monitoring and analyzing memory usage in Linux systems. You have progressed from basic memory reporting to advanced analysis, gaining valuable skills for system administration and troubleshooting.
Key concepts and skills you've learned:
Basic Memory Reporting: You used the
freecommand to view basic memory statistics and learned how to interpret the output columns.Understanding Memory Metrics: You explored what each memory metric means (total, used, free, shared, buff/cache, available) and how to display memory in different units.
Real-Time Monitoring: You learned how to monitor memory usage in real-time using the
watchcommand and created a script to log memory usage at regular intervals.Advanced Memory Analysis: You explored detailed memory information from the
/proc/meminfofile and created comprehensive memory summary reports.
These skills are essential for effective system administration, as memory management directly impacts system performance. By monitoring memory usage, you can:
- Identify memory-related performance issues
- Determine if a system needs more resources
- Understand how applications use memory
- Detect memory leaks or excessive memory usage
- Make informed decisions about system optimization
The techniques you've learned in this lab provide a solid foundation for memory management in Linux systems and can be expanded upon with more advanced monitoring tools and automation techniques.



