#!/bin/bash # Config MAX_ARC=$((32 * 1024 * 1024 * 1024)) # 32 GB MIN_ARC=$((16 * 1024 * 1024 * 1024)) # 16 GB UPPER_FREE=$((20 * 1024 * 1024)) # 20 GB in KB LOWER_FREE=$((10 * 1024 * 1024)) # 10 GB in KB ARC_SYS="/sys/module/zfs/parameters/zfs_arc_max" # Function to get free memory in KB (Linux) get_free_mem() { awk '/MemAvailable/ {print $2}' /proc/meminfo } # Function to get current arc max get_arc_max() { cat $ARC_SYS } # Function to set arc max set_arc_max() { local val=$1 echo $val | sudo tee $ARC_SYS > /dev/null } while true; do free_mem_kb=$(get_free_mem) current_arc=$(get_arc_max) if [ "$free_mem_kb" -lt "$LOWER_FREE" ] && [ "$current_arc" -gt "$MIN_ARC" ]; then echo "Low memory ($free_mem_kb KB). Lowering ARC max to $MIN_ARC bytes." set_arc_max $MIN_ARC elif [ "$free_mem_kb" -gt "$UPPER_FREE" ] && [ "$current_arc" -lt "$MAX_ARC" ]; then echo "High memory ($free_mem_kb KB). Raising ARC max to $MAX_ARC bytes." set_arc_max $MAX_ARC fi sleep 60 # Check every 60 seconds done