title: How to fix OutOfMemoryError thrown while trying to throw OutOfMemoryError with retrofit
body: <p>I am using retrofit to download some media files like video,mp3, jpg, pdf,... in my application.threre is a problem when I want to download a large file with 55MB with the format of mp4. when I want to download this file I get an error like this:</p>
  
  <pre><code>OutOfMemoryError threw while trying to throw OutOfMemoryError; no stack trace available
  </code></pre>
  
  <p>this is my code:</p>
  
  <pre><code>  private void downloadFile() {
  
      ArrayList&lt;FileModel&gt; filesInDB = G.bootFileFromFileDB();
  
      for (final FileModel fm : filesInDB) {
  
        APIService downloadService = ServiceGenerator.createServiceFile(APIService.class, "username", "password");
  
        //Id of apk file that you want to download
        Call&lt;ResponseBody&gt; call = downloadService.downloadFileWithDynamicUrlSync("file/download/" + String.valueOf(fm.getFileId()));
        call.enqueue(new Callback&lt;ResponseBody&gt;() {
  
          @Override
          public void onResponse(Call&lt;ResponseBody&gt; call, Response&lt;ResponseBody&gt; response) {
            if (response.isSuccess()) {
  
              Log.d("LOGOO", "server contacted and has file");
  
              boolean writtenToDisk = writeResponseBodyToDisk(response.body(), fm.getFileName(), fm.getFileExtension());
  
              response = null;
  
  
              Log.d("LOGOO", "file download was a success? " + writtenToDisk);
  
  
            } else {
              Log.d("LOGOO", "server contact failed");
            }
          }
  
          @Override
          public void onFailure(Call&lt;ResponseBody&gt; call, Throwable t) {
            Log.i("LOGO", "Error is : " + t.getMessage());
  
            Toast.makeText(ActivityInternet.this, R.string.internet_error, Toast.LENGTH_LONG).show();
            Intent intent = new Intent(ActivityInternet.this, ActivityStartup.class);
            startActivity(intent);
          }
        });
      }
  </code></pre>
  
  <p>and this is 'writeResponseBodyToDisk' method that I'm using:</p>
  
  <pre><code>  private boolean writeResponseBodyToDisk(ResponseBody body, String fileName, String fileExtension) {
  
      try {
  
        // Location to save downloaded file and filename
        File futureStudioIconFile = new File(G.DIR_APP + fileName + fileExtension);
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
          byte[] fileReader = new byte[4096];
          long fileSize = body.contentLength();
          long fileSizeDownloaded = 0;
          inputStream = body.byteStream();
          outputStream = new FileOutputStream(futureStudioIconFile);
          while (true) {
            int read = inputStream.read(fileReader);
            if (read == -1) {
              break;
            }
            outputStream.write(fileReader, 0, read);
            fileSizeDownloaded += read;
            Log.d("LOGO", "file download: " + fileSizeDownloaded + " of " + fileSize);
          }
          outputStream.flush();
          return true;
        } catch (IOException e) {
          return false;
        } finally {
          if (inputStream != null) {
            inputStream.close();
          }
          if (outputStream != null) {
            outputStream.close();
          }
        }
      } catch (IOException e) {
        return false;
      }
  
  
    }
  </code></pre>
  
  <p>finally this is my createServiceFile method:</p>
  
  <pre><code>public static &lt;S&gt; S createServiceFile(Class&lt;S&gt; serviceClass, String username, String password) {
          if (username != null &amp;&amp; password != null) {
  
              String credentials = username + ":" + password;
              final String basic =
                "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
              httpClient.addInterceptor(new Interceptor() {
                  @Override
                  public Response intercept(Chain chain) throws IOException {
                      Request original = chain.request();
                      Request.Builder requestBuilder = original.newBuilder()
                        .header("Authorization", basic)
                        .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,audio/mp4,image/jpeg,*/*;q=0.8")
                        .method(original.method(), original.body());
                      Request request = requestBuilder.build();
                      return chain.proceed(request);
                  }
              });
          }
          OkHttpClient client = httpClient.build();
          Retrofit retrofit = builder.client(client).build();
          return retrofit.create(serviceClass);
      }
  </code></pre>
  
  <p>I really appreciate if you can help me :)</p>