内容大纲
内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
1. 内存性能调优概述
内存性能调优是通过调整内存参数、交换空间、缓存等,提高系统内存使用效率的过程。
风哥提示:
# 内存参数调优:调整vm参数
# 交换空间管理:优化swap使用
# 内存缓存优化:调整缓存策略
# 内存分配优化:优化内存分配
# 内存泄漏检测:检测内存泄漏
# 内存压缩:使用内存压缩技术
2. 内存参数调优
调整vm参数优化内存性能。
# 1. 查看vm参数
[root@localhost ~]# sysctl -a | grep vm
vm.swappiness = 60
vm.dirty_ratio = 30
vm.dirty_background_ratio = 10
vm.vfs_cache_pressure = 100
vm.min_free_kbytes = 65536
vm.overcommit_memory = 0
vm.overcommit_ratio = 50
# 2. 调整vm.swappiness参数
[root@localhost ~]# sysctl -w vm.swappiness=10
vm.swappiness = 10
# 3. 调整vm.dirty_ratio参数
[root@localhost ~]# sysctl -w vm.dirty_ratio=15
vm.dirty_ratio = 15
# 4. 调整vm.dirty_background_ratio参数
[root@localhost ~]# sysctl -w vm.dirty_background_ratio=5
vm.dirty_background_ratio = 5
# 5. 调整vm.vfs_cache_pressure参数
[root@localhost ~]# sysctl -w vm.vfs_cache_pressure=75
vm.vfs_cache_pressure = 75
# 6. 永久保存vm参数
[root@localhost ~]# cat > /etc/sysctl.d/99-memory-tuning.conf << 'EOF'
# 内存调优参数
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.vfs_cache_pressure = 75
vm.min_free_kbytes = 131072
vm.overcommit_memory = 1
vm.overcommit_ratio = 100
EOF
# 7. 应用vm参数
[root@localhost ~]# sysctl -p /etc/sysctl.d/99-memory-tuning.conf
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.vfs_cache_pressure = 75
vm.min_free_kbytes = 131072
vm.overcommit_memory = 1
vm.overcommit_ratio = 100
3. 交换空间管理
优化swap使用提高内存性能。
学习交流加群风哥微信: itpux-com
# 1. 查看swap信息
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 4194300 0 -2
# 2. 查看swap使用情况
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 7.6Gi 2.0Gi 4.0Gi 100Mi 1.6Gi 5.3Gi
Swap: 4.0Gi 0B 4.0Gi
# 3. 查看swap使用率
[root@localhost ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/sda2 partition 4194300 0 -2
# 4. 创建swap文件
[root@localhost ~]# dd if=/dev/zero of=/swapfile bs=1G count=4
4+0 records in
4+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 10.1234 s, 424 MB/s
# 5. 设置swap文件权限
[root@localhost ~]# chmod 600 /swapfile
# 6. 格式化swap文件
[root@localhost ~]# mkswap /swapfile
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=12345678-1234-1234-1234-123456789012
# 7. 启用swap文件
[root@localhost ~]# swapon /swapfile
# 8. 设置swap优先级
[root@localhost ~]# swapon -p 100 /swapfile
# 9. 配置开机自动挂载
[root@localhost ~]# cat >> /etc/fstab << 'EOF'
/swapfile swap swap defaults,pri=100 0 0
EOF
# 10. 查看swap优先级
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/swapfile file 4194300 0 100
/dev/sda2 partition 4194300 0 -2
# 11. 禁用swap
[root@localhost ~]# swapoff /swapfile
# 12. 启用swap
[root@localhost ~]# swapon /swapfile
4. 内存缓存优化
优化内存缓存提高性能。
更多视频教程www.fgedu.net.cn
# 1. 查看内存缓存
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 7.6Gi 2.0Gi 4.0Gi 100Mi 1.6Gi 5.3Gi
Swap: 4.0Gi 0B 4.0Gi
# 2. 查看详细内存信息
[root@localhost ~]# cat /proc/meminfo
MemTotal: 8192000 kB
MemFree: 4096000 kB
MemAvailable: 5432320 kB
Buffers: 102400 kB
Cached: 2048000 kB
SwapCached: 0 kB
Active: 2048000 kB
Inactive: 2048000 kB
Active(anon): 1024000 kB
Inactive(anon): 1024000 kB
Active(file): 1024000 kB
Inactive(file): 1024000 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Dirty: 1024 kB
Writeback: 0 kB
AnonPages: 1024000 kB
Mapped: 512000 kB
Shmem: 102400 kB
Slab: 204800 kB
SReclaimable: 102400 kB
SUnreclaim: 102400 kB
KernelStack: 10240 kB
PageTables: 10240 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 8285300 kB
Committed_AS: 2048000 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
Percpu: 1024 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 0 kB
DirectMap4k: 102400 kB
DirectMap2M: 2097152 kB
DirectMap1G: 6291456 kB
# 3. 清理页面缓存
[root@localhost ~]# sync
[root@localhost ~]# echo 1 > /proc/sys/vm/drop_caches
# 4. 清理目录项和inode缓存
[root@localhost ~]# sync
[root@localhost ~]# echo 2 > /proc/sys/vm/drop_caches
# 5. 清理所有缓存
[root@localhost ~]# sync
[root@localhost ~]# echo 3 > /proc/sys/vm/drop_caches
# 6. 设置脏页回写参数
[root@localhost ~]# sysctl -w vm.dirty_ratio=15
vm.dirty_ratio = 15
# 7. 设置脏页回写超时
[root@localhost ~]# sysctl -w vm.dirty_expire_centisecs=3000
vm.dirty_expire_centisecs = 3000
# 8. 设置脏页回写间隔
[root@localhost ~]# sysctl -w vm.dirty_writeback_centisecs=500
vm.dirty_writeback_centisecs = 500
5. 内存性能监控
使用free、vmstat、sar等工具监控内存性能。
# 1. 使用free监控内存
[root@localhost ~]# free -h -s 1
total used free shared buff/cache available
Mem: 7.6Gi 2.0Gi 4.0Gi 100Mi 1.6Gi 5.3Gi
Swap: 4.0Gi 0B 4.0Gi
total used free shared buff/cache available
Mem: 7.6Gi 2.0Gi 4.0Gi 100Mi 1.6Gi 5.3Gi
Swap: 4.0Gi 0B 4.0Gi
# 2. 使用vmstat监控内存
[root@localhost ~]# vmstat -s
8589934592 total memory
4294967296 used memory
4294967296 active memory
2147483648 inactive memory
4294967296 free memory
104857600 buffer memory
2147483648 swap cache
4294967296 total swap
0 used swap
4294967296 free swap
# 3. 使用sar监控内存
[root@localhost ~]# sar -r 1 3
Linux 5.14.0-362.el9.x86_64 (localhost.localdomain) 04/03/2026 _x86_64_ (4 CPU)
10:00:00 AM kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
10:00:01 AM 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0
10:00:02 AM 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0
10:00:03 AM 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0
Average: 4096000 4096000 50.00 102400 2048000 512000 6.25 2048000 2048000 0
# 4. 使用slabtop监控slab缓存
[root@localhost ~]# slabtop -o
Active / Total Objects (% used) : 123456 / 234567 (52.6%)
Active / Total Slabs (% used) : 1234 / 2345 (52.6%)
Active / Total Caches (% used) : 100 / 200 (50.0%)
Active / Total Size (% used) : 12345678 / 23456789 (52.6%)
OBJS ACTIVE USE OBJ SIZE SLABS OBJ/SLAB CACHE SIZE NAME
123456 123456 100% 1024 123 100 2048K dentry
56789 56789 100% 512 567 100 2048K inode
34567 34567 100% 256 345 100 1024K kmalloc-256
23456 23456 100% 128 234 100 512K kmalloc-128
12345 12345 100% 64 123 100 256K kmalloc-64
学习交流加群风哥QQ113257174
6. 实战案例
内存性能调优实战案例。
# 1. 查看内存信息
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 7.6Gi 2.0Gi 4.0Gi 100Mi 1.6Gi 5.3Gi
Swap: 4.0Gi 0B 4.0Gi
# 2. 调整vm参数
[root@localhost ~]# cat > /etc/sysctl.d/99-memory-tuning.conf << 'EOF'
# 内存调优参数
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.vfs_cache_pressure = 75
vm.min_free_kbytes = 131072
vm.overcommit_memory = 1
vm.overcommit_ratio = 100
vm.dirty_expire_centisecs = 3000
vm.dirty_writeback_centisecs = 500
EOF
# 3. 应用vm参数
[root@localhost ~]# sysctl -p /etc/sysctl.d/99-memory-tuning.conf
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
vm.vfs_cache_pressure = 75
vm.min_free_kbytes = 131072
vm.overcommit_memory = 1
vm.overcommit_ratio = 100
vm.dirty_expire_centisecs = 3000
vm.dirty_writeback_centisecs = 500
# 4. 创建swap文件
[root@localhost ~]# dd if=/dev/zero of=/swapfile bs=1G count=4
4+0 records in
4+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 10.1234 s, 424 MB/s
# 5. 设置swap文件权限
[root@localhost ~]# chmod 600 /swapfile
# 6. 格式化swap文件
[root@localhost ~]# mkswap /swapfile
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=12345678-1234-1234-1234-123456789012
# 7. 启用swap文件
[root@localhost ~]# swapon /swapfile
# 8. 配置开机自动挂载
[root@localhost ~]# cat >> /etc/fstab << 'EOF'
/swapfile swap swap defaults,pri=100 0 0
EOF
# 9. 创建内存调优脚本
[root@localhost ~]# cat > /usr/local/bin/memory-tuning.sh << 'EOF'
#!/bin/bash
# script.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn
# 内存调优脚本
LOG_FILE="/var/log/memory-tuning.log"
# 记录日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# 检查内存使用率
check_memory() {
MEMORY_USAGE=$(free | grep Mem | awk ‘{printf(“%.0f”), $3/$2 * 100.0}’)
if [ “$MEMORY_USAGE” -gt 80 ]; then
log “WARNING: Memory usage is ${MEMORY_USAGE}%”
# 清理缓存
log “Clearing page cache…”
sync
echo 1 > /proc/sys/vm/drop_caches
log “Page cache cleared.”
fi
}
# 检查swap使用率
check_swap() {
SWAP_USAGE=$(free | grep Swap | awk ‘{printf(“%.0f”), $3/$2 * 100.0}’)
if [ “$SWAP_USAGE” -gt 50 ]; then
log “WARNING: Swap usage is ${SWAP_USAGE}%”
fi
}
# 主函数
main() {
log “Starting memory tuning…”
check_memory
check_swap
log “Memory tuning completed.”
}
# 执行主函数
main
EOF
# 10. 设置脚本执行权限
[root@localhost ~]# chmod +x /usr/local/bin/memory-tuning.sh
# 11. 配置定时任务
[root@localhost ~]# echo “*/5 * * * * root /usr/local/bin/memory-tuning.sh” > /etc/cron.d/memory-tuning
# 12. 重启cron服务
[root@localhost ~]# systemctl restart crond
# 13. 测试调优脚本
[root@localhost ~]# /usr/local/bin/memory-tuning.sh
# 14. 查看日志
[root@localhost ~]# tail -f /var/log/memory-tuning.log
[2026-04-03 10:00:00] Starting memory tuning…
[2026-04-03 10:00:01] Memory tuning completed.
提示
更多学习教程公众号风哥教程itpux_com
内存性能调优需要根据实际应用场景进行调整。对于数据库应用,建议增加swap空间和调整vm参数;对于Web应用,建
from PG视频:www.itpux.com
议优化缓存策略。定期监控内存使用情况,及时调整调优策略。
