I am using htmlunit
:
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.27</version>
</dependency>
I have simple loop which every one second print some data from response:
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();
}
}
When I run the program, memory is growing, why? It is bug? Or what?
答案 0 :(得分:0)
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次。当然,这会产生记忆效应。
答案 1 :(得分:0)
有人检查过这段代码和jProfiler,似乎根本没有真正的内存泄漏。
public static void main(String[] args) throws Exception {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
try (WebClient webClient = new WebClient()) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setHistorySizeLimit(0);
System.out.println(i);
HtmlPage page = webClient.getPage("https://stackoverflow.com");
System.out.println(page.getHead().toString());
}
Thread.sleep(1000);
}
}