简体   繁体   中英

HTMLUnit Java OutofMemory Heap Space Exception

I have been trying to solve Java Heap Space OutOfMemory Exception thrown from my program that uses HTMLUnit API to visit pages (the pages have extensive javascripts so i choosed to use HTMLUnit for this). The program runs for about 1 hour and the memory exception is encountered. I tried different methods as suggested from other posts, but none solved this issue. While the program runs on Linux: the linux TOP command shows the memory usage by this program is continuously increasing and reaches to 90% and then the OutOfMemory exception is thrown. These are methods i tried to solve this issue:
1.increased heap space
2.run on jdk1.8
3.run on open jre1.7
4.used latest version of html unit ie htmlunit2.7
5.used webclient.close() method in finally block

List<WebWindow> windows = client.getWebWindows();

This list count increases in each new page visit, even if i close all the windows:

webclient.close()

I wonder what is the cause for increased memory usage by HTMLUnit and why program does not release memory even when webclient.close() method is called. Any suggestions related to this will be appreciated. Thank you.

EDIT:

static WebClient client; //this is class variable
public void parsePage(){
HtmlPage pages;
pages = client.getPage(myURL);
Document doc = Jsoup.parse(pages.asXml()); //Using JSOUP library here
client.close();
///do work on the Document
}

You have probably memory leak, ie you're adding some objects (maybe instances of WebWindow or HTMLPage ) to some collection and they're been kept there for the whole time. If it's not the case then it's an issue in the library itself, but first examine your own code. BTW WebClient is autocloseable, you can use try (<obtain resource here>) syntax with it.

I recommend using a try/catch/finally block and put client.close(); in the finally block

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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