Unable to write into DataOutputStream beyond a specific size - OutOfMemoryError

前端 未结 2 609
孤街浪徒
孤街浪徒 2020-12-18 06:07

I have the following code which produces an OutOfMemory exception:

byte[] buf = new byte[10240];
int len = 0;
DataOutputStream dataOS = new DataOutputStream(         


        
相关标签:
2条回答
  • 2020-12-18 06:25

    Depending on the implementation of OutputStream, writing may buffer the content in memory, and not actually send it. If you buffer enough content, you will of course run out of memory.

    At some interval, perhaps each time through your loop, you should call OutputStream.flush(). If there is a buffer in use, that will clear it.

    0 讨论(0)
  • 2020-12-18 06:34

    It has nothing to do with the DataOutputStream (which maintains no data whatsoever) and everything to do with your underlying stream conn.getOutputStream(). now, you haven't show the relevant code there, but i'm going to guess "conn" is an instance of HttpURLConnection. i believe the outputstream for HttpURLConnection buffers the output so that it can determine the output length (unless you set it explicitly). if you know the output length, you could set that directly using HttpURLConnection.setFixedLengthStreamingMode, or you could call HttpURLConnection.setChunkedStreamMode to allow the data to be sent in chunks instead of buffered entirely in memory.

    in the future, when you encounter an OOME you should always generate a heap dump and open that in a memory profiler. that will almost always show you immediately where the problem is (e.g. in the underlying stream, not the DataOutputStream).

    0 讨论(0)
提交回复
热议问题