VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • 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掉整个进程,所以不适合多个线程操作,否则会出 现中断的情况。


原文:https://www.cnblogs.com/lyl6796910/p/14328630.html


相关教程