zoukankan      html  css  js  c++  java
  • c#爬虫-从内存中释放Selenium chromedriver.exe终极杀

    背景

    之前在做爬虫的时候,遇到内存释放不掉的问题,从内存中释放Selenium chromedriver.exe。后面我使用以下方法:

    public override void DoJob(IJobExecutionConxt context, ILifetimeScope scope, string[] args)
            {
                Console.WriteLine(nameof(LoginReptiles1688Job) + " 开始-------------------");
                ChromeOptions options = null;
                IWebDriver driver = null;
                try
                {
                。。。。。。。。。。。。。。。。。。。。。。。
                      }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    driver?.Close(); // Close the chrome window
                    driver?.Quit(); // Close the console app that was used to kick off the chrome window
                    driver?.Dispose(); // Close the chromedriver.exe
    
                    driver = null;
                    options = null;
                    detailtry = 0;
                    shoptry = 0;
                    Console.WriteLine(nameof(LoginReptiles1688Job) + " 结束-------------------");
                }
            }

    这不,还真降下来了;但是,没等几天服务器又报警了,还是老问题,后面捣鼓了半天,还是没有解决问题。

    问题窥探

    其实问题的根本原因是Selenium chromedriver.exe没法正常关闭,虽然尝试了很多方法,但是始终没法测底解决;后面灵机一闪,何不来个彻底点的,使用kill把整个chromedriver.exe线程干掉,这不就彻底了;说干就干。

    主要代码

     #region 异常  退出chromedriver
    
            [DllImport("user32.dll", EntryPoint = "FindWindow")]
            private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            [DllImport("user32.dll", EntryPoint = "SendMessage")]
            public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    
            public const int SW_HIDE = 0;
            public const int SW_SHOW = 5;
    
            [DllImport("user32.dll", EntryPoint = "ShowWindow")]
            public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
    
            /// <summary>
            /// 获取窗口句柄
            /// </summary>
            /// <returns></returns>
            public IntPtr GetWindowHandle()
            {
                string name = (Environment.CurrentDirectory + "\chromedriver.exe");
                IntPtr hwd = FindWindow(null, name);
                return hwd;
            }
    
            /// <summary>
            /// 关闭chromedriver窗口
            /// </summary>
            public void CloseWindow()
            {
                try
                {
                    IntPtr hwd = GetWindowHandle();
                    SendMessage(hwd, 0x10, 0, 0);
                }
                catch { }
            }
    
            /// <summary>
            /// 退出chromedriver
            /// </summary>
            /// <param name="driver"></param>
            public void CloseChromeDriver(IWebDriver driver)
            {
                try
                {
                    driver.Quit();
                    driver.Dispose();
                }
                catch { }
                CloseWindow();
            }
    
            #endregion 异常  退出chromedriver

    总结

    1、果然够彻底的,chromedriver.exe每次都正常关闭了,内存占用也正常。

    2、事有两面性,有时候反方向来解决问题未尝不是一个好的办法。

    3、使用这种方式,等于kill掉整个进程,所以不适合多个线程操作,否则会出 现中断的情况。

  • 相关阅读:
    [转]Delphi DLL的创建、静态 以及动态调用
    Delphi txt文件的操作(读取、写入)
    为什么要使用动态链接库(DLL)
    TStringGrid 实现下拉框
    Ryzen 4000'Vermeer' CPU和Radeon RX'Big Navi'图形卡
    AMD Ryzen 5000‘Cezanne’APU
    AMD–7nm “Rome”芯片SOC体系结构,支持64核
    ARM Cortex-M嵌入式C基础编程(下)
    ARM Cortex-M嵌入式C基础编程(上)
    基于ARM Cortex-M的SoC存储体系结构和实战
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/14328630.html
Copyright © 2011-2022 走看看