程序 WebClient webClient = new WebClient(BrowserVersion.CHROME); webClient.getOptions().setJavaScriptEnabled(false); webClient.getOptions().setCssEnabled(false); HtmlPage page; try { page = webClient.getPage("http://www.taolife.com.cn/"); System.out.print(page.asText()); } catch (FailingHttpStatusCodeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
bug:
1Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.lang.Class.getDeclaredFields0(Native Method) at java.lang.Class.privateGetDeclaredFields(Class.java:2583) at java.lang.Class.getField0(Class.java:2975) at java.lang.Class.getField(Class.java:1701) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.configureConstants(JavaScriptEngine.java:612) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.init(JavaScriptEngine.java:271) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.access$000(JavaScriptEngine.java:105) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$1.run(JavaScriptEngine.java:173) at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:628) at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.initialize(JavaScriptEngine.java:184) at com.gargoylesoftware.htmlunit.WebClient.initialize(WebClient.java:1008) at com.gargoylesoftware.htmlunit.WebWindowImpl.setEnclosedPage(WebWindowImpl.java:135) at com.gargoylesoftware.htmlunit.html.FrameWindow.setEnclosedPage(FrameWindow.java:105) at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:221) at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:199) at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:272) at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:160) at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:476) at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:350) at com.gargoylesoftware.htmlunit.html.BaseFrameElement.loadInnerPageIfPossible(BaseFrameElement.java:184) at com.gargoylesoftware.htmlunit.html.BaseFrameElement.loadInnerPage(BaseFrameElement.java:122) at com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames(HtmlPage.java:1945) at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:227) at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:483) at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:350) at com.gargoylesoftware.htmlunit.html.BaseFrameElement.loadInnerPageIfPossible(BaseFrameElement.java:184) at com.gargoylesoftware.htmlunit.html.BaseFrameElement.loadInnerPage(BaseFrameElement.java:122) at com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames(HtmlPage.java:1945) at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:227) at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:483) at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:350)
修改运行参数:-Xms1024m -Xmx1024m,依旧报错。
修改
webClient.getOptions().setJavaScriptEnabled(false);
程序正常运行;
分析:
估计htmunit采用的javascript解析器存在问题,陷入死循环耗尽内存;
因项目时间问题,暂时采集true方案,回头用内存监控工具 jconsole或者eclipse memoery analyzer找出是哪个模块出问题。
时间: 2024-11-09 10:05:39