我有这个代码,我试图在OutputStream上只使用1kb来避免java.lang.OutOfMemoryError,试图上传超过10mb的文件。但在传输一些字节后,我在while
中得到了java.lang.OutOfMemoryError。我想知道为什么我得到java.lang.OutOfMemoryError以及如何不使用大量内存。
public static HttpURLConnection getConnection(String url, String methode, String contenType) throws IOException {
if(methode == null){
methode = "POST";
}
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
httpURLConnection.setRequestMethod(methode);
httpURLConnection.setRequestProperty("Authorization", TOKEN);
httpURLConnection.setRequestProperty("content-type", contenType);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
return httpURLConnection;
}
HttpURLConnection httpURLConnection = getConnection(httpsUrl, null, "application/octet-stream");
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
if(Build.VERSION.SDK_INT == 19)
httpURLConnection.setFixedLengthStreamingMode(file.length());
else httpURLConnection.setFixedLengthStreamingMode((int) file.length());
bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
bufferedOutputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
byte[] bytes = new byte[1024];
int len;
long count = 0L;
while ((len = bufferedInputStream.read(bytes)) != -1){
bufferedOutputStream.write(bytes, 0, len);
count += len;
//callback for bytes uploaded
uploadProgress.progress(count, file.length());
//I think calling flush will release the bytes from the BufferedOutputStream
bufferedOutputStream.flush();
}
String error = hasError(httpURLConnection.getErrorStream());
if(error != null){
throw new IOException(error);
}
String repons = getRepons(httpURLConnection.getInputStream());
Log.d("repons uploded file", repons);
}finally {
httpURLConnection.disconnect();
if(bufferedInputStream != null)
try {
bufferedInputStream.close();
}catch (Exception e){e.printStackTrace();}
if(bufferedOutputStream != null){
try{
bufferedOutputStream.close();
}catch (Exception e){e.printStackTrace();}
}
}
我的日志:
04-16 02:39:01.526 4633-5590/ I/global: Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
04-16 02:39:01.536 4633-5590/ I/global: Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
04-16 02:39:06.687 4633-5590/ D/dalvikvm: GC_FOR_MALLOC freed 10847 objects / 763744 bytes in 356ms
04-16 02:39:06.737 4633-5590/ I/dalvikvm-heap: Grow heap (frag case) to 9.830MB for 784400-byte allocation
04-16 02:39:07.026 4633-4634/ D/dalvikvm: GC_FOR_MALLOC freed 1171 objects / 274736 bytes in 289ms
04-16 02:39:11.726 4633-5590/ D/dalvikvm: GC_FOR_MALLOC freed 4705 objects / 544488 bytes in 255ms
04-16 02:39:11.786 4633-5590/ I/dalvikvm-heap: Grow heap (frag case) to 10.599MB for 1570832-byte allocation
04-16 02:39:12.046 4633-4634/ D/dalvikvm: GC_FOR_MALLOC freed 5 objects / 88 bytes in 256ms
04-16 02:39:21.346 4633-5590/ D/dalvikvm: GC_FOR_MALLOC freed 8546 objects / 1046152 bytes in 322ms
04-16 02:39:21.446 4633-5590/ I/dalvikvm-heap: Grow heap (frag case) to 12.849MB for 3143696-byte allocation
04-16 02:39:21.806 4633-4634/ D/dalvikvm: GC_FOR_MALLOC freed 11 objects / 200 bytes in 331ms
04-16 02:39:40.156 4633-5590/ D/dalvikvm: GC_FOR_MALLOC freed 17089 objects / 2087952 bytes in 280ms
04-16 02:39:40.166 4633-5590/ I/dalvikvm-heap: Forcing collection of SoftReferences for 6289424-byte allocation
04-16 02:39:40.546 4633-5590/ D/dalvikvm: GC_FOR_MALLOC freed 24 objects / 392 bytes in 382ms
04-16 02:39:40.546 4633-5590/ E/dalvikvm-heap: Out of memory on a 6289424-byte allocation.
04-16 02:39:40.556 4633-5590/ I/dalvikvm: "Thread-30" prio=5 tid=14 RUNNABLE
04-16 02:39:40.566 4633-5590/ I/dalvikvm: | group="main" sCount=0 dsCount=0 s=N obj=0x43f8f1c0 self=0x331ae8
04-16 02:39:40.566 4633-5590/ I/dalvikvm: | sysTid=5590 nice=0 sched=0/0 cgrp=default handle=3437800
04-16 02:39:40.566 4633-5590/ I/dalvikvm: | schedstat=( 8153745356 27960472724 3728 )
04-16 02:39:40.576 4633-5590/ I/dalvikvm: at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~93)
04-16 02:39:40.576 4633-5590/ I/dalvikvm: at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
04-16 02:39:40.576 4633-5590/ I/dalvikvm: at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$DefaultHttpOutputStream.write(HttpURLConnectionImpl.java:750)
04-16 02:39:40.586 4633-5590/ I/dalvikvm: at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:224)
04-16 02:39:40.586 4633-5590/ I/dalvikvm: at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:113)
04-16 02:39:40.586 4633-5590/ I/dalvikvm: at in.guervyl.dailysongs.DropBoxApi.uploadFile(DropBoxApi.java:217)
04-16 02:39:40.596 4633-5590/ I/dalvikvm: at in.guervyl.dailysongs.upload.thread.SongUploadThread.run(SongUploadThread.java:113)
04-16 02:39:40.856 4633-5590/ D/dalvikvm: GC_FOR_MALLOC freed 19 objects / 8824 bytes in 249ms
04-16 02:39:40.856 4633-5590/ I/dalvikvm-heap: Forcing collection of SoftReferences for 6289424-byte allocation
04-16 02:39:41.166 4633-5590/ D/dalvikvm: GC_FOR_MALLOC freed 0 objects / 0 bytes in 310ms
04-16 02:39:41.186 4633-5590/ E/dalvikvm-heap: Out of memory on a 6289424-byte allocation.
04-16 02:39:41.186 4633-5590/ I/dalvikvm: "Thread-30" prio=5 tid=14 RUNNABLE
04-16 02:39:41.186 4633-5590/ I/dalvikvm: | group="main" sCount=0 dsCount=0 s=N obj=0x43f8f1c0 self=0x331ae8
04-16 02:39:41.186 4633-5590/ I/dalvikvm: | sysTid=5590 nice=0 sched=0/0 cgrp=default handle=3437800
04-16 02:39:41.196 4633-5590/ I/dalvikvm: | schedstat=( 8345060001 28395355021 3774 )
04-16 02:39:41.196 4633-5590/ I/dalvikvm: at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~93)
04-16 02:39:41.206 4633-5590/ I/dalvikvm: at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
04-16 02:39:41.206 4633-5590/ I/dalvikvm: at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$DefaultHttpOutputStream.write(HttpURLConnectionImpl.java:750)
04-16 02:39:41.206 4633-5590/ I/dalvikvm: at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:224)
04-16 02:39:41.206 4633-5590/ I/dalvikvm: at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:113)
04-16 02:39:41.206 4633-5590/ I/dalvikvm: at java.io.FilterOutputStream.close(FilterOutputStream.java:61)
04-16 02:39:41.216 4633-5590/ I/dalvikvm: at java.io.BufferedOutputStream.close(BufferedOutputStream.java:187)
04-16 02:39:41.216 4633-5590/ I/dalvikvm: at in.guervyl.dailysongs.DropBoxApi.uploadFile(DropBoxApi.java:238)
04-16 02:39:41.226 4633-5590/ I/dalvikvm: at in.guervyl.dailysongs.upload.thread.SongUploadThread.run(SongUploadThread.java:113)
04-16 02:39:41.256 4633-5590/ W/dalvikvm: threadid=14: thread exiting with uncaught exception (group=0x4001d800)
04-16 02:39:41.256 4633-5590/ E/AndroidRuntime: FATAL EXCEPTION: Thread-30
java.lang.OutOfMemoryError
at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$DefaultHttpOutputStream.write(HttpURLConnectionImpl.java:750)
at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:224)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:113)
at java.io.FilterOutputStream.close(FilterOutputStream.java:61)
at java.io.BufferedOutputStream.close(BufferedOutputStream.java:187)
at in.guervyl.dailysongs.DropBoxApi.uploadFile(DropBoxApi.java:238)
at in.guervyl.dailysongs.upload.thread.SongUploadThread.run(SongUploadThread.java:113)
04-16 02:39:44.896 4633-5590/ I/Process: Sending signal. PID: 4633 SIG: 9