1

I want to upload a file to net server use the codes. but when the size > 6M ,throw a FATAL EXCEPTION java.lang.OutOfMemoryError

    HttpURLConnection conn;
    URL url = new URL(mServerUrl);
    conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", file.getContentType());
    conn.setRequestProperty("Connection", "close");
    conn.setRequestProperty("Charset", "UTF-8");
    conn.connect();
    DataOutputStream os = new DataOutputStream(conn.getOutputStream());

    int totalSize = 0;
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = file.getInStream().read(buffer, 0, 1024)) != -1) {

        totalSize += len;
        WDLog.i("totalSize = " + totalSize);
        os.write(buffer, 0, len);

    }

    file.getInStream().close();
    os.flush();
    os.close();

what could i do to solve the question?? thanks

there are logcat infos:

12-26 19:35:06.976: D/dalvikvm(8952): GC_FOR_MALLOC freed 36718 objects / 1675552 bytes in 53ms
12-26 19:35:06.976: I/dalvikvm-heap(8952): Forcing collection of SoftReferences for 12580880-byte allocation
12-26 19:35:07.006: D/dalvikvm(8952): GC_FOR_MALLOC freed 646 objects / 47808 bytes in 34ms
12-26 19:35:07.006: E/dalvikvm-heap(8952): Out of memory on a 12580880-byte allocation.
12-26 19:35:07.006: I/dalvikvm(8952): "Thread-29" prio=5 tid=8 RUNNABLE
12-26 19:35:07.006: I/dalvikvm(8952):   | group="main" sCount=0 dsCount=0 s=N obj=0x46230978 self=0x30eb98
12-26 19:35:07.006: I/dalvikvm(8952):   | sysTid=8980 nice=0 sched=0/0 cgrp=default handle=3159096
12-26 19:35:07.016: I/dalvikvm(8952):   | schedstat=( 5075164776 7730011023 9520 )
12-26 19:35:07.016: I/dalvikvm(8952):   at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~93)
12-26 19:35:07.016: I/dalvikvm(8952):   at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
12-26 19:35:07.016: I/dalvikvm(8952):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$DefaultHttpOutputStream.write(HttpURLConnectionImpl.java:750)
12-26 19:35:07.016: I/dalvikvm(8952):   at java.io.DataOutputStream.write(DataOutputStream.java:101)
12-26 19:35:07.016: I/dalvikvm(8952):   at com.amtcmv.upload.UploadImgVidFile.postVid(UploadImgVidFile.java:218)
12-26 19:35:07.016: I/dalvikvm(8952):   at com.amtcmv.upload.UploadImgVidFile.run(UploadImgVidFile.java:95)
12-26 19:35:07.016: I/dalvikvm(8952):   at java.lang.Thread.run(Thread.java:1102)
12-26 19:35:07.016: E/dalvikvm(8952): Out of memory: Heap Size=19783KB, Allocated=9352KB, Bitmap Size=162KB
12-26 19:35:07.016: W/dalvikvm(8952): threadid=8: thread exiting with uncaught exception (group=0x400259f8)
12-26 19:35:07.026: E/AndroidRuntime(8952): FATAL EXCEPTION: Thread-29
12-26 19:35:07.026: E/AndroidRuntime(8952): java.lang.OutOfMemoryError
12-26 19:35:07.026: E/AndroidRuntime(8952):     at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:93)
12-26 19:35:07.026: E/AndroidRuntime(8952):     at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
12-26 19:35:07.026: E/AndroidRuntime(8952):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$DefaultHttpOutputStream.write(HttpURLConnectionImpl.java:750)
12-26 19:35:07.026: E/AndroidRuntime(8952):     at java.io.DataOutputStream.write(DataOutputStream.java:101)
12-26 19:35:07.026: E/AndroidRuntime(8952):     at com.amtcmv.upload.UploadImgVidFile.postVid(UploadImgVidFile.java:218)
12-26 19:35:07.026: E/AndroidRuntime(8952):     at com.amtcmv.upload.UploadImgVidFile.run(UploadImgVidFile.java:95)
12-26 19:35:07.026: E/AndroidRuntime(8952):     at java.lang.Thread.run(Thread.java:1102)

and at

os.write(buffer, 0, len);

this line throw the error.

Kara
  • 6,115
  • 16
  • 50
  • 57
Ray
  • 468
  • 4
  • 17

2 Answers2

3

Upload the file to the server in chunk by chunk basis. example. first upload 1024 bytes of data after getting response from server upload the next 1024 bytes of data and so on until the all the data of the file is uploaded.

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • I solve the question like this: in while Cycle add if (totalSize > 5* 1024 * 1024) break out and do ( os.flush(); os.close();conn.disconnect(); System.gc();) don't close the file inputstream. and if unfinish redo upload function. – Ray Dec 27 '11 at 02:56
1

If you testing this code on emulator, make sure that your RAM size close to real device value.

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146