[英]java.lang.OutOfMemoryError in Java
我想通過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的堆空間。
嘗試增加啟動應用程序的堆空間量; 例如
java -Xmx256M ...
我認為您需要了解點擊提交/登錄時會發生什么,因為我懷疑jvm在執行此操作時應該耗盡內存。 也許它會進入某種無限循環並繼續下載數據? 也許Google的登錄會識別非瀏覽器何時嘗試登錄?
您可能還需要在某個時刻增加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默認情況下你就可以開始分析堆轉儲中存在的對象了。
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.