Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java in 2011: threaded sockets VS NIO: what to choose on 64bit OS and latest Java version?

I've read several posts about java.net vs java.nio here on StackOverflow and on some blogs. But I still cannot catch an idea of when should one prefer NIO over threaded sockets. Can you please examine my conclusions below and tell me which ones are incorrect and which ones are missed?

  • Since in threaded model you need to dedicate a thread to each active connection and each thread takes like 250Kilobytes of memory for it's stack, with thread per socket model you will quickly run out of memory on large number of concurrent connections. Unlike NIO.

  • In modern operating systems and processors a large number of active threads and context switch time can be considered almost insignificant for performance

  • NIO throughoutput can be lower because select() and poll() used by asynchronous NIO libraries in high-load environments is more expensive than waking up and putting to sleep threads.

  • NIO has always been slower but it allows you to process more concurrent connections. It's essentially a time/space trade-off: traditional IO is faster but has a heavier memory footprint, NIO is slower but uses less resources.

  • Java has a hard limit per concurrent threads of 15000 / 30000 depending on JVM and this will limit thread per connection model to this number of concurrent connections maximum, but JVM7 will have no such limit (cannot confirm this data).

So, as a conclusion, you can have this:

  • If you have tens of thousands concurrent connections - NIO is a better choice unless a request processing speed is a key factor for you
  • If you have less than that - thread per connection is a better choice (given that you can afford amount of RAM to hold stacks of all concurrent threads up to maximum)
  • With Java 7 you may want to go over NIO 2.0 in either case.

Am I correct?

like image 942
Vladislav Rastrusny Avatar asked Mar 25 '11 20:03

Vladislav Rastrusny


2 Answers

That seems right to me, except for the part about Java limiting the number of threads – that is typically limited by the OS it's running on (see How many threads can a Java VM support? and Can't get past 2542 Threads in Java on 4GB iMac OSX 10.6.3 Snow Leopard (32bit)).

To reach that many threads you'll probably need to adjust the stack size of the JVM.

like image 178
Adam Bryzak Avatar answered Sep 27 '22 17:09

Adam Bryzak


I still think the context switch overhead for the threads in traditional IO is significant. At a high level, you only gain performance using multiple threads if they won't contend for the same resources as much, or they spend time much higher than the context switch overhead on the resources. The reason for bringing this up, is with new storage technologies like SSD, your threads come back to contend on the CPU much quicker

like image 29
byte_array Avatar answered Sep 27 '22 18:09

byte_array