# Out of memorry Error When Using Java Client API

**URL:** <https://discuss.elastic.co/t/out-of-memorry-error-when-using-java-client-api/297038>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [February 12, 2022, 6:23am UTC](https://discuss.elastic.co/t/out-of-memorry-error-when-using-java-client-api/297038 "2022-02-12T06:23:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gandalf](https://avatars.discourse-cdn.com/v4/letter/g/59ef9b/32.png) [@Gandalf](https://discuss.elastic.co/u/Gandalf)\
**Post date:** [February 12, 2022, 6:23am UTC](https://discuss.elastic.co/t/out-of-memorry-error-when-using-java-client-api/297038/1 "2022-02-12T06:23:48Z")

</div>

I'm migrating my code from Java Rest High Level API to Java Client API 7.16 version. Everything goes all right in my local enviroment but got the ` OutOfMemoryError: Direct buffer memory` error when I pull it to the Test Enviroment.  
Here is the error logs:

```auto
Exception in thread "I/O dispatcher 79" java.lang.OutOfMemoryError: Direct buffer memory
        at java.nio.Bits.reserveMemory(Bits.java:694)
        at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
        at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311)
        at sun.nio.ch.Util.getTemporaryDirectBuffer(Util.java:241)
        at sun.nio.ch.IOUtil.read(IOUtil.java:195)
        at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
        at ......

```

Once it happenes, all of my operations will be failed with error logs bellow before I restart my project.

```auto
java.lang.IllegalStateException: Request cannot be executed; I/O reactor status: STOPPED
        at org.apache.http.util.Asserts.check(Asserts.java:46) ~[httpcore-4.4.11.jar!/:4.4.11]
        at ......

```

and

```auto
 org.apache.http.ConnectionClosedException: Connection closed unexpectedly
        at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:778) ~[elasticsearch-rest-client-7.4.2.jar!/:7.4.2]
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:218) ~[elasticsearch-rest-client-7.4.2.jar!/:7.4.2]
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:205) ~[elasticsearch-rest-client-7.4.2.jar!/:7.4.2]
        at ......

```

I've increased the memory and the direct buffer memory of jvm, but it still doesn't work.  
And it never happenned by using Java Rest High Level API.  
Is it a bug or an enhancement of the new client API?  
Can anyone suggest me how to troubleshoot this issue?

---

<div class="post-metadata">

**Author:** ![swallez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/swallez/32/11972_2.png) [@swallez](https://discuss.elastic.co/u/swallez)\
**Post date:** [February 12, 2022, 10:07am UTC](https://discuss.elastic.co/t/out-of-memorry-error-when-using-java-client-api/297038/2 "2022-02-12T10:07:50Z")

</div>

In the Java client, the only persistent memory allocations happen in the http library that maintains a pool of connections and buffers.

Are you creating new transport objects (`RestClientTransport` or the underlying `RestClient`) repeatedly? Could it be that they're not properly closed once used, leading their internal buffer pools to stay in memory? Once used, make sure to call `RestClientTransport.close()` or `RestClient.close()` to release their resources. Or even better, only create one transport object that is used throughout the entire application lifetime.

---

<div class="post-metadata">

**Author:** ![Gandalf](https://avatars.discourse-cdn.com/v4/letter/g/59ef9b/32.png) [@Gandalf](https://discuss.elastic.co/u/Gandalf)\
**Post date:** [February 14, 2022, 1:58am UTC](https://discuss.elastic.co/t/out-of-memorry-error-when-using-java-client-api/297038/3 "2022-02-14T01:58:52Z")

</div>

Thanks for your replay. And here is how I create connections which is the same as official documents.

```auto
    public static ElasticsearchClient initEsConnection() {
        RestClient restClient = RestClient.builder(new HttpHost(HOST_NAME, PORT, SCHEME)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }

```

There is only one connection throughout my whole function. So you mean before the function is finished, I should close the connection manually by using `RestClientTransport.close()`?  
By the way, I cannot find methods like `client.close()` to close the searchRequest connections in Java Client API. Is it replaced by ` RestClientTransport.close()` and ` RestClient.close()`?  
Thanks again.

---

<div class="post-metadata">

**Author:** ![Gandalf](https://avatars.discourse-cdn.com/v4/letter/g/59ef9b/32.png) [@Gandalf](https://discuss.elastic.co/u/Gandalf)\
**Post date:** [February 15, 2022, 6:03am UTC](https://discuss.elastic.co/t/out-of-memorry-error-when-using-java-client-api/297038/4 "2022-02-15T06:03:04Z")

</div>

It seems like I solved the problem.  
Finishing my search requests, a close client method will be called:

```auto
     public static void destroyConnection(ElasticsearchClient client) throws IOException {
        ElasticsearchTransport elasticsearchTransport = client._transport();
        elasticsearchTransport.close();
    }

```

I used to think it will be automatically closed. But it doesn`t. And the way to close a connection is hard to find. So, could it be possible that Java Client API offers a simpler method to close a ElasticsearchClient just like Java Rest High Level Client API.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 15, 2022, 6:03am UTC](https://discuss.elastic.co/t/out-of-memorry-error-when-using-java-client-api/297038/5 "2022-03-15T06:03:33Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
