A tool that monitors memory usage on macOS and can automatically free up unused memory to improve system performance.
This tool helps maintain optimal system performance by:
- Monitoring memory usage at regular intervals
- Freeing up inactive memory when usage exceeds a threshold
- Providing both automatic and manual optimisation options
- Sending notifications when memory usage is high
On macOS, memory is categorized into different types:
- Active memory: Currently being used by applications
- Wired memory: Required by the system and cannot be freed
- Inactive memory: Previously used but can be reclaimed when needed
- Free memory: Available for immediate use
Over time, macOS may keep a lot of inactive memory that isn't immediately needed. The purge command forces the system to free up inactive memory, which can improve performance when memory usage is high.
- memory_optimiser.py: The main Python script that monitors and optimises memory
- start_memory_optimiser.sh: A shell script to easily launch the optimiser with different configurations
- install_memory_optimiser.sh: An installation script that sets up the optimiser on your system
- README.md: This file (comprehensive documentation)
- macOS (the
purgecommand is macOS-specific) - Python 3.6+
- psutil library (
pip install psutil)
You can run the memory optimiser directly from this directory:
# Make scripts executable
chmod +x memory_optimiser.py
chmod +x start_memory_optimiser.sh
# Run with default settings (monitor only)
./start_memory_optimiser.sh
# Run with automatic optimisation
./start_memory_optimiser.sh --auto
# Run with custom settings
./start_memory_optimiser.sh --interval 120 --threshold 70 --auto
# Run in test mode to see the interface
./start_memory_optimiser.sh --testTo install the memory optimiser on your system:
# Make the installation script executable
chmod +x install_memory_optimiser.sh
# Run the installation script
./install_memory_optimiser.shThis will:
- Copy the files to ~/memory-optimiser/
- Add an alias to your shell profile
- Optionally set up automatic startup at login
After installation, you can run the optimiser by typing memory-optimiser in your terminal.
The memory optimiser comes with several preset modes:
-
Default Mode: Checks memory usage every 5 minutes and notifies you when it exceeds 75%
./start_memory_optimiser.sh
-
Monitor Mode: Checks memory usage every minute and notifies you when it exceeds 50%
./start_memory_optimiser.sh --monitor
-
Gentle Mode: Conservative settings (threshold: 85%, interval: 10 min)
./start_memory_optimiser.sh --gentle
-
Aggressive Mode: Aggressive optimisation (threshold: 65%, interval: 2 min, auto)
./start_memory_optimiser.sh --aggressive
-
Test Mode: Simulates the interface without actually monitoring or optimising memory
./start_memory_optimiser.sh --test
You can also run any of these modes in the background:
./start_memory_optimiser.sh --gentle --background# Monitor memory usage every minute, notify when above 50%
./start_memory_optimiser.sh --monitor
# Conservative settings, run in background
./start_memory_optimiser.sh --gentle --background
# Aggressive optimisation, check every 2 minutes, auto-optimise at 65%
./start_memory_optimiser.sh --aggressive
# Test mode to simulate the interface
./start_memory_optimiser.sh --testYou can also run the Python script directly:
python3 memory_optimiser.pyBy default, the script will:
- Monitor memory usage every 5 minutes
- Alert you when memory usage exceeds 75%
- Only notify you without automatically freeing memory
usage: memory_optimiser.py [-h] [--interval INTERVAL] [--threshold THRESHOLD] [--auto] [--test]
macOS Memory Optimiser
optional arguments:
-h, --help show this help message and exit
--interval INTERVAL Check interval in seconds (default: 300)
--threshold THRESHOLD
Memory usage threshold percentage (default: 75.0)
--auto Automatically optimise memory when threshold is exceeded
--test Run in test mode to simulate memory optimisation
Monitor every minute and notify when memory exceeds 70%:
python3 memory_optimiser.py --interval 60 --threshold 70Automatically free up memory when it exceeds 80%:
python3 memory_optimiser.py --threshold 80 --autoThe test mode allows you to see what the interface would look like when the memory optimiser detects high memory usage. It shows:
- Current memory status (total, used, free, active, inactive, and wired memory)
- A simulated high memory usage warning
- A prompt asking if you want to optimise memory
- A simulation of what happens when you choose to optimise or not
This is useful for understanding how the tool works without waiting for high memory usage to occur.
To run the test mode:
./start_memory_optimiser.sh --testOr directly with the Python script:
python3 memory_optimiser.py --testTo run the script in the background:
nohup python3 memory_optimiser.py --auto > /dev/null 2>&1 &Or using the launcher script:
./start_memory_optimiser.sh --auto --backgroundTo stop the background process:
pkill -f memory_optimiser.pyThe purge command requires sudo privileges. When running in auto-optimisation mode, you'll be prompted for your password when needed.
If you want to run this script without password prompts, you can configure sudo to allow the purge command without a password by editing your sudoers file:
- Run
sudo visudoto edit the sudoers file - Add the following line (replace
yourusernamewith your actual username):yourusername ALL=(ALL) NOPASSWD: /usr/sbin/purge
Warning: Modifying the sudoers file can be dangerous. Only do this if you understand the security implications.
The script creates logs in the logs directory:
memory_optimiser.log: Contains detailed information about memory usage and optimisation activities
While freeing up inactive memory can temporarily improve performance, macOS memory management is designed to use available memory efficiently. Frequent use of the purge command may not always be beneficial for overall system performance.
Use this tool when you notice your system becoming sluggish due to high memory usage, particularly after running memory-intensive applications.
This software is provided as-is under the MIT License.