Linux automatically caches frequently accessed files in unused RAM through its page cache system, but we can take more control over this process. The kernel's virtual memory subsystem maintains this cache transparently, serving subsequent requests from RAM instead of disk.
# Check current cache usage
free -h
# Output example:
# total used free shared buff/cache available
# Mem: 3.9G 1.1G 500M 50M 2.3G 2.4G
# Swap: 2.0G 50M 2.0G
Using vmtouch - The Virtual Memory Toucher
vmtouch is a perfect tool for portable cache control:
# Install vmtouch on Debian/Ubuntu
sudo apt-get install vmtouch
# Load entire directory into cache
vmtouch -t /var/www/html/
# Check what's currently cached
vmtouch /var/www/html/main.css
# Output: /var/www/html/main.css is 92% cached
Direct POSIX Methods
For programmatic control, use POSIX_FADV_WILLNEED:
#include <fcntl.h>
#include <unistd.h>
void preload_file(const char *path) {
int fd = open(path, O_RDONLY);
posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED);
close(fd);
}
Tune swappiness and cache pressure for better results:
# Reduce swap tendency (0-100, lower values avoid swapping)
echo 10 > /proc/sys/vm/swappiness
# Increase cache retention (0-100, higher values keep cache longer)
echo 50 > /proc/sys/vm/vfs_cache_pressure
# Make changes permanent
echo "vm.swappiness = 10" >> /etc/sysctl.conf
echo "vm.vfs_cache_pressure = 50" >> /etc/sysctl.conf
Create a service to cache files at boot:
# /etc/systemd/system/precache.service
[Unit]
Description=Preload important files into RAM cache
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/bin/vmtouch -dl /var/www/html/ /usr/lib/large_library/
[Install]
WantedBy=multi-user.target
Use tools to verify your cache strategy:
# Install and use cachestat
sudo apt-get install perf-tools-unstable
sudo cachestat 1 # 1-second intervals
# Alternative using /proc
grep -E '^(Dirty|Writeback):' /proc/meminfo
For special cases consider:
- tmpfs: mount -t tmpfs -o size=1G tmpfs /mnt/ramdisk
- ramfs: mount -t ramfs ramfs /mnt/ramdisk (no size limits)
- mlock(): System call to prevent paging of critical files
Linux automatically caches frequently accessed files in RAM through its page cache mechanism. However, we can optimize this behavior for specific use cases. Here's how to actively control what gets cached:
# Check current cache usage
free -h
cat /proc/meminfo | grep -i cache
Using vmtouch - The Virtual Memory Toucher
This excellent tool gives you precise control over file caching:
# Install vmtouch on Debian/Ubuntu
sudo apt-get install vmtouch
# Load entire directory into cache
vmtouch -t /var/www/html/
# Lock files in RAM (prevent eviction)
vmtouch -l /var/www/html/important_files/
Kernel-Level Control with fincore
For more advanced control, examine and manipulate the page cache directly:
# Check which pages are cached
fincore /var/www/html/*.js
# Preload files using dd
dd if=/path/to/file of=/dev/null bs=1M
Systemd Service for Automatic Preloading
Create a service to cache files at boot:
# /etc/systemd/system/preload.service
[Unit]
Description=Preload web files into RAM
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/bin/vmtouch -dl /var/www/html/
[Install]
WantedBy=multi-user.target
Using preload Daemon
The preload package predicts and caches frequently used applications:
sudo apt install preload
sudo systemctl enable --now preload
RAM Disk with Transparent Fallback
For mission-critical files, consider tmpfs with automatic HDD fallback:
# /etc/fstab entry
tmpfs /var/www/html/critical tmpfs defaults,size=2G 0 0
# Mirror files with rsync
rsync -a --delete /var/www/html/critical/ /var/www/html-copy/
Kernel Parameter Tuning /h2>
Optimize cache behavior via sysctl:
# Increase cache pressure (lower = keep more cached)
vm.vfs_cache_pressure=50
# Reduce swap tendency (avoid caching to disk)
vm.swappiness=10
Essential commands to verify cache effectiveness:
# Check cached files
vmtouch -v /var/www/html/
# Monitor cache hits
sudo perf stat -e cache-references,cache-misses -a sleep 5
# Detailed page cache stats
cat /proc/meminfo | grep -i cached