How to get rid of "java.lang.OutOfMemoryError: Java heap space" error?

159 Views Asked by At

I'm taking multipart file in rest input and validating some properties of file after which I'm uploading a multipart file to different service using file.getResource()

    try {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        **body.add("model", file.getResource());**
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
}

I need to pass the entire file to a downstream system while doing so, JVM memory is getting increased exorbitantly and it is not freeing up the space for a very long time meanwhile multiple such operations are utilizing the entire heap space.

I could increase the max heap size using command line option to java, but that doesn't seems to be a feasible solution going forward.

I noticed via JProfiler that it takes a lot space in heap memory and the main problem is that it doesn't freeing up the space after execution of that api.

I'm sharing screenshot of JProfiler too. You can see that the graph is not coming down as its not emptying the space. (https://i.stack.imgur.com/ycrRJ.png)

I'm looking for a solution where I can take the file in input and upload to the downstream application while utilizing very minimum heap space.

Thanks in advance.

0

There are 0 best solutions below