Effectiveness and optimization in Java

12 April 2018 news

Optimization is one of the most interesting processes in software development. Every time your application needs performance improvement, you apply optimization.

What is optimization? Optimization is generally reducing resources usage. Which resources are applicable here?

  • CPU utilization
  • Memory usage(stack, heap, native memory)
  • Network utilization
  • I/O utilization (disk systems, etc)

When people talk about optimization, they most often mean CPU and memory usage. How can we achieve that? From the programming point of view, there are few well-known approaches:

  • Choose efficient algorithms and data structures
  • Reuse/cache objects and variables
  • Prefer primitive data types in favor of object data types
  • Use native libraries/components
  • Direct memory access

However before you start your optimization process you should be aware that compiler and JVM perform most of the optimization routines implicitly. How can compiler affect optimization? It can throw away dead code, make constant folding and fixed expression evaluation.

However, the real genius of optimization is JVM. Hotspot JVM together with JIT compiler can reach similar functionality comparing to low-level languages (for example, C).  The wide range of supported improvements includes:

  • Methods inline
  • Eliminate locks
  • Replace interface with direct method calls
  • Join synchronized blocks
  • Eliminate dead code
  • Drop memory write for non-volatile variables

I can summarize in conclusion that optimization is heuristic multi-step process that requires a lot of knowledge and experience from developer. You should have ultimate algorithmic skills in, know JVM and JDK internals and measure your performance changes.

 

Article written by:

Sergey Morenets – Java Evangelist

 

Partners