简体   繁体   English

从InputStream读取导致OutOfMemoryError

[英]Read from InputStream causes OutOfMemoryError

My app connects to a Wi-Fi peripheral. 我的应用程序连接到Wi-Fi外围设备。 I'm using Socket.getInputStream() and Socket.getOutputStream() to read/write data. 我正在使用Socket.getInputStream()Socket.getOutputStream()读取/写入数据。 When the connection is established I store these two streams so that I can reuse them as long as I'm connected. 建立连接后,我将存储这两个流,以便我只要连接就可以重用它们。 My app sends a command via the OutputStream every second and reads the result from the InputStream by its read() method. 我的应用程序每秒通过OutputStream发送一条命令,并通过其read()方法从InputStream读取结果。 After some time I get an "OutOfMemoryError". 一段时间后,我收到“ OutOfMemoryError”。 Correct me if I'm wrong but I think this is because read() does not remove the read data from the InputStream , right? 如果我错了,请纠正我,但我认为这是因为read()不会从InputStream删除读取的数据,对吗?

My question is: Is it a good practice to store the Streams? 我的问题是:存储流是否是一种好习惯? Or should I use Socket.getInputStream() , Socket.getOutputStream() every time I send a new command? 还是应该在每次发送新命令时都使用Socket.getInputStream()Socket.getOutputStream()

It seems not to be a problem with the OutputStream since I can call flush() for that. 因为我可以为此调用flush() ,所以OutputStream似乎没有问题。 What about reset() of InputStream ? 那么InputStream reset()呢? Does this remove the data for the stream? 这会删除流的数据吗?

Here is the code how I encapsulate my Streams: 这是我封装流的代码:

@Override
public InputStream getInputStream() throws IOException {
    return _Socket.getInputStream();
}

@Override
public OutputStream getOutputStream() throws IOException {
    return _Socket.getOutputStream();
}

@Override
public void connect() throws IOException {
    try {
        SocketAddress socketAddress = new InetSocketAddress(_ip, _port);

        _Socket = new Socket(_ip, _port);
    } catch (IOException e) {
        MyExceptionHandler.appendLog(MyExceptionHandler.exceptionToString(e));

        throw e;
    }
}

The code for sending and receiving commands comes from this api: 用于发送和接收命令的代码来自此api:

https://github.com/pires/obd-java-api/blob/master/src/main/java/com/github/pires/obd/commands/ObdCommand.java https://github.com/pires/obd-java-api/blob/master/src/main/java/com/github/pires/obd/commands/ObdCommand.java

The exception does also not come immediately. 异常也不会立即出现。 It occurs after ~30 Minutes and a lot of commands sent/received 它在〜30分钟后发生,并且发送/接收了大量命令

Correct me if I'm wrong but I think this is because “read()” does not remove the read data from the InputStream, right? 如果我错了,请纠正我,但我认为这是因为“ read()”不会从InputStream中删除读取的数据,对吗?

Wrong. 错误。 If you're running out of memory it's not because of InputStream. 如果内存不足,则不是因为InputStream。 You have a bug in your code. 您的代码中有一个错误。

My question is: Is it a good practice to store the Streams? 我的问题是:存储流是否是一种好习惯?

Yes. 是。

Or should I use “Socket.getInputStream(), Socket.getOutputStream() every time I send a new command? 还是应该在每次发送新命令时都使用“ Socket.getInputStream(),Socket.getOutputStream()”?

No. 没有。

What about “reset()” of InputStream? 那么InputStream的“ reset()”呢? Removes this the data for the stream? 要删除该数据流吗?

No, it does what it says in the Javadoc. 不,它按照Javadoc中的说明进行操作。

EDIT The code you have linked to is at first inspection a load of rubbish. 编辑您链接到的代码首先要检查一堆垃圾。 It never checks for end of stream for example, so when that happens it will read forever, accumulating 0xff bytes and eventually filling up memory. 例如,它从不检查流的末尾,因此当它发生时,它将永远读取,累积0xff字节并最终填满内存。 Find something better. 找到更好的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM