selenium 未即时关闭引起的内存泄漏 差点死机

selenium webdriver firefox 测试自动登录获取 token,测试可以达到目的。然后日常摸鱼后发现浏览器快卡死了,切tty看top,任务没跑多少,内存倒是快榨干了,这不合理,也没有跑什么大内存程序,先把bt、sync给kill了,内存情况也没有太好转,于是看下内存占用,然后找内存占用高的进程,把这些占用高的给kill掉,内存占用就下来了。然后发现都是 firefox 相关的,然后此时我早已关闭了浏览器,然后考虑到之前用 selenium 跑测试来着,因此大概率是没正常释放资源,一看代码确实 quit 不一定执行,比如说中间发生异常,就不会执行quit,于是try except finally 补丁。奇怪的是,印象里程序异常退出、资源就释放掉了,比如父子进程

  • top 看在跑的进程数量、进程的 cpu 占用、内存占用,按 m 可以看内存占用的条形图
  • free -g 可以看内存总量、剩余量,-g 表示单位是 GB
  • ps -aux | sort -k4nr | head -10 查看内存占用前十的进程。ps 用于查看当前系统的进程状态, sort对输入的内容进行排序,-k4 指定以第四列的数值作为排序的键,按 -k4nr 表示按照第 4 列的数值进行降序排列,查看占用内存最多的进程
$ top -b | head -15
top - 11:49:40 up 9 days, 30 min,  2 users,  load average: 8.29, 48.25, 32.03
任务: 424 total,   2 running, 421 sleeping,   0 stopped,   1 zombie
%Cpu(s):  3.8 us,  6.4 sy,  0.0 ni, 89.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st 
MiB Mem :  15270.4 total,   1092.5 free,  12969.7 used,   3985.7 buff/cache     
MiB Swap:      0.0 total,      0.0 free,      0.0 used.   2300.8 avail Mem 

 进程号 USER      PR  NI    VIRT    RES    SHR    %CPU  %MEM     TIME+ COMMAND
 356300 zhang shan     20   0   15484   6000   3696 R  23.1   0.0   0:00.06 top
 280353 root      20   0  181792  21336   4080 S  15.4   0.1   0:21.84 dirmngr
  33103 zhangshan     20   0 2547336  36488   2244 S   7.7   0.2 141:24.28 WebExte+
  35611 zhangshan     20   0 2547336  32864   2584 S   7.7   0.2 143:03.09 WebExte+
 308391 zhangshan     20   0 3029928 210272  17160 S   7.7   1.3   9:40.32 firefox
      1 root      20   0   22444   8900   5304 S   0.0   0.1   0:15.02 systemd
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.13 kthreadd
      3 root      20   0       0      0      0 S   0.0   0.0   0:00.00 pool_wo+

$ free -g
               total        used        free      shared  buff/cache   available
内存:            14          12           1           2           3           2
交换:             0           0           0
$ ps -aux | sort -k4nr | head -10
zhangshan      35505  0.9  1.7 3044476 269632 ?      Sl   4月20 117:21 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 55663 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofileYUuCMy
zhangshan      31216  0.9  1.6 3045484 257948 ?      Sl   4月20 119:45 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 40957 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofileQkLA3z
zhangshan      31697  0.9  1.6 3046036 256520 ?      Sl   4月20 115:40 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 48411 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofile6gxPMw
zhangshan      32516  0.8  1.6 3036764 254252 ?      Sl   4月20 112:57 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 55389 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofileKmaAFn
zhangshan      33538  0.9  1.6 3071120 253928 ?      Sl   4月20 116:45 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 44327 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofileqxHGiz
zhangshan      32997  0.9  1.5 2969196 236076 ?      Sl   4月20 113:59 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 33505 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofileGBP2p7
zhangshan     306446  0.8  1.3 2969684 209232 ?      Sl   4月28   9:29 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 60987 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofileWojSCe
zhangshan     308391  0.8  1.3 3029928 210272 ?      Sl   4月28   9:40 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 45885 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofilexxB0wC
zhangshan     310041  0.8  1.3 2976120 217360 ?      Sl   4月28   9:35 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 57229 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofile1bynOt
zhangshan     311240  0.8  1.3 2974576 209136 ?      Sl   4月28   9:50 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 40533 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofile0cG0ul
$ kill 35505 31216 31697 32516 33538 32997 306446

