本文介绍了如何限制HtmlUnit的历史记录大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HtmlUnit进行解析,并且发现内存浪费了WebClient来保存每个WebWindow的历史记录.我完全不使用历史记录,我想禁用它的管理或至少将其大小限制为1或2.这可能吗?

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?

推荐答案

以下代码会将ignoreNewPages_设置为true:

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");
}

访问者:

private static WebTester getTester() {
    return JWebUnit.getTester();
}

private HtmlUnitTestingEngineImpl getHtmlUnitEngine() {
    return (HtmlUnitTestingEngineImpl) getTester().getTestingEngine();
}

private WebClient getWebClient() {
    return getHtmlUnitEngine().getWebClient();
}

这篇关于如何限制HtmlUnit的历史记录大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 21:17