Dynamic swap space manager — auto-creates/removes 1GB swap chunks based on usage
- Shell 100%
- BAUTOSWAP_CHUNK_SIZE → BAUTOSWAP_BASE_CHUNK (bytes) - BAUTOSWAP_LOW_FREE → BAUTOSWAP_ADD_THRESH - BAUTOSWAP_HIGH_FREE → BAUTOSWAP_REMOVE_BUFFER |
||
|---|---|---|
| bautoswap | ||
| bautoswap.service | ||
| install.sh | ||
| LICENSE | ||
| README.md | ||
bautoswap
Dynamic swap space manager for Linux. Runs as a daemon, automatically creating and removing swap file chunks that scale in size.
How it works
- Chunks scale as powers of 2: 1GB → 2GB → 4GB → 8GB → 16GB ...
- Free swap < 512MB → adds next chunk
- Free swap > last chunk size + 1GB → removes last chunk
- No swap at all → creates first chunk immediately
- Adaptive polling: 128s when idle, halves on action, doubles back when idle
Chunk scaling
chunk_00 = 1GB
chunk_01 = 2GB
chunk_02 = 4GB
chunk_03 = 8GB
chunk_04 = 16GB
...
With max 8 chunks: up to 255GB total swap (1+2+4+8+16+32+64+128).
Dynamic removal threshold
Removal threshold adapts to the last chunk's size:
1 chunk (1GB): remove when free > 1GB + 1GB = 2GB
2 chunks (3GB): remove when free > 2GB + 1GB = 3GB
3 chunks (7GB): remove when free > 4GB + 1GB = 5GB
4 chunks (15GB): remove when free > 8GB + 1GB = 9GB
Adaptive timing
Action taken → interval halves (128→64→32→16→8→4→2→1s)
No action → interval doubles (1→2→4→8→16→32→64→128s)
Install
One-liner
curl -fsSL https://raw.githubusercontent.com/bradcarnage/bautoswap/main/install.sh | sudo bash
Manual
sudo cp bautoswap /usr/local/bin/bautoswap
sudo chmod +x /usr/local/bin/bautoswap
sudo mkdir -p /var/swapfiles
sudo chmod 700 /var/swapfiles
Usage
Systemd (recommended)
# /etc/systemd/system/bautoswap.service
[Unit]
Description=Dynamic swap space manager
After=local-fs.target
[Service]
Type=simple
ExecStart=/usr/local/bin/bautoswap
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
sudo systemctl enable --now bautoswap
Manual
sudo bautoswap
Configuration
All settings via environment variables:
| Variable | Default | Description |
|---|---|---|
BAUTOSWAP_BASE_CHUNK |
1073741824 (1GB) |
Starting chunk size in bytes (doubles each chunk) |
BAUTOSWAP_DIR |
/var/swapfiles |
Directory for swap files |
BAUTOSWAP_ADD_THRESH |
536870912 (512MB) |
Add chunk when free swap below this (bytes) |
BAUTOSWAP_REMOVE_BUFFER |
1073741824 (1GB) |
Remove when free > last chunk size + this buffer |
BAUTOSWAP_MAX_CHUNKS |
8 |
Maximum number of chunks |
BAUTOSWAP_MAX_INTERVAL |
128 |
Max seconds between checks |
BAUTOSWAP_MIN_INTERVAL |
1 |
Min seconds between checks |
Filesystem compatibility
- Uses
fallocatefor fast allocation - Falls back to
ddon CoW filesystems (btrfs, ZFS)
Log output
[2026-06-02 14:30:01] START: add_thresh=512M remove_buffer=1.0G base=1.0G max=8 interval=1-128s
[2026-06-02 14:30:01] STATUS: free=256M <512M, adding 1.0G chunk, interval=128s
[2026-06-02 14:30:01] ADD: /var/swapfiles/chunk_00 (1.0G), now 1 chunks, free=1.2G
[2026-06-02 14:30:02] STATUS: free=128M <512M, adding 2.0G chunk, interval=64s
[2026-06-02 14:30:02] ADD: /var/swapfiles/chunk_01 (2.0G), now 2 chunks, free=2.8G
[2026-06-02 14:35:01] STATUS: free=4.5G >3.0G, removing 2.0G chunk, interval=128s
[2026-06-02 14:35:01] REMOVE: /var/swapfiles/chunk_01 (2.0G), now 1 chunks, free=2.5G
License
MIT