Is Java performance slow? Tips to increase speed

by
30th Aug 2000
Share this content

Java has become one of the most popular languages for software development especially on the Internet. When compared with C++, it has several advantages such as:

  • It can run on multiple platforms
  • It is internet-enabled
  • It has built-in security
  • Less prone to bugs
  • Ability to run anywhere
  • Immunity from memory leaks

However, Java applications still appeared to run much more slowly than C++ applications. This created disadvantages when creating large network applications.

Java's main advantages are the reasons for its lack of speed as a Java program is compiled to bytecode rather than native code. Bytecode is platform independent and has to be interpreted by a Java virtual machine (VM). When running a C++ program, this only involves executing the native code.

As Java code needs translation from bytecode to native code and then executing that code, it may not be as efficient, as a single bytecode instruction can require many processor operations, thus making it equivalent to several compiled C++ instructions.

Also, a bytecode instruction has to be interpreted again every time it runs even if the VM has already previously interpreted it.

Java's Abstract Windowing Toolkit (AWT) acts as a mediating layer between a Java program and the windowing platform which also produces performance consequences.

Java allocates memory only when a program is run as a security measure, unlike C++. The bytecode is then checked by the VM's verifier which will not allow anything that may violate Java's security and language rules. When objects that have been created by the program are no longer required, they are destroyed by the garbage collector which eliminates the memory leaks which C++ applications can experience.

This is why it can take up to twice the length of time Java takes in comparison to C++ because of the amount of CPU time and resources it takes to allocating and freeing memory.

Here are some steps that can be taken in increase Java speed.

Coding for speed

  • Synchronised methods take ten times longer to interpret as unsynchronised ones.
  • Re-use objects
  • Buffer input and output
  • Avoid concatenated strings

Try using Java's built-in multithreading support where possible.

Compiling for speed

  • Increase speed by 15% by using O option when compiling with Javac
  • At compile time translate Java files directly into native code rather than bytecode, several compilers are available that do this.

A disadvantage with this is that it makes Java programs code platform specific.

Faster virtual machines

  • Use a just-in-time (JIT) Compiler instead of a standard Java VM
  • The Microsystems Java HotSpot performance engine is a VM that can double the speed of Java applications.

Embedded Java

  • Most radical is to run on dedicated Java processors. Sun Microsystems picoJava chips execute bytecode, have built-in support for threads and for garbage collection. This make it possible to run Java on any hardware device, from cell phones to network appliances.

Owing to these developments, Java can now run almost as fast as C++, making it a better option not only for smaller applets, but also for larger applications.

Tags:

Replies (0)

Please login or register to join the discussion.

There are currently no replies, be the first to post a reply.