简体   繁体   English

htmlunit内存泄漏

[英]htmlunit memory leaks

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. PS显然,以上方法在HtmlUtit的较新版本中已弃用,而应使用webClient.close()

Since version 2.16 closeAllWindows() is deprecated, since 2.21 it is removed. 由于不推荐使用版本2.16 closeAllWindows(),因此将从2.21起将其删除。 So now you can call 所以现在你可以打电话

webClient.close();

to close the client and stop javascript excecution. 关闭客户端并停止执行javascript。

The JVM is not keen on freeing data as soon as possible. JVM不希望尽快释放数据。 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. 如果您给虚拟机分配240MB的空间,它将接近该值,然后您才能看到垃圾收集器为其赚钱。 Continue this test until you run into an OutofMemoryError. 继续此测试,直到遇到OutofMemoryError。 If so, there might be a leak in the HtmlUnit library. 如果是这样,则HtmlUnit库中可能存在泄漏。

Each time the webClient.getPage is called, htmlunit creates a new window for that page. 每次调用webClient.getPagehtmlunit都会为该页面创建一个新窗口。 It is similar to tabs in web browsers. 它类似于Web浏览器中的选项卡。

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. 它可能会帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM