网络作业作业中的 Java.lang.OutOfMemoryError

标签 java exception networking heap-memory

我目前正在为我的一门大学类(class)编写网络作业,但遇到了一些问题。此分配包括设置一组节点,这些节点使用一组预定义的消息通过套接字相互通信。这些消息必须能够从发送回第四个套接字的字节数据构建。

以下是处理来自字节 [] 的数据的消息之一内部的方法示例:

    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):

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() 获得的值很可能不是您所期望的。我建议将其打印出来并确保其正确。

可能有一个 endianness问题,或者可能是接收代码与发送代码不 100% 一致。

关于网络作业作业中的 Java.lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9196706/

相关文章:

c - TCP套接字: Server/Client code structure

java - 使用子类的对象调用父类(super class)中的方法它是如何工作的?

java - 在 MySQL : strange error 中将 Java 对象存储为 BLOB

java - 避免嵌套的 try/catch

exception - haskell "exceptions"

java - 无法启动 HSQLDB (java.net.BindException : Address already in use)

python - 简单服务器-客户端程序的问题

azure - 使用 IP 地址而不是主机名访问 azure 应用程序服务

java - 访问 java 项目中的 osgi 声明式服务

java - 如何在类中的多个方法中检索 Http session 数据