Memory is going to increase by each loop cycle. Any idea why is it?
public static void main(String p[]) throws IOException {
WebClient webClient = new WebClient();
for (int a = 0; a < 100000; a++) {
HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
String pageAsXml = page.asXml();
System.out.println(pageAsXml);
}
}
Thanks in Advance
You need to call
webClient.closeAllWindows()
in the loop after you're done with page.
PS Apparently above method is deprecated in the newer versions of HtmlUtit and webClient.close()
should be used instead.
Since version 2.16 closeAllWindows() is deprecated, since 2.21 it is removed. So now you can call
webClient.close();
to close the client and stop javascript excecution.
The JVM is not keen on freeing data as soon as possible. If you give the VM 240MB to allocate, it will get near that value before you can see the garbage collector doing something for its money. Continue this test until you run into an OutofMemoryError. If so, there might be a leak in the HtmlUnit library.
Each time the webClient.getPage
is called, htmlunit
creates a new window for that page. It is similar to tabs in web browsers.
Try
protected void closeWebClient(WebClient wc) {
List<WebWindow> windows = wc.getWebWindows();
for (WebWindow wd : windows) {
// wd.getThreadManager().interruptAll();
wd.getJobManager().removeAllJobs();
}
wc.closeAllWindows();
}
It might help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.