title: WebFlux WebClient loading entire file into direct buffer memory during multipart upload
body: <p>I'm building an app with Spring Boot 2.1.6.RELEASE &amp; Webflux</p>
  
  <p>We have an endpoint that we can upload files using multipart and are using WebClient for our uploads.</p>
  
  <p>Our upload client code looks like this</p>
  
  <pre><code>@Component
  class UploadClient(
      private val client: WebClient,
  ) {
      suspend fun upload(filePath: String) =
          client.post()
                .uri("/upload")
                .contentType(MULTIPART_FORM_DATA)   
                .body(BodyInserters.fromMultipartData(generateMultipartBody(filePath)))
                .retrieve()
                .bodyToMono(UploadResult::class.java)
                .awaitFirst()
  
      private fun generateMultipartBody(filePath: String): MultiValueMap&lt;String, HttpEntity&lt;*&gt;&gt; {
          val builder = MultipartBodyBuilder()
          builder.part("file", FileSystemResource(filePath))
          return builder.build()
      }
  }
  </code></pre>
  
  <p>However when we upload a large file, (1.6gb) we are seeing that this entire file is loaded into direct memory:</p>
  
  <p><a href="https://i.stack.imgur.com/wVj7C.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wVj7C.png" alt="visualvm"></a></p>
  
  <p>As the file is uploaded, the memory is released, then when the next file is uploaded you can see the spike in memory again.</p>
  
  <p>For contrast I tried replacing WebClient with <a href="https://github.com/AsyncHttpClient/async-http-client" rel="nofollow noreferrer">https://github.com/AsyncHttpClient/async-http-client</a> and the memory usage is much lower, ~60mb per upload</p>
  
  <p>I don't really want to pull in another http client dependency when the WebFlux client is fine for all of our other uses.</p>