1

I am doing a https post of large file using HttpsURLConnection in chunks using link -

file upload using httppost android

I am getting fatal exception -

06-09 11:44:47.041 E/AndroidRuntime( 6250): FATAL EXCEPTION: Thread-712

06-09 11:44:47.041 E/AndroidRuntime( 6250): java.lang.OutOfMemoryError

06-09 11:44:47.041 E/AndroidRuntime( 6250):     at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:91)

06-09 11:44:47.041 E/AndroidRuntime( 6250):     at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:201)

06-09 11:44:47.041 E/AndroidRuntime( 6250):     at libcore.net.http.RetryableOutputStream.write(RetryableOutputStream.java:61)

06-09 11:44:47.041 E/AndroidRuntime( 6250):     at java.io.DataOutputStream.write(DataOutputStream.java:98)

HPROF Analysis -

Leak Suspect

One instance of "libcore.net.http.RetryableOutputStream" loaded by "" occupies 134,216,008 (82.46%) bytes. The memory is accumulated in one instance of "byte[]" loaded by "".

Details

buf java.io.ByteArrayOutputStream @ 0x444fd460 16 134,215,984

content libcore.net.http.RetryableOutputStream @ 0x444fd448 24 134,216,008

out java.io.DataOutputStream @ 0x4467f918 24 48

Can someone please help me getting rid of this.

I have added DataOutputStream.flush as well but for no use

    // read file and write to buffer
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    int pktSend = 0;
    // continue till bytes read not null
    while (bytesRead > 0) {
        dos.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        pktSend++;
        dos.flush();
    }
Community
  • 1
  • 1
Chauhan
  • 71
  • 1
  • 5
  • probably not related, but you should have: `dos.write(buffer, 0, bytesRead);` – marcinj Jun 09 '14 at 06:40
  • DataOutputStream stucks at write() method after adding HttpUrlConnectionObject.setChunkedStreamingMode(maxBufferSize); in the code. Can someone help ?? – Chauhan Jun 11 '14 at 04:57

1 Answers1

0

maybe you can try below settings on your code :

        // set the request mode as POST
        urlConnection.setRequestMethod("POST");

        // disable using cache.
        urlConnection.setUseCaches(false);

        // enable Output/Input.
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
Zephyr
  • 6,123
  • 34
  • 33