在 FileOutputStream 中写入 InputStream 时出现 java.lang.OutOfMemoryError

标签 java out-of-memory inputstream fileoutputstream

我不明白,我做错了什么:

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();
    }
}

异常:

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)    
<小时/>

图书馆:

  • 导入 android.util.Log;
  • 导入 org.apache.http.NameValuePair;
  • 导入 javax.net.ssl.*;
  • 导入java.io.*;
  • 导入java.net.*;
  • 导入 java.security.cert.X509Certificate;
  • 导入 java.text.SimpleDateFormat;
  • 导入java.util.Date;
  • 导入 java.util.List;导入java.util.Locale;

最佳答案

您正在使用Splunk Mint来监控应用程序。该错误存在于 Splunk 的代码中:它尝试创建一个包含流的全部内容的字符串,谁知道是做什么用的,这就是导致应用程序内存不足的原因。必须有一种方法来限制 Splunk Mint 捕获的流部分的大小,或者完全禁用此特定功能。

关于在 FileOutputStream 中写入 InputStream 时出现 java.lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27857227/

相关文章:

java - 如何使 Android CircleImageView 位于布局中间

java - Hibernate Cache1 OutOfMemory 与 OpenSessionInView

java - 如何从 iText 获取书面 PDF 的 OutputStream

.net - 如何在 .NET(Excel 加载项)中捕获 OutOfMemoryException

python - 在程序使用太多内存之前停止程序

java - 从给定的类路径获取文件

http - 在 Servlet 中发送和接收二进制数据

Java Properties 属性值 - 带有前导空格的消息

java泛型与 map 通配符

java - 如何使用 REST 将数组作为 POST 参数/有效负载发送到 Android 中的 Web 服务