java – 如何限制HtmlUnit的历史记录大小?
作者:互联网
我正在使用HtmlUnit进行解析工作,并且我发现内存被WebClient浪费在每个WebWindow上.我根本不使用历史记录,我想禁用它的管理或者至少用1或2来限制它的大小.这可能吗?
解决方法:
以下代码将ignoreNewPages_设置为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");
}
访问者:
private static WebTester getTester() {
return JWebUnit.getTester();
}
private HtmlUnitTestingEngineImpl getHtmlUnitEngine() {
return (HtmlUnitTestingEngineImpl) getTester().getTestingEngine();
}
private WebClient getWebClient() {
return getHtmlUnitEngine().getWebClient();
}
标签:java,history,htmlunit 来源: https://codeday.me/bug/20190710/1422571.html