Logo

dev-resources.site

for different kinds of informations.

Automatically Monitor and Manage RAM Usage for Next.js Development

Published at
1/8/2025
Categories
nextjs
performance
bash
linux
Author
anggakswr
Categories
4 categories in total
nextjs
open
performance
open
bash
open
linux
open
Author
9 person written this
anggakswr
open
Automatically Monitor and Manage RAM Usage for Next.js Development

When working with heavy frameworks like Next.js 14 and above, you might notice significant memory usage, especially during development. If your system has limited resources, running yarn dev or pnpm dev can sometimes cause your machine to hang or become unresponsive. In this article, we will create a simple Bash script to monitor RAM usage and automatically stop the development server if the usage exceeds a defined limit.

Why Monitor Memory Usage?

High RAM usage can slow down your system, leading to decreased productivity and potential data loss if the machine crashes. By automating the process of stopping heavy processes when memory usage crosses a certain threshold, you can prevent such issues and ensure smoother development.

The Script

Here is the Bash script that monitors RAM usage and terminates processes when necessary:

#!/bin/bash

# RAM usage limit (percentage)
LIMIT=95  # Change to 90 if you want a lower limit

# Function to get current RAM usage
get_ram_usage() {
    free | awk '/Mem:/ {printf "%.0f", $3/$2 * 100}'
}

# Loop to monitor RAM periodically
while true; do
    RAM_USAGE=$(get_ram_usage)
    echo "RAM usage: $RAM_USAGE%"
    if (( RAM_USAGE >= LIMIT )); then
        echo "RAM has reached the limit of $LIMIT%. Stopping node processes."
        killall -SIGINT node
    fi
    sleep 5  # Check every 5 seconds
done
Enter fullscreen mode Exit fullscreen mode

Copy the script and save it as monitor_ram.sh.

Explanation of the Script

  1. LIMIT: Sets the threshold for RAM usage. You can adjust this value as needed.
  2. get_ram_usage: Calculates current memory usage using the free command.
  3. Main Loop: Continuously monitors memory usage and stops all Node.js processes if the limit is exceeded.

How to Use the Script

Make the script executable:

chmod +x monitor_ram.sh
Enter fullscreen mode Exit fullscreen mode

Run the script:

./monitor_ram.sh
Enter fullscreen mode Exit fullscreen mode

The script will monitor your RAM usage and stop the development server if the usage exceeds the defined limit.

Here’s a screenshot of the script in action:

Image description

In this example, when the RAM usage hits 95%, the script successfully terminates the Node.js processes, reducing the RAM usage and keeping the system responsive.

Conclusion

This script provides a simple and effective way to prevent system crashes due to high memory usage during Next.js development. Although the example provided here is specifically for Linux-based systems like Debian or Ubuntu, you can adapt the script for macOS or Windows as well. If the Bash script doesn't work out of the box, you can always ask ChatGPT for help to tailor it to your operating system.

Moreover, the concept isn't limited to monitoring RAM for Next.js. You can modify the script to work with other technologies by following the same principle: monitor resource usage and terminate services or processes that are consuming too much memory. Feel free to experiment and expand on this idea!

If you have any questions or suggestions, let me know in the comments. Happy coding!

bash Article's
30 articles in total
Favicon
Understanding Linux Shells: Interactive, Non-Interactive, and RC Files
Favicon
Fixing Linux Backup Sync Issues for exFAT Compatibility
Favicon
Ergonomic Pyhon Text Piping Solution for Linux Shell with pypyp and uv
Favicon
Changing the Login Shell with Security and Interactivity
Favicon
Final Bash Script Series Mastering Remote Server Management and Web App Deployment
Favicon
Writting a GH extension with AI
Favicon
Test GPIO pins on BeagleBone Black by toggling high to low
Favicon
NYP Infosec December CTF 2024 writeup!
Favicon
Building a Smart Heater Controller with Python, Docker, and Bluetooth #3
Favicon
The Complete Guide to Bash Commands
Favicon
How to Get Started with Bash Scripting for Automation
Favicon
Building a Basic Testing Framework in Bash 🐚
Favicon
Funny how, as I was writing this, I started wondering—could we make this pipeline even smarter? That’s when SonarQube came to mind. But can it even work with Bash scripts? I’d love to hear your thoughts!
Favicon
Domina Bash con ejemplos prácticos de Git
Favicon
Explaining Null Device or Black Hole Register in Vim
Favicon
A Media Server on Steroids - Walkthrough
Favicon
From FZF file preview to a browser for cht.sh to discovering the ideal solution
Favicon
Code. Battery notifier.
Favicon
🌟Simplifying User Account Management with Bash Scripting Day3🌟
Favicon
Become a Bash Scripting Pro in 10 Minutes: A Quick Guide for Beginners
Favicon
Automatically Monitor and Manage RAM Usage for Next.js Development
Favicon
Bash Script - Task Management
Favicon
Bash Your Tools
Favicon
Bash Script Series: Automating Log Analysis with Bash Script or Shell Script
Favicon
The Most Interesting Mostly Tech Reads of 2024
Favicon
🚀 Automating Process Monitoring & Restarting with Bash Scripting Day4! 🔧
Favicon
🚀 RazzShell v1.0.1 is Here: Plugin Support, Enhanced Job Management, and More! 🌟
Favicon
Step-by-Step Guide: Assigning a Namecheap Domain to DigitalOcean Hosting with Nginx
Favicon
Simplify GitHub Workflow Management with My-GitHub-Manager
Favicon
How to play a bunch of lemmings

Featured ones: