javamemory-leaksgarbage-collectionapache-httpclient-4.xapache-httpasyncclient

Apache HttpAsyncClient memory leak


I need to use HttpAsyncClient under the high load. I create HttpAsyncClient like this:

RequestConfig requestConfig = RequestConfig.custom()
        .setConnectTimeout(CONNECT_TIMEOUT)
        .setSocketTimeout(SOCKET_TIMEOUT)
        .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
        .build();
HttpAsyncClient client = HttpAsyncClients.custom()
        .setDefaultRequestConfig(createRequestConfig())
        .build();

And then I use it like this:

HttpPost request = new HttpPost(url);
request.setEntity(new StringEntity(requestBody, "UTF-8"));
client.execute(request, null)

Usually I don't really care about response, so I don't initialize Future<HttpResponse> variable and don't do Future.get(). Well, just for the clarification (I don't think it has something to do with question), I care about responses sometimes, but 99% of responses are not interesting for me.

The problem is when I do a lot of requests (for instance 300 every second, and by "request" word here I mean client.execute() invokation) I finally get java.lang.OutOfMemoryError: GC overhead limit exceeded. I tried to use VisualVM to find out what's happening. I see that java.lang.Object[], char[], java.lang.String, byte[], short, char[] instances count is growing (I tried to force GC and to limit heap size to be sure it isn't normal - didn't help). And so is growing the used heap space.

What causes this problem? Maybe I should use HttpAsyncClient some different way? Do I need to use custom RequestProducer, ResponseProducer or to use CountDownLatch?

UPD The problem was because of PowerMock library


Solution

  • The reason was that I were using PowerMock to run my tests. Without PowerMock everything works fine.