# Are there memory leaks in ElasticSearch's TransportClient 5.4.3 or is my code flawed?

**URL:** <https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989>\
**Category:** Elasticsearch\
**Created:** [July 5, 2017, 9:51pm UTC](https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989 "2017-07-05T21:51:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan-K](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Dan-K](https://discuss.elastic.co/u/Dan-K)\
**Post date:** [July 5, 2017, 9:51pm UTC](https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989/1 "2017-07-05T21:51:28Z")

</div>

Are there memory leaks in ElasticSearch's TransportClient 5.4.3? Or am I not using and closing the objects correctly?

I'm using the client in a Jenkins (2.68) which is on JRE 1.8.0\_66 and writing my code in Groovy. `BulkRequestBuilder` is being used to submit multiple records.

Throughout the code, these 4x 16 MB byte arrays appear in memory but never get closed even past the TransportClient.close() method:  
[![4x 16 MB byte arrays](https://i.stack.imgur.com/rHgMo.png)](https://i.stack.imgur.com/rHgMo.png)  
If I step through the code more slowly, I can get up to 9x of these 16 MB bytes arrays, some of which look very similar:  
[![7x 16MB byte arrays](https://i.stack.imgur.com/qrGYQ.png)](https://i.stack.imgur.com/qrGYQ.png)

I believes I'm following the official documentation fully: [https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html)

Here's a trimmed down version of the code I'm using:

```
@Grapes([
    @Grab(group = "org.apache.logging.log4j", module = "log4j-api", version = "2.8.2", initClass = true),
    @Grab(group = "org.apache.logging.log4j", module = "log4j-core", version = "2.8.2", initClass = true),
    @Grab(group = "org.elasticsearch.client", module = "transport", version = "5.4.3", initClass = true)
])
public class ElasticSearchReport implements Serializable {

    pubblic void execute() {
        // Data to report.
        List<Map<String, Object>> data = ...

        // ElasticSearch settings. Do not use sniffing because we want to upload data to the master node.
        Settings settings = Settings.builder()
            .put("cluster.name", "my-cluster")
            .put("client.transport.sniff", false)
            .build()

        PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(settings)
        TransportClient client = preBuiltTransportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("my-elastic-search-host"), 9300))
        List<String> errors = new LinkedList<String>()

        try {
            BulkRequestBuilder bulkRequest = client.prepareBulk()

            // Build the bulk query with each data entry.
            data.each({entry ->
                String serialisedEntry = SerializationUtils.toJson(entry)
                bulkRequest.add(client.prepareIndex("my-index", "my-type").setSource(serialisedEntry, XContentType.JSON))
            })

            // Process the response.
            bulkRequest.get().getItems().each({response ->
                if (response.failed) {
                    errors.add(response.failure.message)
                    return
                }

                final String statusName = response.status().name()

                // Check statusName ...
            })

            if (errors) throw new Exception(...)
        } finally {
            client.close()
            preBuiltTransportClient.close()
        }
    }
}

```

From the objects I'm using, only `PreBuiltTransportClient` and `TransportClient` implement `Closeable` so I'm trying to explicitly close them in my `finally` block.

Tried using `.withCloseable()` (Groovy's equivalent to `try-with-resources`) but it did not prevent the issue from happening.

I thought perhaps my `bulkRequest.get().getItems().each({response ->` line was doing something funny so I replaced it with the following but to no effect:

```
BulkResponse bulkResponse = bulkRequest.get()
BulkItemResponse[] bulkItemResponses = bulkResponse.getItems()
for (int responseIndex = 0; responseIndex < bulkItemResponses.length; responseIndex++) { ... }

```

I've also tried replacing `BulkResponse bulkResponse = bulkRequest.get()` with `BulkResponse bulkResponse = bulkRequest.execute().actionGet()` with no luck.

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [July 6, 2017, 10:03am UTC](https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989/2 "2017-07-06T10:03:39Z")

</div>

Instead of

> [@Dan-K](#):
>
> BulkResponse bulkResponse = bulkRequest.get()

try

```
BulkResponse bulkResponse = bulkRequest.execute().actionGet()

```

---

<div class="post-metadata">

**Author:** ![Dan-K](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Dan-K](https://discuss.elastic.co/u/Dan-K)\
**Post date:** [July 6, 2017, 6:04pm UTC](https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989/3 "2017-07-06T18:04:20Z")

</div>

Thanks for suggestion Jörg.  
Tried it out but I still get the same result.

---

<div class="post-metadata">

**Author:** ![Dan-K](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Dan-K](https://discuss.elastic.co/u/Dan-K)\
**Post date:** [July 12, 2017, 5:03pm UTC](https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989/4 "2017-07-12T17:03:05Z")

</div>

The [ElasticSearch Java REST Client](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/index.html) doesn't appear to have these issues.  
I've switched over to it and haven't experienced out-of-memory errors. All relatively large byte arrays are released once the client is closed and scoped out.

---

<div class="post-metadata">

**Author:** ![Thiago\_Sayao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago_sayao/32/20647_2.png) [@Thiago\_Sayao](https://discuss.elastic.co/u/Thiago_Sayao)\
**Post date:** [July 31, 2017, 7:48pm UTC](https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989/5 "2017-07-31T19:48:41Z")

</div>

I have a similar code and it is also leaking. I am using 5.5.1.

It seems to be leaking byte arrays:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/d/fd4330f224f98ceb0e6da91aa20987f36d204905.png)

The code looks like this:

```
try(PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(settings)) {
    try(TransportClient transportClient = preBuiltTransportClient.addTransportAddress(
            new InetSocketTransportAddress(InetAddress.getByName(endPoint), javaPort))) {
    
        populateIndex(transportClient, newIndexName, produtoService);
        reassignAlias(transportClient, newIndexName);
        cleanIndices(transportClient, newIndexName);
    }
    catch(IOException e) {
        logger.error(e.getMessage(), e);
    }
}
```

---

<div class="post-metadata">

**Author:** ![Thiago\_Sayao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago_sayao/32/20647_2.png) [@Thiago\_Sayao](https://discuss.elastic.co/u/Thiago_Sayao)\
**Post date:** [August 3, 2017, 11:42am UTC](https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989/6 "2017-08-03T11:42:36Z")

</div>

Dan,

Are you using spring-boot together with elasticsearch?

---

<div class="post-metadata">

**Author:** ![Dan-K](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@Dan-K](https://discuss.elastic.co/u/Dan-K)\
**Post date:** [August 3, 2017, 5:01pm UTC](https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989/7 "2017-08-03T17:01:06Z")

</div>

Hey @Thiago_Sayao,

No, I'm only using it in two ways:

1. Running tests with maven-surefire-plugin / maven-failsafe-plugin
2. Running [Jenkins Pipeline Shared-Library](https://jenkins.io/doc/book/pipeline/shared-libraries/) jobs

---

<div class="post-metadata">

**Author:** ![Thiago\_Sayao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago_sayao/32/20647_2.png) [@Thiago\_Sayao](https://discuss.elastic.co/u/Thiago_Sayao)\
**Post date:** [August 4, 2017, 1:43pm UTC](https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989/8 "2017-08-04T13:43:10Z")

</div>

@Dan-K

Take a look on [https://github.com/elastic/elasticsearch/issues/26048](https://github.com/elastic/elasticsearch/issues/26048), the solution is there.

---

<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:** [September 1, 2017, 1:43pm UTC](https://discuss.elastic.co/t/are-there-memory-leaks-in-elasticsearchs-transportclient-5-4-3-or-is-my-code-flawed/91989/9 "2017-09-01T13:43:28Z")

</div>

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