다음 코드는 Windows 폼에서 실행되어 WebBrowser 컨트롤을 만들고 여러 URL로 이동합니다. URL을 탐색 할 때마다 응용 프로그램에서 사용하는 메모리가 증가하고 절대로 공개되지 않는 것 같습니다. 필자의 경우 소비되는 메모리는 10Mb에서 4Mb의 네비게이션까지 45Mb까지 증가한다. 나는 신속하게 모든 물리적 메모리를 사용하게하는 수백 개의 문서를 탐색해야한다.Windows.Forms.WebBrowser 컨트롤을 사용하여 웹 문서를 탐색하는 동안 메모리를 해제하는 방법
WebBrowser 컨트롤에서 사용하는 리소스를 어떻게 해제합니까?
private void ProcessDocs()
{
Random rnd = new Random();
DateTime dtWaitUntil = DateTime.Now;
string[] arrInputUrl = new string[] { "http://www.google.co.uk/", "http://www.google.com/", "http://www.yahoo.com/", "http://www.amazon.co.uk/" };
for (int i = 0; i < arrInputUrl.Length; i++)
{
using (WebBrowser objWebBrowser = new WebBrowser())
{
string txtInputUrl = arrInputUrl[i];
tboxTestingOutput.Text += "\r\n" + txtInputUrl;
objWebBrowser.ScriptErrorsSuppressed = true;
objWebBrowser.Navigate(txtInputUrl);
//Should do this a better way, but, wait 10 secs for document to complete.
//Would use objWebBrowser.DocumentCompleted event
//but it seems to fire several times per document load!!!
dtWaitUntil = DateTime.Now.AddSeconds(10);
do
{
Application.DoEvents();
} while (DateTime.Now < dtWaitUntil);
//Do whatever I need to with the document contents
//...
//...
}
}
tboxTestingOutput.Text += "\r\nFinished";
}
웹 페이지 콘텐츠를 읽을 필요가 있다면'WebRequest'를 대신 사용해 보았습니까? (http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx 참조) – MiMo
글쎄, 우리는 45MB를 소비한다는 것을 알고있다. 브라우저 용 땅콩이다. 우리가 알지 못하는 것은 충돌 할 때 얼마나 많은 VM을 소비하는지입니다. 당신은 실제로 충분히 오랫동안 기다렸나요 아니면 그냥 계속 올라가고 있다고 추측하고 있습니까? 그것은 메모리 관리자가 작동하는 방식이 아닙니다. –
@MiMo WebBrowser를 사용하여 HTTP 헤더 리디렉션, Javascript 리디렉션, SWF 리디렉션 등의 결과 일 수있는 최종 리디렉션 된 페이지로 이동합니다. 대부분은 WebRequest에서 처리하지 않습니다. – Steven