gpt4 book ai didi

java - Htmlunit 2.27 中的内存泄漏

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:39 25 4
gpt4 key购买 nike

我正在使用htmlunit:

<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.27</version>
</dependency>

我有一个简单的循环,每隔一秒从响应中打印一些数据:

public static void main(String[] args) throws InterruptedException, IOException {

WebClient webClient;

webClient = new WebClient();

webClient = new WebClient();
webClient.getOptions().setThrowExceptionOnScriptError(false);
final History window = webClient.getWebWindows().get(0).getHistory();
final Field f;
try {
f = window.getClass().getDeclaredField("ignoreNewPages_"); //NoSuchFieldException
f.setAccessible(true);
((ThreadLocal<Boolean>) f.get(window)).set(Boolean.TRUE);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
System.out.println(ex.getMessage());
}

for (int i = 0; i < Integer.MAX_VALUE; i++) {
System.out.println(i);
HtmlPage page = webClient.getPage("https://stackoverflow.com");
System.out.println(page.getHead().toString());

Thread.sleep(1000);

webClient.getCookieManager().clearCookies();
webClient.getCache().clear();

final List<WebWindow> windows = webClient.getWebWindows();

for (final WebWindow wd : windows) {

wd.getJobManager().removeAllJobs();

}

webClient.close();

}
}

当我运行程序时,内存在增长,为什么?这是错误吗?或者什么?

最佳答案

for (int i = 0; i < Integer.MAX_VALUE; i++) {
System.out.println(i);
HtmlPage page = webClient.getPage("https://stackoverflow.com");
System.out.println(page.getHead().toString());

Thread.sleep(1000);

webClient.getCookieManager().clearCookies();
webClient.getCache().clear();

final List<WebWindow> windows = webClient.getWebWindows();

for (final WebWindow wd : windows) {
wd.getJobManager().removeAllJobs();
}
}

根据上面的循环,看起来您正在加载整个 SOF 主页 Integer.MAX_VALUE 次。当然,这会产生内存影响。

关于java - Htmlunit 2.27 中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44504967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com