You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HttpComponentsClientHttpRequestFactory.setBufferRequestBody(false) does not stop OutOfMemoryError [SPR-11977] #16593
I am using HttpComponentsClientHttpRequestFactory to establish HttpConnection pooling. I am getting OutOfMemoryError. To avoid this as suggested I used HttpComponentsClientHttpRequestFactory.setBufferRequestBody(false) to prevent the payload getting buffered. But still I get the same OutOfMemoryError.
Here is the code snippet.
public String send(String url, String request) throws RestClientException {
// Create the HttpComponentsClientHttpRequestFactory
HttpComponentsClientHttpRequestFactory preconfiguredHTTPInstance = new HttpComponentsClientHttpRequestFactory();
// Set BufferRequestBody as false, default is true.
preconfiguredHTTPInstance.setBufferRequestBody(false);
preconfiguredHTTPInstance.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(preconfiguredHTTPInstance);
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
String responseString = restTemplate.postForObject(url, request, String.class);
return responseString;
}
I did quite an extensive debugging and profiling session on your code using a 30MB file as input. It turns out that the streaming of the request works just fine and the memory hotspot is on the request object holding those 30MB.
I tried to change your original code to stream the request as well instead of holding it in memory. This turns out to be much harder than I thought so we need to work on that.
Well, that's exactly what I am saying! My tests show that storing the data in a String is the reason of the OutOfMemoryError. If you read that data from a stream instead, the memory usage is perfectly fine.
I would like to confirm that I haven't missed anything. Please look at the code in my previous comment: if that fits with your use case, then I'll resolve this issue as invalid.
Activity
spring-projects-issues commentedon Jul 10, 2014
Stéphane Nicoll commented
Could you please post the OOM stacktrace? Thanks.
spring-projects-issues commentedon Jul 10, 2014
Stéphane Nicoll commented
I did quite an extensive debugging and profiling session on your code using a 30MB file as input. It turns out that the streaming of the request works just fine and the memory hotspot is on the
requestobject holding those 30MB.I tried to change your original code to stream the request as well instead of holding it in memory. This turns out to be much harder than I thought so we need to work on that.
Option 1
Option 2: switch to the simple counter part (that supports the regular callback)
Could you please try this out and let me know if that fixes your issue? Thanks.
spring-projects-issues commentedon Jul 11, 2014
Syam Sarangadharan commented
I am trying to post the string data to the url. I am not trying to read from a file and post the data. Please clarify.
spring-projects-issues commentedon Jul 28, 2014
Stéphane Nicoll commented
Well, that's exactly what I am saying! My tests show that storing the data in a String is the reason of the OutOfMemoryError. If you read that data from a stream instead, the memory usage is perfectly fine.
I would like to confirm that I haven't missed anything. Please look at the code in my previous comment: if that fits with your use case, then I'll resolve this issue as invalid.