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