【发布时间】:2012-03-01 02:06:48
【问题描述】:
我目前正在为我的一门大学课程编写网络作业,但遇到了一些问题。此分配包括设置一组节点,这些节点使用一组预定义的消息通过套接字相互通信。这些消息必须能够从发回的字节数据和第四个套接字构造。
这是处理来自字节 [] 的数据的消息之一中的方法示例:
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)
有人知道如何解决这个问题吗?我是否错误地从字节 [] 读取数据?
【问题讨论】:
-
流有多大,即
stream.readInt()返回什么?你给JVM多少内存?您是否设置了最大内存或将其保留为默认值?哪个 JVM - Hotspot、JRockit、...?
标签: java exception networking heap-memory