Java中的java.lang.OutOfMemoryError

我想通过java progrram登录ORKUT而不涉及IE或Firefox,

我以下列方式使用HTMLUnit :

import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput; import com.gargoylesoftware.htmlunit.html.HtmlTextInput; import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; public class HtmlUnit { public void submittingForm() throws Exception { final WebClient webClient = new WebClient(); // Get the first page final HtmlPage page1 = webClient.getPage("https://www.google.com/accounts/ServiceLogin?service=orkut&hl=en-US&rm=false&continue=http%3A%2F%2Fwww.orkut.com%2FRedirLogin%3Fmsg%3D0%26page%3Dhttp%253A%252F%252Fwww.orkut.co.in%252FHome.aspx&cd=IN&passive=true&skipvpage=true&sendvemail=false"); System.out.println("Page1: " + page1.toString()); // Get the form that we are dealing with and within that form, // find the submit button and the field that we want to change. final HtmlSubmitInput button = page1.getElementByName("signIn"); final HtmlTextInput userName = page1.getElementByName("Email"); final HtmlPasswordInput password = page1.getElementByName("Passwd"); // Change the value of the text field userName.setValueAttribute("username"); password.setValueAttribute("password"); // Now submit the form by clicking the button and get back the second page. final HtmlPage page2 = button.click(); System.out.println("Page2: " + page2.toString()); } public static void main(String[] args) { try { new HtmlUnit().submittingForm(); }catch(Exception ex) { ex.printStackTrace(); } } } 

但运行上述程序后,我收到以下错误:

 Page1: HtmlPage(https://www.google.com/accounts/ServiceLogin?service=orkut&hl=en-US&rm=false&continue=http%3A%2F%2Fwww.orkut.com%2FRedirLogin%3Fmsg%3D0%26page%3Dhttp%253A%252F%252Fwww.orkut.co.in%252FHome.aspx&cd=IN&passive=true&skipvpage=true&sendvemail=false)@6011238 Sep 15, 2009 4:54:37 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://static1.orkut.com/js/gen/in_frame046.js', but got 'application/x-javascript'. Sep 15, 2009 4:54:38 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://static1.orkut.com/js/gen/gtalk_client031.js', but got 'application/x-javascript'. Sep 15, 2009 4:54:38 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://static1.orkut.com/ifpc/ifpc.js', but got 'application/x-javascript'. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at net.sourceforge.htmlunit.corejs.javascript.ObjToIntMap.rehashTable(ObjToIntMap.java:330) at net.sourceforge.htmlunit.corejs.javascript.ObjToIntMap.ensureIndex(ObjToIntMap.java:415) at net.sourceforge.htmlunit.corejs.javascript.ObjToIntMap.intern(ObjToIntMap.java:197) at net.sourceforge.htmlunit.corejs.javascript.TokenStream.getToken (TokenStream.java:435) at net.sourceforge.htmlunit.corejs.javascript.Parser.peekToken(Parser.java:313) at net.sourceforge.htmlunit.corejs.javascript.Parser.variables(Parser.java:1791) at net.sourceforge.htmlunit.corejs.javascript.Parser.statementHelper (Parser.java:959) at net.sourceforge.htmlunit.corejs.javascript.Parser.statement(Parser.java:877) at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:549) at net.sourceforge.htmlunit.corejs.javascript.Parser.parse(Parser.java:492) at net.sourceforge.htmlunit.corejs.javascript.Context.compileImpl(Context.java:2404) at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1375) at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory$TimeoutContext.compileString(HtmlUnitContextFactory.java:173) at net.sourceforge.htmlunit.corejs.javascript.Context.compileString(Context.java:1364) at com.gargoylesoftware.htmlunit.javascript.host.Window.custom_eval(Window.java:1143) at com.gargoylesoftware.htmlunit.javascript.host.Window.jsxFunction_execScript(Window.java:1122) at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:161) at net.sourceforge.htmlunit.corejs.javascript.FunctionObject.call(FunctionObject.java:476) at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1688) at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:845) at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:164) at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:427) at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:263) at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3058) at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:175) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$5.doRun(JavaScriptEngine.java:415) at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:520) at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:529) at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:536) 

所以,我不明白问题出在哪里。 执行以下行时发生错误:

 final HtmlPage page2 = button.click(); 

错误的名称给出了一个提示:Java虚拟机需要更多内存。 您可以通过将标志’-Xmx128M’添加到jvm来增加堆空间(或者增加整数值以防万一,java调用已经有一个值较小的Xmx参数。

通过上面的示例,您将拥有128 MB的堆空间。

我想你需要找出点击提交/登录时会发生什么,因为我怀疑jvm在执行此操作时应该耗尽内存。 也许它会进入某种无限循环并继续下载数据? 也许谷歌的登录可以识别非浏览器何时尝试登录?

尝试增加启动应用程序的堆空间量; 例如

 java -Xmx256M ... 

您可能还需要在某个时刻增加permSize …所以使用以下内容来避免两者:

java -Xmx512m -XX:MaxPermSize = 256m …

您将收到java.lang.OutOfMemoryError:Java堆空间

1)在java中解决OutOfMemoryError的简单方法是通过使用JVM选项“-Xmx512M”来增加最大堆大小,这将立即解决您的OutOfMemoryError。

当我在构建项目时在Eclipse,Maven或ANT中获得OutOfMemoryError时,这是我的首选解决方案,因为根据项目的大小,您可以轻松地耗尽Memory.here是一个增加JVM最大堆大小的示例。

如果在java应用程序中设置堆大小,最好将-Xmx保持为-Xms比例为1:1或1:1.5

export JVM_ARGS =“ – Xms1024m -Xmx1024m”

2)在Java中解决OutOfMemoryError的第二种方法相当困难,当你没有太多内存时,甚至在增加最大堆大小之后你仍然得到java.lang.OutOfMemoryError,在这种情况下你可能想要分析你的应用程序和寻找任何内存泄漏。

如果您使用的是Eclipse IDE,则可以使用Eclipse Memory Analyzer检查堆转储,也可以使用Netbeans或JProbe等任何分析器。 这是一个棘手的解决方案,需要一些时间来分析和发现内存泄漏。

3)有许多工具可以调查和修复Java中的OutOfMemoryError

—-> Visualgc

Visualgc代表Visual Garbage Collection Monitoring Tool,您可以将它附加到已检测的hostspot JVM。 visualgc的主要优势在于它以图形方式显示所有关键数据,包括类加载器,垃圾收集和JVM编译器性能数据。 目标JVM由其虚拟机标识符标识,也称为vmid。

—-> Jmap

Jmap是JDK6附带的命令行实用程序,允许您在文件中获取堆的内存转储。 它很容易用作下面的shwon:

jmap -dump:format = b,file = heapdump 6054

这里file指定内存转储文件的名称,即“heapdump”,6054是Java进度的PID。 您可以使用“ps -ef”或Windows任务管理器或使用名为“jps”(Java虚拟机进程状态工具)的工具找到PDI。

—-> Jhat

Jhat之前被称为hat(堆分析器工具),但它现在是JDK6的一部分。 您可以使用jhat分析使用“jmap”创建的堆转储文件。 Jhat也是一个命令行实用程序,您可以从cmd窗口中浏览它,如下所示:

jhat -J-Xmx256m heapdump

这里它将分析文件“heapdump”中包含的内存转储。 当你启动jhat时它会读取这个堆转储文件,然后开始在http端口上监听,只需将浏览器指向jhat正在监听的端口7000,然后你就可以开始分析堆转储中存在的对象了。

希望这会有所帮助。