This guide provides scripts and tips to optimize RAM usage on systems with Bash installed.
Use this script in a cron job to clean RAM if usage exceeds 90%.
#!/bin/bash
# Set the threshold for free memory in bytes (90% of total RAM)
THRESHOLD=$(( 90 * $(grep MemTotal /proc/meminfo | awk '{print $2}') / 100 ))
# Get current free memory
FREE=$(grep MemAvailable /proc/meminfo | awk '{print $2}')
# Clean RAM if free memory is below threshold
if [ $FREE -lt $THRESHOLD ]; then
echo "Cleaning RAM..."
sync && echo 3 > /proc/sys/vm/drop_caches
echo "RAM cleaned."
else
echo "No need to clean RAM."
fi
Make the script executable and schedule it to run every 5 minutes:
chmod +x clean_ram.sh
crontab -e
Add the following line to the crontab:
*/5 * * * * /path/to/clean_ram.sh
Verify if ZRAM is installed:
lsmod | grep zram
If not installed, use your package manager to install it.
Create a ZRAM device and set its size to 1 GB with compression:
sudo modprobe zram num_devices=1
sudo zramctl -f -s 1G -t 2
Set the swappiness value:
sudo sysctl -w vm.swappiness=10
Format and activate the ZRAM device as swap:
sudo mkswap /dev/zram0
sudo swapon /dev/zram0
Verify ZRAM usage:
sudo swapon -s
Create ZRAM swap space as 25% of physical RAM:
sudo zramctl --find --size $(($(free | awk '/^Mem:/{print $2}') / 4)) --mkswap
sudo swapon /dev/zram0
Use lightweight window managers like Xfce, LXDE, or Openbox to reduce memory usage.
Disable services to free up memory and reduce CPU usage:
sudo systemctl disable <service_name>
Use `htop` or `top` to monitor and close unused applications.
Adjust process priority with `nice`:
nice -n <priority> <command>
Free up memory by clearing disk cache:
sudo sync
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
Use `ulimit` to set limits on system resources for processes:
ulimit -m <memory_limit>
ulimit -t <cpu_time_limit>
These tips help optimize and clean up RAM on Bash-installed systems, improving performance and stability.
Volkan Kücükbudak