有2个地方可以看到你可能会建立内存使用情况:
>在缓冲区读取你的输入文件.
在缓冲区写入你的输出流(HTTPOutputStream?)
对于#1,我建议通过FileInputStream直接从文件读取而不使用BufferedInputStream.先尝试一下,看看它是否解决了你的问题.即:
FileInputStream in = new FileInputStream(file);
代替:
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
如果#1无法解决问题,您可以尝试在写入很多数据后定期刷新输出流(如有必要,减小块大小):
即:
try
{
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buf=new byte[8192];
int bytesread = 0, bytesBuffered = 0;
while( (bytesread = fileInputStream.read( buf )) > -1 ) {
out.write( buf, 0, bytesread );
bytesBuffered += bytesread;
if (bytesBuffered > 1024 * 1024) { //flush after 1MB
bytesBuffered = 0;
out.flush();
}
}
}
finally {
if (out != null) {
out.flush();
}
}