When your server starts swapping memory or your application begins to slow down due to high memory usage, it's often hard to pinpoint the exact cause. This is where real-time system monitoring becomes invaluable. In this article, we'll walk through a practical Python script that helps you identify and address high memory leaks by providing instant insights into your system's memory and CPU usage.
Most developers and system administrators have faced the frustrating scenario where their application starts using more memory over time, leading to performance degradation or even crashes. Traditional tools often require setup or are too complex to use on the fly. In this guide, we'll show you how to create a lightweight script that gives you real-time insights and alerts when memory usage exceeds a threshold.
Why Real-Time Monitoring Matters
Real-time monitoring is crucial because it allows you to catch issues before they escalate. For example, a memory leak might start subtly, but if you're not monitoring, you might not notice it until your system is already under stress. By having a script that runs in the background and reports memory usage, you can take corrective action immediately.
Getting Started with the Script
The script we'll build uses Python's psutil library to access system metrics. This library is cross-platform and provides a simple interface to system information. Let's start by installing the required package.
pip install psutil
Once installed, you can use the following script to monitor memory usage:
import psutil
import time
def monitor_memory(threshold=80):
print("Starting memory monitor...")
while True:
memory = psutil.virtual_memory()
if memory.percent > threshold:
print(f"⚠️ High memory usage: {memory.percent}%")
else:
print(f"Memory usage: {memory.percent}%")
time.sleep(5)
if __name__ == "__main__":
monitor_memory()
This script checks the memory usage every 5 seconds and prints a warning if it exceeds the specified threshold. You can adjust the threshold based on your system's needs.
Customizing and Extending the Script
While the script above is a good starting point, you can extend it to include more detailed metrics or even send alerts via email or Slack. Here's an example of how you might modify the script to include CPU usage and send an alert when memory usage is high:
import psutil
import time
import smtplib
from email.message import EmailMessage
def send_alert(subject, body, to_email):
msg = EmailMessage()
msg.set_content(body)
msg['Subject'] = subject
msg['From'] = 'your_email@example.com'
msg['To'] = to_email
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login('your_email@example.com', 'your_password')
server.send_message(msg)
def monitor_system(threshold=80):
print("Starting system monitor...")
while True:
memory = psutil.virtual_memory()
cpu = psutil.cpu_percent(interval=1)
print(f"CPU: {cpu}%, Memory: {memory.percent}%")
if memory.percent > threshold:
alert_body = f"High memory usage detected: {memory.percent}%"
send_alert("System Alert: High Memory Usage", alert_body, "admin@example.com")
time.sleep(5)
if __name__ == "__main__":
monitor_system()
This version of the script includes CPU usage and sends an email alert when memory usage is high. You can customize the alert mechanism to fit your environment.
Conclusion
By using Python and tools like psutil, you can create a powerful system monitoring solution that fits your needs without any complex setup. Whether you're managing a single server or a fleet of machines, real-time monitoring helps you stay proactive and avoid downtime.
If you're looking for a ready-to-use solution that offers more features and is easy to set up, consider the Disk Usage Reporter. It provides instant insights into your system's performance and is designed for developers and system administrators who need reliable monitoring tools.
Top comments (0)