0

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:

  • import android.util.Log;
  • import org.apache.http.NameValuePair;
  • import javax.net.ssl.*;
  • import java.io.*;
  • import java.net.*;
  • import java.security.cert.X509Certificate;
  • import java.text.SimpleDateFormat;
  • import java.util.Date;
  • import java.util.List; import java.util.Locale;
2
  • Avoid assigning and reading the value in the same operation. This will only confuse you and anyone else reading the code. Commented Jan 9, 2015 at 9:23
  • 1
    What makes you think that the particular OME is coming from the code you mentioned ? Looks like you are using some library com.splunk.mint.network.io. Commented Jan 9, 2015 at 9:23

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

0

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

1 Comment

This code works good in older project build with current backend api, but now i get this error when try launch my old java code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.