Wednesday, October 05, 2011

How to get C like performance in Java


  • The JVM does implicit bounds checking on array access and updates. This has a small overhead - you can unsafely eliminate this (and open yourself to buffer overflows and other problems) using the the Unsafe class or direct buffers.
  • Use memory-minimized collections to reduce memory usage.
  • You can use Direct memory to store data how you wish (this is what BigMemory uses).
  • Use blocking IO in NIO (which is the default for a Channel) - don't use Selectors unless you need them.
  • Most systems can handle 1K-10K threads efficiently. Scalability beyond 10K users/server doesn't buy you anything in the real world since the server resources will be consumed servicing 10k concurrent users.
  • -XX:+UseCompressedStrings use byte[] instead of char[] for strings which don't need 16-bit characters - this saves memory but is 5%-10% slower.
  • To reduce string space usage, you can use your own Text type which wraps a byte[], or get your text data from ByteBuffer, CharBuffer or use Unsafe or -XX:+UseCompressedStrings.
  • To start the JVM faster, load fewer libraries.
  • Use primitives instead of primitive wrapper objects.

No comments: