0

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 i.e. 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
}
Aryal Sumu
  • 53
  • 7
  • The latest version of HTMLUnit is 2.18, but I don't changing this will help. – MirMasej Jul 30 '15 at 07:06
  • You need to post the code where you read in/parse the pages. Its for sure your program does not release created objects, or reads to many bytes at a time. – Stefan Jul 30 '15 at 07:07

2 Answers2

2

You have probably memory leak, i.e. 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.

MirMasej
  • 1,592
  • 12
  • 17
0

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

Arya
  • 8,473
  • 27
  • 105
  • 175