# ElasticSearch .NET Slow System.IO.MemoryStream:set\_Capacity

**URL:** <https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974>\
**Category:** Elasticsearch\
**Created:** [July 24, 2019, 7:49am UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974 "2019-07-24T07:49:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajee\_siapno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ajee_siapno/32/48587_2.png) [@ajee\_siapno](https://discuss.elastic.co/u/ajee_siapno)\
**Post date:** [July 24, 2019, 7:49am UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974/1 "2019-07-24T07:49:18Z")

</div>

Hi,

Have anyone using ElasticSearch 6.3 and [ElasticSearch.NET](http://ElasticSearch.NET) and NEST libraries have experienced slow code on the following stack trace (with response time)

```
Nest.ElasticClient:Search: Unknown (execution time: 0 ms of 9000 ms total)
                          Nest.ElasticClient:Nest.IHighLevelToLowLevelDispatcher.Dispatch: Unknown (execution time: 0 ms of 9000 ms total)
                            Nest.LowLevelDispatch:SearchDispatch: Unknown (execution time: 0 ms of 9000 ms total)
                              Elasticsearch.Net.Transport`1:Request: Unknown (execution time: 0 ms of 9000 ms total)
                                Elasticsearch.Net.RequestPipeline:CallElasticsearch: Unknown (execution time: 0 ms of 9000 ms total)
                                  Elasticsearch.Net.HttpConnection:Request: Unknown (execution time: 0 ms of 9000 ms total)
                                    Elasticsearch.Net.ResponseBuilder:ToResponse: Unknown (execution time: 0 ms of 9000 ms total)
                                      Elasticsearch.Net.ResponseBuilder:SetBody: Unknown (execution time: 0 ms of 9000 ms total)
                                        System.IO.Stream:InternalCopyTo: Unknown (execution time: 0 ms of 8984 ms total)
                                          System.IO.MemoryStream:Write: Unknown (execution time: 0 ms of 8984 ms total)
                                            System.IO.MemoryStream:EnsureCapacity: Unknown (execution time: 0 ms of 8984 ms total)
                                              System.IO.MemoryStream:set_Capacity: Unknown (execution time: 8984 ms of 8984 ms total)

```

Does it have to do with the amount data returned from the search request?

Thanks,  
Ajee

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [July 24, 2019, 1:36pm UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974/2 "2019-07-24T13:36:13Z")

</div>

Looks to be spending a fair amount of time in ensuring sufficient capacity in a byte buffer backed `MemoryStream`. What version of NEST and [Elasticsearch.Net](http://Elasticsearch.Net) are you using?

---

<div class="post-metadata">

**Author:** ![ajee\_siapno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ajee_siapno/32/48587_2.png) [@ajee\_siapno](https://discuss.elastic.co/u/ajee_siapno)\
**Post date:** [July 24, 2019, 6:21pm UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974/3 "2019-07-24T18:21:30Z")

</div>

We used these nugets to get the libraries.  
[Elasticsearch.Net 6.3](https://www.nuget.org/packages/Elasticsearch.Net/6.3.0)  
[NEST 6.3](https://www.nuget.org/packages/NEST/6.3.0)

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [July 24, 2019, 9:31pm UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974/4 "2019-07-24T21:31:47Z")

</div>

Can you update to latest 6.x NEST and [Elasticsearch.Net](http://Elasticsearch.Net), [which is 6.8.1 currently](https://www.nuget.org/packages/NEST/6.8.1), and see if the problem persists? If it does, you'll need to provide more information around what you're doing in the client, and how you're capturing timings.

---

<div class="post-metadata">

**Author:** ![ajee\_siapno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ajee_siapno/32/48587_2.png) [@ajee\_siapno](https://discuss.elastic.co/u/ajee_siapno)\
**Post date:** [July 25, 2019, 4:15am UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974/5 "2019-07-25T04:15:53Z")

</div>

Hi forloop,

Unfortunately, upgrade is not in the options. We need to figure out what's causing this its current configuration.

We have a Application Performance Management tool that captures code execution and run time. The stack trace (with response time) was the result of a previous performance test.

I should have noted earlier that the scenario is on a web application with high concurrent users. Could the concurrent requests be having problems with run time memory allocation?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [July 26, 2019, 4:01am UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974/6 "2019-07-26T04:01:03Z")

</div>

> [@ajee\_siapno](#):
>
> Unfortunately, upgrade is not in the options. We need to figure out what's causing this its current configuration.

I mean upgrade the client libraries to 6.8.1; you don't need to update Elasticsearch too, the 6.8.1 client libraries are compatible with Elasticsearch 6.3.0. It is worth doing this to rule out a potential bug in the client that may now be fixed in a later release.

---

<div class="post-metadata">

**Author:** ![ajee\_siapno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ajee_siapno/32/48587_2.png) [@ajee\_siapno](https://discuss.elastic.co/u/ajee_siapno)\
**Post date:** [July 27, 2019, 9:18am UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974/7 "2019-07-27T09:18:12Z")

</div>

I guess we can give that a try. Other than that, can we do anything else to pin point the problem?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [July 29, 2019, 1:21am UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974/8 "2019-07-29T01:21:43Z")

</div>

We would need to know much more information about exactly what queries are being sent, how the client is configured, how the APM tool is capturing code execution, etc., to be able to understand if this is a real issue in the client. There could be usage patterns such as returning very large number of results in a single response, combined with `.DisableDirectStreaming()`, that would require buffering in memory and large numbers of byte array allocations that could manifest in a significant amount of time in `MemoryStream` methods. Whether this is a real issue will however be context dependent.

First though, I'd recommend updating the client to the latest 6.x before going down this route as you may end up spending time investigating an issue that no longer exists in a later version.

---

<div class="post-metadata">

**Author:** ![ajee\_siapno](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ajee_siapno/32/48587_2.png) [@ajee\_siapno](https://discuss.elastic.co/u/ajee_siapno)\
**Post date:** [July 29, 2019, 3:22pm UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974/9 "2019-07-29T15:22:17Z")

</div>

You may have hit it right there.  
We do have a scenario where there is a quite large number of results using a client connection with .DisableDirectStreaming().

From ElasticSearch [Guide](https://www.elastic.co/guide/en/elasticsearch/client/net-api/6.x/configuration-options.html) it says:

`DisableDirectStreaming`

Ensures the response bytes are always available on the `ElasticsearchResponse<T>`

![Important](https://us1.discourse-cdn.com/elastic/original/3X/1/6/1651c28dfc2a5f31f5beed96ecdce2f72815d897.png)

Depending on the registered serializer, this may cause the response to be buffered in memory first, potentially affecting performance.

We are using ISearchResponse but we did not change any serializer.

Let me get back to you with the test result using the latest library.

---

<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:** [August 26, 2019, 3:31pm UTC](https://discuss.elastic.co/t/elasticsearch-net-slow-system-io-memorystream-set-capacity/191974/10 "2019-08-26T15:31:27Z")

</div>

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