Skip to content
This repository was archived by the owner on Apr 5, 2022. It is now read-only.
This repository was archived by the owner on Apr 5, 2022. It is now read-only.

OutOfMemoryError from RestTemplate.getForObject #230

Open
@kosarko

Description

@kosarko

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions