Reputation: 1011
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
Upvotes: 2
Views: 2402
Reputation: 8169
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.
Upvotes: 2
Reputation: 134
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.
Upvotes: 1
Reputation: 744
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.
Upvotes: 0
Reputation: 11
Each time the webClient.getPage
is called, htmlunit
creates a new window for that page. It is similar to tabs in web browsers.
Upvotes: 1
Reputation: 4122
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.
Upvotes: 1