问题
I'm using HtmlUnit for a parsing job and I've discovered that the memory gets wasted with the WebClient holding the history for each WebWindow. I don't use the history at all and I'd like to disable its management or at least limit its size with 1 or 2. Is that possible?
回答1:
The following code will set ignoreNewPages_
to true:
try {
final WebClient webClient = getWebClient();
final List<WebWindow> webWindows = webClient.getWebWindows();
History window = webWindows.get(0).getHistory();
Field f = window.getClass().getDeclaredField("ignoreNewPages_"); //NoSuchFieldException
f.setAccessible(true);
((ThreadLocal<Boolean>) f.get(window)).set(true);
} catch (Exception e) {
e.printStackTrace();
throw new AssertionError("Can't disable history");
}
The accessors:
private static WebTester getTester() {
return JWebUnit.getTester();
}
private HtmlUnitTestingEngineImpl getHtmlUnitEngine() {
return (HtmlUnitTestingEngineImpl) getTester().getTestingEngine();
}
private WebClient getWebClient() {
return getHtmlUnitEngine().getWebClient();
}
回答2:
There is no option in HtmlUnit to disable history that I am aware of. The History class has a getHistory() method but no setHistory() or disableHistory(). What I have done, and it is certainly not ideal, is release the web page and reinstantiate it. As long as you don't release your CookieManager you should be ok on the cookie front. Basically once I get all the way through and logged in I nullify my window after storing the current page to a temp string, then I reinstantiate it to where I left off. I do this at given points to clear out history.
String tempPage = currentHtmlPage.getUrl().toString(); //HtmlPage class
window = null;
window = new WebWindow();
currentHtmlPage = new WebWindow.getWebClient().getPage(tempPage); //HtmlPage class
This allows the window to pick up where it left off. Its ugly but if you are desperate, it may work.
来源:https://stackoverflow.com/questions/2849425/how-to-limit-htmlunits-history-size