. NET Webbroswer Memory Release

Source: Internet
Author: User

Recently wrote the novel Crawl tool encountered a performance bottleneck, using multiple webbroswer controls to preload multiple pages, memory will continue to increase, up to about 400M, can not tolerate.

Examples of failures

First try to set the Webbroswer object to Null,wb=null, waiting for the GC to be automatically recycled, no effect.

            WebBrowser wb = new WebBrowser ();            Wb. DocumentCompleted + = Delegate (object sender, WebBrowserDocumentCompletedEventArgs args)            {                if (WB! = NULL && Amp Isbroswerok (WB))                {                    Html = wb. DocumentText;                    Delete browser                    wb = null;                }            };            Wb. Navigate (C.url);

Then try to join a forced garbage collection

System.GC.Collect ();

Still invalid

Examples of success

Sure enough himself or unreliable, honestly open Google Search, finally on MSDN find a solution. Original post point I

First, import the system kernel at the beginning of the class

        [DllImport ("KERNEL32. DLL ", EntryPoint =" SetProcessWorkingSetSize ", SetLastError = true, CallingConvention = Callingconvention.stdcall)]        internal static extern bool SetProcessWorkingSetSize (IntPtr pprocess, int dwminimumworkingsetsize, int dwmaximumworkingsetsize);        [DllImport ("KERNEL32. DLL ", EntryPoint =" GetCurrentProcess ", SetLastError = true, CallingConvention = Callingconvention.stdcall)]        Internal static extern IntPtr getcurrentprocess ();

Called when the browser memory is freed

                    // Delete Browser                    NULL ;                     // Clean up memory                    IntPtr Phandle = getcurrentprocess ();                     -1,-1);

It is worth noting that this method is not suitable for frequent use

It brings a very serious disadvantage, the operating system in order to achieve the limit of memory size, will continue to the memory and virtual memory between the conversion, but greatly increased the burden of the operating system, so it is not commonly used.

. NET Webbroswer Memory Release

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.