centos

CentOS Swap最佳实践

小樊
40
2025-11-01 11:32:14
栏目: 智能运维

Creating Swap Space
Swap can be configured as a dedicated partition or a swap file, depending on system requirements and disk layout. For partition-based setup, use fdisk or parted to create a new partition (e.g., /dev/sdb2), then format it with mkswap /dev/sdb2 and enable it via swapon /dev/sdb2. To make it persistent, add an entry to /etc/fstab (e.g., /dev/sdb2 swap swap defaults 0 0). For file-based setup, use fallocate -l 4G /swapfile (or dd if=/dev/zero of=/swapfile bs=1M count=4096 for older systems) to create a 4GB file, set strict permissions with chmod 600 /swapfile, format it with mkswap /swapfile, and enable it with swapon /swapfile. Add /swapfile none swap sw 0 0 to /etc/fstab for automatic mounting at boot.

Determining Optimal Swap Size
The ideal swap size depends on physical memory (RAM) and workload characteristics. Common recommendations include:

Tuning Swappiness Parameter
The vm.swappiness parameter controls the kernel’s tendency to use swap (range: 0–100). A lower value reduces swap usage (preferring to keep data in RAM), while a higher value increases it (useful for freeing RAM quickly). For most CentOS servers (especially production), set swappiness between 10–30 to minimize swap-induced latency. To adjust:

Performance Optimization Techniques

Monitoring and Maintenance
Regularly monitor swap usage to identify potential issues (e.g., excessive swapping leading to performance bottlenecks). Key commands include:

Important Considerations

0
看了该问题的人还看了