Monitoring Real-Time RAM Usage: A Step Towards Optimized System Performance

In this blog, I want to share a practical exercise I undertook to enhance my understanding of system performance by monitoring real-time RAM usage on a Linux system. This was both a learning experience and an opportunity to gain hands-on practice with shell scripting and system monitoring tools.

Why Monitor RAM Usage?

Understanding how your system utilizes its memory is crucial, especially when working on resource-intensive tasks such as running virtual machines, deploying applications, or executing complex algorithms. Monitoring RAM usage not only helps in identifying potential issues but also teaches you about the underlying system behavior and how to manage resources effectively.

The Script: A Learning Experience

As part of my practice, I wrote a shell script to calculate the percentage of RAM usage in real-time. Here’s how I approached the task:

Step 1: Retrieve Memory Data

Using the free command, I extracted details about the system's used and total memory. This step helped me familiarize myself with Linux commands and their output.

memUse=`free -m | head -2 | tail -1 | awk '{print $3}'`
totaluse=`free -m | head -2 | tail -1 | awk '{print $2}'`

Step 2: Calculate Usage Percentage

I used the expr command to calculate the percentage of memory being used. This calculation reinforced my understanding of arithmetic operations in shell scripting.

memUsage=`expr $memUse \* 100 / $totaluse`

Step 3: Log the Data

To make the data meaningful, I logged the memory usage percentage along with a timestamp into a .tsv file. This taught me the importance of maintaining logs for analysis.

echo "`date '+%r %D'` $memUsage" > mem_report.tsv

vim getmemusage.sh

memUse=`free -m | head -2 | tail -1 | awk '{print $3}'`
totaluse=`free -m | head -2 | tail -1 | awk '{print $2}'`
memUsage=`expr $memUse \* 100 / $totaluse`
echo "`date '+%r %D'` $memUsage" > mem_report.tsv

just run bash script

bash getmemusage.sh

then list file with

ls

the just read file with cat cmd

cat mem_report.tsv
02:50:28 AM 12/22/24    55

What I Learned

This practical exercise was an eye-opener in several ways:

  • Command-Line Proficiency: It deepened my understanding of Linux commands and how to parse their outputs.

  • Scripting Skills: Writing a functional script gave me confidence in my ability to automate tasks.

  • System Insights: Observing real-time memory usage highlighted how different tasks impact system performance.

How This Data Can Be Used

This kind of monitoring has multiple applications, such as:

  • Detecting memory leaks in applications.

  • Identifying the need for scaling resources.

  • Planning upgrades based on historical usage patterns.

Future Exploration

This exercise was just the beginning. I plan to expand on this by:

  1. Adding Notifications: Setting up alerts for when memory usage crosses a certain threshold.

  2. Data Visualization: Creating graphs and dashboards to better understand usage trends.

  3. Integrating with DevOps Tools: Incorporating this script into a broader infrastructure monitoring setup.

Conclusion

Through this practice, I not only gained technical knowledge but also a deeper appreciation for the importance of monitoring system performance. If you’re looking to improve your Linux skills, I highly recommend trying out exercises like this. It’s a hands-on way to learn, and the insights you gain are invaluable for both personal growth and professional development.