OutOfMemoryError from RestTemplate.getForObject
kosarko opened this issue · comments
Summary
When you are fetching large data (imagine drive file with size in GBs) there's a possibility for OutOfMemoryError if the data does not fit on the heap.
Actual Behavior
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3236)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
at org.springframework.util.StreamUtils.copy(StreamUtils.java:125)
at org.springframework.util.StreamUtils.copyToByteArray(StreamUtils.java:57)
at org.springframework.http.converter.ResourceHttpMessageConverter.readInternal(ResourceHttpMessageConverter.java:65)
at org.springframework.http.converter.ResourceHttpMessageConverter.readInternal(ResourceHttpMessageConverter.java:45)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:153)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:103)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:496)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:452)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:222)
at org.springframework.social.google.api.drive.impl.DriveTemplate.downloadFile(DriveTemplate.java:295)
Expected Behavior
When you need to obtain just the InputStream (ie. you are calling restTemplate.getForObject(url, Resource.class).getInputStream()
) and save the data to a file on disk; the content should not be kept in memory.
The call originates from spring-social-google, but that just calls OAuth2Template.createRestTemplate
, which looks like a reasonable default.
Setting setBufferRequestBody(false)
on the ClientHttpRequestFactory
as suggested in https://jira.spring.io/browse/SPR-7909 might solve this issue, but I'm unsure whether there are other consequences to that.
Version
1.1.4.RELEASE
This isn't precisely a Spring Social issue. From what I understand, it is more related to RestTemplate
from the Spring Framework project. Even if the fix is to call setBufferRequestBody(false)
, that fix would need to be made in Spring Social Google (which is a community-led project, not an officially supported Spring Social project).
I suggest bringing this issue up at https://github.com/spring-social/spring-social-google.