sar查看swap占用--linux030 Linux 使用 sar -S 查看今日 / 昨日 Swap 历史占用与峰值完整教程前言日常跑基因组组装、大数据运算、批量任务时服务器极易出现物理内存不足大量业务数据存入 Swap 交换分区引发程序卡顿、进程 D 态卡死、任务超时等问题。top、free仅能查看实时状态无法追溯历史最高 Swap 占用峰值、判定具体哪个时间段内存压力拉满。sar作为 Linux 系统自带的系统活动统计工具依托sysstat组件自动记录全天资源日志搭配-S参数可精准查看 Swap 全天使用趋势、历史峰值、空闲容量是排查大内存任务 Swap 溢出、服务器内存瓶颈最实用的命令本文适配 CentOS、Rocky、Ubuntu 全系列服务器。环境准备安装依赖组件sar 命令隶属于sysstat工具包极简安装即可使用CentOS/RHEL 系列yum install sysstat -yUbuntu/Debian 系列apt install sysstat -y确认日志采集开启主流服务器默认开启日志采集查看配置cat /etc/default/sysstat确保配置为ENABLEDtrue开启后系统会自动每 10 分钟记录一次系统资源日志。核心知识sar -S 输出字段释义执行基础命令后会输出时间序列 Swap 统计数据四个核心字段必须读懂sar -Skbswpfree空闲 Swap 空间单位 KBkbswpused已使用 Swap 空间单位 KB%swpusedSwap 整体使用率核心判断指标kbswpcad缓存释放的 Swap 内存运维判定标准% swpused 50%内存资源充足无压力50%% swpused80%内存紧张逐步占用 Swap% swpused ≥85%达到 Swap 峰值内存严重不足极易触发 OOM 杀进程% swpused ≥95%Swap 彻底打满服务器读写卡顿、任务停滞实时监控 Swap 动态即时观测1. 持续实时刷新 Swap 状态每 2 秒输出一次 Swap 使用状态持续观测任务运行时 Swap 上涨趋势sar -S 22. 限定采样次数每 1 秒采集 10 组数据后自动停止适合短时压力测试sar -S 1 103. 同时查看内存 Swap 综合状态排查内存不足导致 Swap 暴涨首选命令sar -r -S 2实战 1查看今日全天 Swap 历史 峰值直接执行无参数sar -S自动读取今日全天系统采集的 Swap 日志按时间顺序展示每一个采集节点的占用数据sar -S快速查找今日 Swap 峰值执行命令后向下翻阅全部日志找到整条输出中 **% swpused 数值最大 ** 的一行对应时间即为今日内存压力最高峰同步记录 kbswpused 换算实际占用大小 换算公式KB ÷1024÷1024GB实战 2查看昨日 Swap 历史占用与峰值sar 所有历史日志统一存放路径/var/log/sa/日志命名规则sa当月日期例如 02 号日志为sa02一键查看昨日完整 Swap 日志sar -S -f /var/log/sa/sa$(date -d yesterday %d)精准指定某天 Swap 日志示例查看当月 25 号全天 Swap 记录sar -S -f /var/log/sa/sa25限定时间段查询精准排查故障时段仅查看昨日上午 9 点到下午 18 点之间的 Swap 数据缩小排查范围sar -S -f /var/log/sa/sa$(date -d yesterday %d) -s 09:00:00 -e 18:00:00实战 3导出 Swap 历史日志做复盘分析1. 导出今日 Swap 数据到文件sar -S today_swap.log2. 导出昨日 Swap 峰值日志sar -S -f /var/log/sa/sa$(date -d yesterday %d) yesterday_swap.log导出后可使用vim、grep筛选高占用时间段适配任务复盘、服务器资源扩容评估。搭配命令定位占用 Swap 最高的进程查到 Swap 历史峰值后快速锁定元凶进程针对性关停优化1. 查看当前所有进程 Swap 占用显示进程名 GBfor f in /proc/*/status; do awk /^Name:/{n$2} /^VmSwap:/{s$2} END{print n, s/1024 GB} $f done | sort -k2 -nr | head -102. 查看系统整体 Swap 总量与实时使用free -h swapon --show3. 查看系统累计交换进出总量vmstat -s | grep swap查看swapsar -S Linux 3.10.0-1127.el7.x86_64 (localhost.localdomain) 2026年07月02日 _x86_64_ (112 CPU) 00时00分01秒 kbswpfree kbswpused %swpused kbswpcad %swpcad 00时10分01秒 710072088 342698208 32.55 116340092 33.95 00时20分02秒 682084080 370686216 35.21 110588908 29.83 00时30分01秒 653116616 399653680 37.96 103295692 25.85 00时40分03秒 623609308 429160988 40.76 93927584 21.89 00时50分03秒 594580444 458189852 43.52 85252400 18.61 01时00分03秒 576430260 476340036 45.25 63450404 13.32 01时10分09秒 563997600 488772696 46.43 43956688 8.99 01时20分07秒 557488904 495281392 47.05 40019808 8.08 01时30分08秒 551670228 501100068 47.60 39293912 7.84 01时40分19秒 546143020 506627276 48.12 39143092 7.73 01时50分08秒 541795860 510974436 48.54 38371744 7.51 02时00分11秒 538154528 514615768 48.88 37436048 7.27 02时10分07秒 534079996 518690300 49.27 37421132 7.21 02时20分05秒 530803908 521966388 49.58 34224652 6.56 02时30分29秒 527618272 525152024 49.88 32790404 6.24 02时41分19秒 526324668 526445628 50.01 31319584 5.95 03时00分17秒 523845564 528924732 50.24 27000440 5.10 03时10分01秒 525593052 527177244 50.08 26697000 5.06 03时20分01秒 528197852 524572444 49.83 26730468 5.10 03时30分01秒 530442972 522327324 49.61 26792224 5.13 03时40分02秒 532445404 520324892 49.42 26901516 5.17 03时50分01秒 533747932 519022364 49.30 27110020 5.22 04时00分01秒 535787484 516982812 49.11 27244452 5.27 04时10分01秒 537517020 515253276 48.94 27411040 5.32 04时20分01秒 538952156 513818140 48.81 27605856 5.37 04时30分01秒 540219100 512551196 48.69 27829684 5.43 04时40分01秒 541353180 511417116 48.58 28105536 5.50 04时50分02秒 542233052 510537244 48.49 28381092 5.56 05时00分01秒 542700252 510070044 48.45 28713980 5.63 05时10分01秒 542989532 509780764 48.42 29071400 5.70 05时20分01秒 543161308 509608988 48.41 29440836 5.78 05时30分01秒 543276252 509494044 48.40 29819864 5.85 05时40分01秒 543355356 509414940 48.39 30208088 5.93 05时50分01秒 543415516 509354780 48.38 30593804 6.01 06时00分02秒 543459292 509311004 48.38 30981520 6.08 06时10分01秒 543566300 509203996 48.37 31379920 6.16 06时20分01秒 543799004 508971292 48.35 31638840 6.22 06时30分01秒 544361436 508408860 48.29 31685600 6.23 06时40分01秒 544918492 507851804 48.24 31741036 6.25 06时50分01秒 545470172 507300124 48.19 31795692 6.27 07时00分01秒 546032860 506737436 48.13 31850876 6.29 07时10分01秒 546589404 506180892 48.08 31904164 6.30 07时20分01秒 547147228 505623068 48.03 31957036 6.32 07时30分01秒 547707612 505062684 47.97 32011704 6.34 07时40分01秒 548253148 504517148 47.92 32069432 6.36 07时50分01秒 548776924 503993372 47.87 32125744 6.37 08时00分01秒 549240540 503529756 47.83 32173496 6.39 08时10分02秒 549666780 503103516 47.79 32213548 6.40 08时20分01秒 550086620 502683676 47.75 32255624 6.42 08时30分01秒 550460892 502309404 47.71 32294736 6.43 08时40分01秒 550797788 501972508 47.68 32326984 6.44 08时50分01秒 551088348 501681948 47.65 32357596 6.45 09时00分01秒 551350236 501420060 47.63 32385040 6.46 09时00分01秒 kbswpfree kbswpused %swpused kbswpcad %swpcad 09时10分02秒 551608028 501162268 47.60 32410440 6.47 09时20分01秒 551855836 500914460 47.58 32434320 6.48 09时30分01秒 552104156 500666140 47.56 32459212 6.48 09时40分01秒 552351708 500418588 47.53 32482204 6.49 09时50分01秒 552597212 500173084 47.51 32502072 6.50 10时00分01秒 552851676 499918620 47.49 32526736 6.51 10时10分02秒 553112284 499658012 47.46 32550148 6.51 10时20分01秒 553365724 499404572 47.44 32576500 6.52 10时30分01秒 553624028 499146268 47.41 32601848 6.53 10时40分01秒 553874140 498896156 47.39 32626256 6.54 10时50分01秒 554188764 498581532 47.36 32654828 6.55 11时00分01秒 554439132 498331164 47.34 32679412 6.56 11时10分01秒 554693852 498076444 47.31 32704980 6.57 平均时间: 553343066 499427230 47.44 37618893 7.53查看昨天swapsar -S -f /var/log/sa/sa$(date -d yesterday %d) Linux 3.10.0-1127.el7.x86_64 (localhost.localdomain) 2026年07月01日 _x86_64_ (112 CPU) 00时00分01秒 kbswpfree kbswpused %swpused kbswpcad %swpcad 00时10分01秒 778163080 274607216 26.08 123873260 45.11 00时20分01秒 778105952 274664344 26.09 123968964 45.13 00时30分02秒 778033320 274736976 26.10 124052660 45.15 00时40分01秒 777974416 274795880 26.10 124132648 45.17 00时50分01秒 777864604 274905692 26.11 124245652 45.20 01时00分01秒 777747008 275023288 26.12 124260100 45.18 01时10分01秒 777697072 275073224 26.13 124336388 45.20 01时20分01秒 777669792 275100504 26.13 124379580 45.21 01时30分01秒 777645964 275124332 26.13 124495100 45.25 01时40分01秒 777580740 275189556 26.14 124551404 45.26 01时50分01秒 777495444 275274852 26.15 124587536 45.26 02时00分01秒 777461564 275308732 26.15 124644928 45.27 02时10分01秒 777394772 275375524 26.16 124722372 45.29 02时20分01秒 777305868 275464428 26.17 124790540 45.30 02时30分01秒 777247612 275522684 26.17 124897620 45.33 02时40分02秒 777102820 275667476 26.18 124993952 45.34 02时50分01秒 777016816 275753480 26.19 125014888 45.34 03时00分01秒 776964564 275805732 26.20 125064092 45.34 03时10分01秒 776928976 275841320 26.20 125112956 45.36 03时20分01秒 776869064 275901232 26.21 125154052 45.36 03时30分01秒 776811704 275958592 26.21 125237308 45.38 03时40分01秒 776776108 275994188 26.22 125317660 45.41 03时50分01秒 776440192 276330104 26.25 124711024 45.13 04时00分01秒 776128304 276641992 26.28 124265116 44.92 04时10分01秒 776084572 276685724 26.28 124295144 44.92 04时20分01秒 776086876 276683420 26.28 124480652 44.99 04时30分01秒 776084488 276685808 26.28 124654328 45.05 04时40分01秒 776075164 276695132 26.28 124806840 45.11 04时50分01秒 776046976 276723320 26.29 124956976 45.16 05时00分02秒 776028772 276741524 26.29 125068888 45.19 05时10分01秒 775958880 276811416 26.29 125097580 45.19 05时20分01秒 775882076 276888220 26.30 125094716 45.18 05时30分01秒 775800908 276969388 26.31 125161300 45.19 05时40分01秒 775713596 277056700 26.32 125232328 45.20 05时50分01秒 775611696 277158600 26.33 125299800 45.21 06时00分01秒 775525888 277244408 26.33 125358272 45.22 06时10分01秒 775412732 277357564 26.35 125403140 45.21 06时20分01秒 775349240 277421056 26.35 125536928 45.25 06时30分01秒 775149540 277620756 26.37 125562144 45.23 06时40分01秒 775013300 277756996 26.38 125599140 45.22 06时50分01秒 774878820 277891476 26.40 125497996 45.16 07时00分01秒 774885988 277884308 26.40 125670108 45.22 07时10分01秒 774851844 277918452 26.40 125816948 45.27 07时20分01秒 774752248 278018048 26.41 125872364 45.27 07时30分01秒 774660272 278110024 26.42 125931812 45.28 07时40分01秒 774625244 278145052 26.42 126042052 45.32 07时50分01秒 774628060 278142236 26.42 126232360 45.38 08时00分01秒 774592436 278177860 26.42 126359272 45.42 08时10分02秒 774503780 278266516 26.43 126458980 45.45 08时20分01秒 774417836 278352460 26.44 126508844 45.45 08时30分01秒 774337444 278432852 26.45 126559328 45.45 08时40分01秒 774307236 278463060 26.45 126712624 45.50 08时50分01秒 774267272 278503024 26.45 126815644 45.53 08时50分01秒 kbswpfree kbswpused %swpused kbswpcad %swpcad 09时00分01秒 774146588 278623708 26.47 126902256 45.55 09时10分01秒 774091344 278678952 26.47 126997192 45.57 09时20分02秒 773950788 278819508 26.48 127026720 45.56 09时30分01秒 773920660 278849636 26.49 127177484 45.61 09时40分01秒 773867316 278902980 26.49 127271928 45.63 09时50分01秒 773792712 278977584 26.50 127349472 45.65 10时00分01秒 773706588 279063708 26.51 127424568 45.66 10时10分01秒 773597224 279173072 26.52 127500300 45.67 10时20分01秒 773421976 279348320 26.53 127446168 45.62 10时30分02秒 773295208 279475088 26.55 127534320 45.63 10时40分01秒 773252960 279517336 26.55 127701844 45.69 10时50分01秒 773196128 279574168 26.56 127828492 45.72 11时00分01秒 773117448 279652848 26.56 127916428 45.74 11时10分01秒 773056124 279714172 26.57 127987864 45.76 11时20分02秒 772899880 279870416 26.58 128138716 45.79 11时30分01秒 772739616 280030680 26.60 128287364 45.81 11时40分01秒 772664088 280106208 26.61 128307584 45.81 11时50分01秒 772579964 280190332 26.61 128467328 45.85 12时00分01秒 772440740 280329556 26.63 128599548 45.87 12时10分01秒 772307264 280463032 26.64 128754924 45.91 12时20分01秒 772216476 280553820 26.65 128879124 45.94 12时30分01秒 772105724 280664572 26.66 129001396 45.96 12时40分01秒 771701688 281068608 26.70 129091324 45.93 12时50分01秒 771579744 281190552 26.71 129254660 45.97 13时00分01秒 771516724 281253572 26.72 129426532 46.02 13时10分01秒 771444140 281326156 26.72 129583868 46.06 13时20分01秒 771395636 281374660 26.73 129739912 46.11 13时30分02秒 771259152 281511144 26.74 129819040 46.12 13时40分01秒 771066632 281703664 26.76 129965064 46.14 13时50分01秒 770891776 281878520 26.77 130100944 46.15 14时00分01秒 770712544 282057752 26.79 130219488 46.17 14时10分01秒 770589920 282180376 26.80 130307132 46.18 14时20分01秒 770481628 282288668 26.81 130439976 46.21 14时30分02秒 770340808 282429488 26.83 130574896 46.23 14时40分01秒 770332888 282437408 26.83 130756588 46.30 14时50分01秒 770246864 282523432 26.84 130894272 46.33 15时00分01秒 770097872 282672424 26.85 131008856 46.35 15时10分01秒 769954512 282815784 26.86 131165152 46.38 15时20分01秒 769841612 282928684 26.87 131221648 46.38 15时30分02秒 769773252 282997044 26.88 131371256 46.42 15时40分01秒 769630656 283139640 26.89 131512116 46.45 15时50分01秒 769493432 283276864 26.91 131684164 46.49 16时00分01秒 769380272 283390024 26.92 131827332 46.52 16时10分01秒 769208888 283561408 26.93 132003088 46.55 16时20分01秒 769128952 283641344 26.94 132179904 46.60 16时30分01秒 769023864 283746432 26.95 132222204 46.60 16时40分07秒 768939384 283830912 26.96 132234444 46.59 16时50分02秒 767519836 285250460 27.10 130809936 45.86 17时00分01秒 767523676 285246620 27.09 130999968 45.93 17时10分01秒 767524684 285245612 27.09 131183236 45.99 17时20分02秒 767253048 285517248 27.12 130679408 45.77 17时30分01秒 767145780 285624516 27.13 130665536 45.75 17时40分01秒 767148072 285622224 27.13 130850244 45.81 17时40分01秒 kbswpfree kbswpused %swpused kbswpcad %swpcad 17时50分01秒 767132960 285637336 27.13 130985964 45.86 18时00分01秒 767108628 285661668 27.13 131089600 45.89 18时10分02秒 767115784 285654512 27.13 131270500 45.95 18时20分01秒 767104736 285665560 27.13 131412508 46.00 18时30分01秒 767104736 285665560 27.13 131582584 46.06 18时40分01秒 767106784 285663512 27.13 131771564 46.13 18时50分01秒 767109532 285660764 27.13 131955792 46.19 19时00分01秒 767109476 285660820 27.13 132142720 46.26 19时10分02秒 767115640 285654656 27.13 132333756 46.33 19时20分01秒 767097976 285672320 27.14 132493332 46.38 19时30分01秒 767091828 285678468 27.14 132677020 46.44 19时40分02秒 767083120 285687176 27.14 132840404 46.50 19时50分01秒 767082600 285687696 27.14 133018256 46.56 20时00分01秒 767061352 285708944 27.14 133165592 46.61 20时10分01秒 767035664 285734632 27.14 133304776 46.65 20时20分01秒 767007800 285762496 27.14 133432584 46.69 20时30分01秒 766994156 285776140 27.15 133580040 46.74 20时40分01秒 766963944 285806352 27.15 133700836 46.78 20时50分02秒 765278896 287491400 27.31 132668528 46.15 21时00分01秒 765144548 287625748 27.32 132518656 46.07 21时10分01秒 765146340 287623956 27.32 132729064 46.15 21时20分01秒 765147876 287622420 27.32 132918200 46.21 21时30分01秒 765148900 287621396 27.32 133104868 46.28 21时40分02秒 765150436 287619860 27.32 133291096 46.34 21时50分01秒 765112532 287657764 27.32 133403228 46.38 22时00分01秒 765108692 287661604 27.32 133577856 46.44 22时10分01秒 765109460 287660836 27.32 133768636 46.50 22时20分01秒 765113300 287656996 27.32 133960388 46.57 22时30分01秒 765113812 287656484 27.32 134147324 46.63 22时40分02秒 765115348 287654948 27.32 134334908 46.70 22时50分01秒 765118932 287651364 27.32 134521500 46.77 23时00分01秒 765121492 287648804 27.32 134709560 46.83 23时10分01秒 765122772 287647524 27.32 134896240 46.90 23时20分01秒 765123984 287646312 27.32 135083668 46.96 23时30分01秒 765110400 287659896 27.32 135242632 47.01 23时40分02秒 764776572 287993724 27.36 135237356 46.96 23时50分07秒 763028848 289741448 27.52 134209912 46.32 平均时间: 771699256 281071040 26.70 128855041 45.84echo -e 今日Swap峰值 ;sar -S|awk NR3$4!~/^0/{print $4,$5}|sort -k2 -nr|head -5 今日Swap峰值 32.55 116340092 35.21 110588908 37.96 103295692 40.76 93927584 43.52 85252400 echo -e \n 昨日Swap峰值 ;sar -S -f /var/log/sa/sa$(date -d yesterday %d) 2/dev/null|awk NR3$4!~/^0/{print $4,$5}|sort -k2 -nr|head -5 昨日Swap峰值 27.32 135242632 27.36 135237356 27.32 135083668 27.32 134896240 27.32 134709560 echo -e \n 当前Swap占用Top10进程 ;for f in /proc/*/status;do awk /^Name:/{n$2} /^VmSwap:/{s$2} END{print n,s/1024 GB} $f;done|sort -k2 -nr|head -10 当前Swap占用Top10进程 hifiasm 443795 GB gnome-software 661.133 GB frp-plus-client 512.781 GB gnome-disks 343.926 GB gnome-shell 161.555 GB X 136.664 GB soffice.bin 123.727 GB firefox 123.293 GB Web 94.2148 GB WebExtensions 33.0039 GB