java.lang.OutOfMemoryError: Java heap space thrown from java.net.http.HttpClient

25 views Asked by At

My Java application sends HTTP requests through java.net.http.HttpClient. The class that holds the HttpClient is a singleton that initializes the client in the constructor, which means only one HttpClient was created. The HttpClient is used from multiple threads and sends requests to an Nginx used to load balance the requests.

When sending a large request I'm getting java.lang.OutOfMemoryError: Java heap space from the HttpClient.

HttpClient initialization:

httpClient  = HttpClient.newBuilder().build();

HTTP request:

HttpRequest.newBuilder().uri(URI.create(String.format(Config.getConfig("URL"), HOST, POST))).header("Content-Type", "application/json").POST(BodyPublishers.ofString(queryString)).build();

HTTP request send (From this line the exception is thrown):

httpClient.send(request, BodyHandlers.ofString());

Am I using the HttpClient the wrong way? Is the exception related to the HttpClient? Why do I get this exception?

0

There are 0 answers