$ ps -aux | sort -k4nr | head -10
zhangshan     316113  0.8  1.2 2966888 195944 ?      Sl   4月28   9:18 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 51917 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofileY1JSG1
zhangshan     335124  1.0  1.2 3025232 188836 ?      Sl   09:03   1:43 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 38679 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofile9rOJME
zhangshan     335620  1.1  1.0 2981756 157876 ?      Sl   09:04   1:59 /usr/lib/firefox/firefox --marionette --headless --remote-debugging-port 48943 --remote-allow-hosts localhost -no-remote -profile /tmp/rust_mozprofiletfvn8Y
zhangshan       1244  0.0  0.6 586220 94128 ?        Sl   4月20   5:34 /usr/bin/fcitx5
root         523  3.6  0.6 916988 101208 tty7    Ssl+ 4月20 479:46 /usr/lib/Xorg :0 -seat seat0 -auth /run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
zhangshan     336749  0.1  0.5 2728544 84420 ?       Sl   09:04   0:15 /usr/lib/firefox/firefox -contentproc -childID 3 -isForBrowser -prefsLen 28477 -prefMapSize 241150 -jsInitLen 234952 -parentBuildID 20240416221418 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appDir /usr/lib/firefox/browser {3ca94547-d8eb-4d3f-a3e5-cc863397af7a} 335620 true tab
zhangshan       1219  0.0  0.4 586368 72796 ?        Sl   4月20   0:43 xfdesktop
zhangshan     335345  0.0  0.4 2713192 77140 ?       Sl   09:03   0:03 /usr/lib/firefox/firefox -contentproc -childID 3 -isForBrowser -prefsLen 28629 -prefMapSize 241150 -jsInitLen 234952 -parentBuildID 20240416221418 -greomni /usr/lib/firefox/omni.ja -appomni /usr/lib/firefox/browser/omni.ja -appDir /usr/lib/firefox/browser {bf8e4007-45c1-46b9-9f14-fd8f9856f0d5} 335124 true tab
root         488  1.2  0.4 2507020 73324 ?       Ssl  4月20 160:03 /usr/bin/opensnitchd -rules-path /etc/opensnitchd/rules
zhangshan       1148  1.1  0.3 1021576 54492 ?       Sl   4月20 148:07 xfwm4
$ kill 316113 335124 335620
$ ps -aux | sort -k4nr | head -10
zhangshan       1244  0.0  0.6 586220 94128 ?        Sl   4月20   5:34 /usr/bin/fcitx5
root         523  3.6  0.6 917300 101464 tty7    Ssl+ 4月20 479:46 /usr/lib/Xorg :0 -seat seat0 -auth /run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
zhangshan       1219  0.0  0.4 586368 72796 ?        Sl   4月20   0:43 xfdesktop
root         488  1.2  0.4 2507020 73708 ?       Ssl  4月20 160:04 /usr/bin/opensnitchd -rules-path /etc/opensnitchd/rules
zhangshan       1148  1.1  0.3 1021576 54492 ?       Sl   4月20 148:07 xfwm4
zhangshan       1207  0.0  0.3 1022016 54500 ?       Sl   4月20   3:59 xfce4-panel
zhangshan     355237  1.5  0.3 629832 59440 ?        Sl   11:47   0:03 /usr/bin/xfce4-terminal
root         223  0.0  0.3 165244 57048 ?        Ss   4月20   1:47 /usr/lib/systemd/systemd-journald
root         640  0.0  0.3 2708056 54740 ?       Ssl  4月20   4:51 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
$ free -g
               total        used        free      shared  buff/cache   available
内存:            14           3          10           1           3          11
交换:             0           0           0
posted @ 2024-04-30 10:26  沙滩炒花蛤  阅读(29)  评论(0编辑  收藏  举报