# java.io.IOException: Too many open files

**URL:** <https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080>\
**Category:** Elasticsearch\
**Created:** [February 22, 2017, 4:38pm UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080 "2017-02-22T16:38:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lior\_Abramov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lior_abramov/32/15778_2.png) [@Lior\_Abramov](https://discuss.elastic.co/u/Lior_Abramov)\
**Post date:** [February 22, 2017, 4:38pm UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/1 "2017-02-22T16:38:47Z")

</div>

Hi all,  
I am using in elasticsearch and I am getting this error "java.io.IOException: Too many open files"  
I'm running the following command: sudo lsof | grep java | grep mpidcmgr | wc -l,the result increased after any query to elasticsearch.  
Why the number of files increases and not decreases?  
What's my problem? Do you have a solution to this problem?

Full exciption :  
java.io.IOException: Too many open files  
at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method) ~[na:1.8.0\_77]  
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422) ~[na:1.8.0\_77]  
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250) ~[na:1.8.0\_77]  
at org.jboss.netty.channel.socket.nio.NioServerBoss.process(NioServerBoss.java:100) [io.netty.netty-3.10.6.Final.jar:na]  
at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337) [io.netty.netty-3.10.6.Final.jar:na]  
at org.jboss.netty.channel.socket.nio.NioServerBoss.run(NioServerBoss.java:42) [io.netty.netty-3.10.6.Final.jar:na]  
at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) [io.netty.netty-3.10.6.Final.jar:na]  
at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42) [io.netty.netty-3.10.6.Final.jar:na]  
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0\_77]  
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0\_77]  
at java.lang.Thread.run(Thread.java:745) [na:1.8.0\_77]  
Add Comment Collaps

---

<div class="post-metadata">

**Author:** ![jkuang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jkuang/32/72637_2.png) [@jkuang](https://discuss.elastic.co/u/jkuang)\
**Post date:** [February 22, 2017, 8:46pm UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/2 "2017-02-22T20:46:58Z")

</div>

The file descriptor threads are increased because the data might be stored on multiple segments / files. So in order to read / write to these segments they must be opened. I would make sure the ulimit for the user running the ES process has at least 65k for open files and max user processes.

---

<div class="post-metadata">

**Author:** ![Lior\_Abramov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lior_abramov/32/15778_2.png) [@Lior\_Abramov](https://discuss.elastic.co/u/Lior_Abramov)\
**Post date:** [February 23, 2017, 9:14am UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/3 "2017-02-23T09:14:01Z")

</div>

> [@jkuang](#):
>
> The file descriptor threads are increased because the data might be stored on multiple segments / files. So in order to read / write to these segments they must be opened. I would make sure the ulimit for the user running the ES process has at least 65k for open files and max user processes.

Where checking?  
And how to increase?

---

<div class="post-metadata">

**Author:** ![Lior\_Abramov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lior_abramov/32/15778_2.png) [@Lior\_Abramov](https://discuss.elastic.co/u/Lior_Abramov)\
**Post date:** [February 23, 2017, 12:17pm UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/4 "2017-02-23T12:17:46Z")

</div>

Hello again,  
I found the following guide: [Http://queirozf.com/entries/elasticsearch-too-many-open-files](Http://queirozf.com/entries/elasticsearch-too-many-open-files) , and I did exactly what they proposed.

Limit of the number of files has increased but after stress test ( Jmeter) I got the error again.  
before the change I got the error after 2000 open files and now after 10000 open files.

I don't want to crash ...  
How often open files are closed?  
What is the trigger?  
Do you have a solution to this problem?

---

<div class="post-metadata">

**Author:** ![jkuang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jkuang/32/72637_2.png) [@jkuang](https://discuss.elastic.co/u/jkuang)\
**Post date:** [February 23, 2017, 5:00pm UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/5 "2017-02-23T17:00:39Z")

</div>

How often open files are closed?  
Open files are closed when the application / process that needed the thread finishes.

What is the trigger?  
I do not know the trigger without heavily debugging this. However, if you say this happens when you run your queries in ES with Jmeter as the benchmarking application I'm leaning toward the benchmarking load it is doing. I would like to know the level of benchmarking you are doing. How many queries you are running and how many indices the query is hitting.

Do you have a solution to this problem?  
Once we find the trigger then I can propose a solution. I would cut the benchmarking load to 1/10 and see if you still get the same problem and slowly increasing it.

---

<div class="post-metadata">

**Author:** ![Lior\_Abramov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lior_abramov/32/15778_2.png) [@Lior\_Abramov](https://discuss.elastic.co/u/Lior_Abramov)\
**Post date:** [February 26, 2017, 10:03am UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/6 "2017-02-26T10:03:19Z")

</div>

Is it necessary to close the connection after each query?

this.client = new PreBuiltXPackTransportClient(settings)  
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(elasticSearchHost), elasticSearchPort));

and

```
   this.client.close();
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 26, 2017, 10:23am UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/7 "2017-02-26T10:23:45Z")

</div>

No. Just close the client when your application stops.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 26, 2017, 10:25am UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/8 "2017-02-26T10:25:31Z")

</div>

BTW use a singleton for the client.

---

<div class="post-metadata">

**Author:** ![Lior\_Abramov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lior_abramov/32/15778_2.png) [@Lior\_Abramov](https://discuss.elastic.co/u/Lior_Abramov)\
**Post date:** [February 26, 2017, 12:22pm UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/9 "2017-02-26T12:22:15Z")

</div>

Ok, thanks.  
But I still have a problem with the growth of the number of files.  
Why it causes to my server getting stuck?

I tried to add limitations like the following:

The following code works and returns an answer but without limitations "  
Settings settings = Settings.builder().put("[cluster.name](http://cluster.name)", clusterName)  
.put("xpack.security.user", user)  
.put("xpack.security.transport.ssl.enabled", true)  
.put("request.headers.X-Found-Cluster", "${[cluster.name](http://cluster.name)}").build()

The following code returns the following error but with limitations:

```
  Settings settings = Settings.builder().put("cluster.name", clusterName)
            .put("xpack.security.user", user)
            .put("xpack.security.transport.ssl.enabled", true)
            .put("request.headers.X-Found-Cluster", "${cluster.name}")
            .put("client.transport.sniff", true)
            .put("thread_pool.search.size", 8)
            .put("thread_pool.search.queue_size", 100)
            .build();

```

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{i3fGwgU7QNm60-yTrpFtIQ}{35.163.210.174}{35.163.210.174:9300}]]

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 26, 2017, 6:53pm UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/10 "2017-02-26T18:53:39Z")

</div>

Hard to say. May be share a code example?

Please format your code using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will make your post more readable.

Or use markdown style like:

````
```
CODE
```
````

---

<div class="post-metadata">

**Author:** ![Lior\_Abramov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lior_abramov/32/15778_2.png) [@Lior\_Abramov](https://discuss.elastic.co/u/Lior_Abramov)\
**Post date:** [February 27, 2017, 10:05am UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/11 "2017-02-27T10:05:54Z")

</div>

I FOUND IT,  
THANKS.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 27, 2017, 11:38am UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/12 "2017-02-27T11:38:47Z")

</div>

Yeah. You never initialized your elasticsearchConnectionProvider object.  
So you were building everytime a new instance.

Please don't remove your code as it can help others.  
It's better to see what you did to fix it.

---

<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 27, 2017, 11:39am UTC](https://discuss.elastic.co/t/java-io-ioexception-too-many-open-files/76080/13 "2017-03-27T11:39:09Z")

</div>

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