title: Why does select() consume so much CPU time in my program?
body: <p>I have several Java applications that use MINA, and all of them use 20 MINA threads. One application serves around 10,000 concurrent connections, that are usually idle but receive input sometimes. 20 is likely a reasonable threadcount for that application, although I haven't exactly profiled it (which this question is getting at).  Another application serves only around 15 connections at a time but initiates the IO work so they are very busy, and has 20 MINA threads anyway, which is obviously too many.</p>
  
  <p>The thing that's strange to me is both applications always devote about 30%, sometimes as high as 60%, of their CPU time into MINA's select() method, profiled in VisualVM.  The call stack looks like this:</p>
  
  <pre><code>java.lang.Thread.State: RUNNABLE
  at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
  at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:228)
  at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:81)
  at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
  - locked &lt;40ca5d54&gt; (a sun.nio.ch.Util$2)
  - locked &lt;24649fe8&gt; (a java.util.Collections$UnmodifiableSet)
  - locked &lt;3fae9662&gt; (a sun.nio.ch.EPollSelectorImpl)
  at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
  at org.apache.mina.transport.socket.nio.NioProcessor.select(NioProcessor.java:72)
  at org.apache.mina.core.polling.AbstractPollingIoProcessor$Processor.run(AbstractPollingIoProcessor.java:1093)
  at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
  at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>It seems to be based in a busy poll, which sounds really wrong to me.</p>
  
  <p>Should I be worried when I see the number that high?  What causes this?  Is it something I need to optimize out or is it more akin to a sleep or idle routine?  If it's more like a sleep routine is it somehow scheduled to be lower priority than other CPU work?</p>
  
  <p>Update:  <a href="https://stackoverflow.com/a/13266063/1339987">this thread</a> seems to be the same issue. I followed its advice, and am now running Java 1.7.0_45, but I am still seeing <code>select</code> taking as high as 90% of CPU time in an application with 10k connections.</p>
  
  <p>We are using MINA 2.0.4, which means <a href="https://issues.apache.org/jira/browse/DIRMINA-678" rel="noreferrer">this relevant bug</a> is fixed.</p>