title: High memory usage when using aws-sdk-java to upload a multipart file to Amazon S3 via streaming
body: <p>The method below in my Java SpringBoot application directly streams and uploads a <code>InputStream</code> to an Amazon S3 bucket using the <code>TransferManager</code>,  and the <code>InputStream</code> is derived by the <code>MultipartFile</code>. I found that the code costs so many memory and I don't know why. I also found some similar questions: <a href="https://stackoverflow.com/questions/58245893/high-memory-usage-when-uploading-a-multipart-file-to-amazon-s3-via-streaming">High memory usage when uploading a multipart file to Amazon S3 via streaming?</a> and <a href="https://stackoverflow.com/questions/54379555/java-heap-space-is-insufficient-to-upload-files-on-aws-s3">Java Heap Space is insufficient to upload files on AWS S3</a>, but nothing help. Can anyone give me some help or suggestions, thank you.</p>
  <pre class="lang-java prettyprint-override"><code>public void upload(String key, MultipartFile file) throws AmazonClientException, InterruptedException, IOException {
          TransferManager tm = null;
          try {
              ObjectMetadata objectMetadata = new ObjectMetadata();
              objectMetadata.setContentLength(file.getSize());
              tm = TransferManagerBuilder.standard().withS3Client(s3).withMinimumUploadPartSize(5 * 1024 * 1024L).withMultipartUploadThreshold(5 * 1024 * 1024L).build();
              Upload upload = tm.upload(s3Config.getBucket(), key, file.getInputStream(), objectMetadata);
              upload.waitForCompletion();
          } finally {
              input.close();
              if (tm != null) {
                  tm.shutdownNow();
              }
          }
      }
  </code></pre>