我目前正在爲我的大學課程之一編寫一個網絡任務,我遇到了一些問題。此分配包括設置一組使用預定義消息在套接字上相互通信的節點。這些消息必須能夠由發回的字節數據和第四個套接字構成。與網絡作業分配的Java.lang.OutOfMemoryError
這裏是消息中的一個的內部的方法,該方法從一個字節[]處理數據的一個示例:
public void processData(byte [] data) throws IOException
{
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(data));
//get the status code
statusCode = (byte)stream.read();
System.out.println("Status Code: "+statusCode);
//get the additional info
byte [] additionalInfoData = new byte[stream.readInt()];
stream.read(additionalInfoData, 0, additionalInfoData.length);
additionalInfo = new String(additionalInfoData);
System.out.println("additionalInfo: "+statusCode);
stream.close();
}
當運行該程序,並做到了這一點(這是在第一後郵件已經發送),它會暫停在
byte [] additionalInfoData = new byte[stream.readInt()];
與此異常:
Exception in thread "Thread-1" java.lang.OutOfMemoryError: Java heap space at cdn.wireformats.RegisterResponse.processData(RegisterResponse.java:58) at cdn.wireformats.RegisterResponse.<init>(RegisterResponse.java:31) at cdn.wireformats.WireFormatFactory.getWireFormatMessage(WireFormatFactory.java:22) at cdn.communications.Link.readMessage(Link.java:62) at cdn.communications.Link.run(Link.java:98) at java.lang.Thread.run(Thread.java:679)
有沒有人有任何想法如何解決這個問題?我是否正確讀取了byte []的數據?
流有多大,即stream.readInt()返回的是什麼?你給JVM多少內存?您是否設置了最大內存或將其保留爲默認值?哪個JVM - 熱點,JRockit,...? – Thomas 2012-02-08 15:53:20