KursikS
KursikS

Reputation: 326

java.lang.OutOfMemoryError for writing of InputStream in FileOutputStream

I can not understand, what I do wrong:

public static void writeToFile(InputStream inputStream, File file) throws IOException, FileNotFoundException {
    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
    try {
        int size = 1024 * 1024;
        byte[] buf = new byte[size];
        int byteRead;
        while ((byteRead = inputStream.read(buf)) > 0) {
            outputStream.write(buf, 0, byteRead);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Exception:

6157-6185/com.wiserep E/AndroidRuntime﹕ FATAL EXCEPTION:
IntentService[SynchronizationService] java.lang.OutOfMemoryError
    at java.lang.String.<init>(String.java:432)
    at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:642)
    at java.lang.StringBuffer.toString(StringBuffer.java:723)
    at com.splunk.mint.network.io.InputStreamMonitor.updateBody(InputStreamMonitor.java:104)
    at com.splunk.mint.network.io.InputStreamMonitor.read(InputStreamMonitor.java:71)
    at com.wiserep.web.HttpTransport$HttpHelper.writeToFile(HttpTransport.java:196)    

libraries:

Upvotes: 0

Views: 1108

Answers (2)

Joni
Joni

Reputation: 111259

You are using Splunk Mint to monitor the application. The bug is in the code from Splunk: it tries to create a string with the entire contents of the stream, who knows what for, and this is what causes the app to run out of memory. There has to be a way to limit the size of the part of the stream that Splunk Mint captures, or disable this particular feature completely.

Upvotes: 4

&#193;lvaro G&#243;mez
&#193;lvaro G&#243;mez

Reputation: 306

Maybe the cause it's that you are requesting chunks of memory too big and the phone doesn't have any contiguous block big enough to allocate it, that's why it's giving Outofmemoryerror. Try to lower the chunk size

Upvotes: 0

Related Questions