java performance

{{Short description|Aspect of Java programming language}}

{{Update|reason=Is missing the many improvements in Java 8, 11, 17, 21, ... |date=November 2023}}

{{Use mdy dates|date=October 2018}}

In software development, the programming language Java was historically considered slower than the fastest third-generation typed languages such as C and C++.{{Cite web|url=http://www.scribblethink.org/Computer/javaCbenchmark.html|title=Java versus C++ benchmarks}} In contrast to those languages, Java compiles by default to a Java Virtual Machine (JVM) with operations distinct from those of the actual computer hardware. Early JVM implementations were interpreters; they simulated the virtual operations one-by-one rather than translating them into machine code for direct hardware execution.

Since the late 1990s, the execution speed of Java programs improved significantly via introduction of just-in-time compilation (JIT) (in 1997 for Java 1.1),{{Cite web

| url=http://www.symantec.com/about/news/release/article.jsp?prid=19970407_03

| archive-url=https://web.archive.org/web/20100628171748/http://www.symantec.com/about/news/release/article.jsp?prid=19970407_03

| url-status=dead

| archive-date=June 28, 2010

| title=Symantec's Just-In-Time Java Compiler To Be Integrated Into Sun JDK 1.1

}}{{cite web

| url=http://grnlight.net/index.php/programming-articles/116-java-gets-four-times-faster-with-new-symantec-just-in-time-compiler

| archive-url=https://archive.today/20140527181040/http://grnlight.net/index.php/programming-articles/116-java-gets-four-times-faster-with-new-symantec-just-in-time-compiler

| url-status=usurped

| archive-date=May 27, 2014

| title=Java gets four times faster with new Symantec just-in-time compiler}} the addition of language features supporting better code analysis, and optimizations in the JVM (such as HotSpot becoming the default for Sun's JVM in 2000). Sophisticated garbage collection strategies were also an area of improvement. Hardware execution of Java bytecode, such as that offered by ARM's Jazelle, was explored but not deployed.

The performance of a Java bytecode compiled Java program depends on how optimally its given tasks are managed by the host Java virtual machine (JVM), and how well the JVM exploits the features of the computer hardware and operating system (OS) in doing so. Thus, any Java performance test or comparison has to always report the version, vendor, OS and hardware architecture of the used JVM. In a similar manner, the performance of the equivalent natively compiled program will depend on the quality of its generated machine code, so the test or comparison also has to report the name, version and vendor of the used compiler, and its activated compiler optimization directives.

Virtual machine optimization methods

Many optimizations have improved the performance of the JVM over time. However, although Java was often the first virtual machine to implement them successfully, they have often been used in other similar platforms as well.

=Just-in-time compiling=

{{Further|Just-in-time compilation|HotSpot (virtual machine)}}

Early JVMs always interpreted Java bytecodes. This had a large performance penalty of between a factor 10 and 20 for Java versus C in average applications.{{cite web | url=http://www.shudo.net/jit/perf/ | title=Performance Comparison of Java/.NET Runtimes (Oct 2004) }} To combat this, a just-in-time (JIT) compiler was introduced into Java 1.1. Due to the high cost of compiling, an added system called HotSpot was introduced in Java 1.2 and was made the default in Java 1.3. Using this framework, the Java virtual machine continually analyses program performance for hot spots which are executed frequently or repeatedly. These are then targeted for optimizing, leading to high performance execution with a minimum of overhead for less performance-critical code.

{{Cite web

| url=https://weblogs.java.net/blog/kohsuke/archive/2008/03/deep_dive_into.html

| title=Deep dive into assembly code from Java

| last=Kawaguchi

| first=Kohsuke

| date=March 30, 2008

| access-date=April 2, 2008

| archive-url=https://web.archive.org/web/20080402034758/http://weblogs.java.net/blog/kohsuke/archive/2008/03/deep_dive_into.html

| archive-date=April 2, 2008

| url-status=dead

| df=mdy-all

}}

{{Cite web

| url=http://ei.cs.vt.edu/~cs5314/presentations/Group2PLDI.pdf

| title=Fast, Effective Code Generation in a Just-In-Time Java Compiler

| publisher=Intel Corporation

| access-date=June 22, 2007}}

Some benchmarks show a 10-fold speed gain by this means.This [http://www.shudo.net/jit/perf/ article] shows that the performance gain between interpreted mode and Hotspot amounts to more than a factor of 10. However, due to time constraints, the compiler cannot fully optimize the program, and thus the resulting program is slower than native code alternatives.[http://www.itu.dk/~sestoft/papers/numericperformance.pdf Numeric performance in C, C# and Java ][http://www.cherrystonesoftware.com/doc/AlgorithmicPerformance.pdf Algorithmic Performance Comparison Between C, C++, Java and C# Programming Languages] {{webarchive|url=https://web.archive.org/web/20100331155325/http://www.cherrystonesoftware.com/doc/AlgorithmicPerformance.pdf |date=March 31, 2010 }}

=Adaptive optimizing=

{{Further|Adaptive optimization}}

Adaptive optimizing is a method in computer science that performs dynamic recompilation of parts of a program based on the current execution profile. With a simple implementation, an adaptive optimizer may simply make a trade-off between just-in-time compiling and interpreting instructions. At another level, adaptive optimizing may exploit local data conditions to optimize away branches and use inline expansion.

A Java virtual machine like HotSpot can also deoptimize code formerly JITed. This allows performing aggressive (and potentially unsafe) optimizations, while still being able to later deoptimize the code and fall back to a safe path.{{Cite web

| url=http://java.sun.com/products/hotspot/docs/whitepaper/Java_Hotspot_v1.4.1/Java_HSpot_WP_v1.4.1_1002_4.html#hotspot

| title=The Java HotSpot Virtual Machine, v1.4.1

| publisher=Sun Microsystems

| access-date=April 20, 2008}}{{Cite web

| url=http://headius.blogspot.com/2008/01/langnet-2008-day-1-thoughts.html

| title=Lang.NET 2008: Day 1 Thoughts

| quote=Deoptimization is very exciting when dealing with performance concerns, since it means you can make much more aggressive optimizations...knowing you'll be able to fall back on a tried and true safe path later on

| last=Nutter|first=Charles

| date=January 28, 2008

| access-date=January 18, 2011}}

=Garbage collection=

{{Further|Garbage collection (computer science)}}

The 1.0 and 1.1 Java virtual machines (JVMs) used a mark-sweep collector, which could fragment the heap after a garbage collection.

Starting with Java 1.2, the JVMs changed to a generational collector, which has a much better defragmentation behaviour.[http://www-128.ibm.com/developerworks/library/j-jtp01274.html IBM DeveloperWorks Library]

Modern JVMs use a variety of methods that have further improved garbage collection performance.For example, the duration of pauses is less noticeable now. See for example this clone of Quake II written in Java: [http://bytonic.de/html/jake2.html Jake2].

=Other optimizing methods=

==Compressed Oops==

Compressed Oops allow Java 5.0+ to address up to 32 GB of heap with 32-bit references. Java does not support access to individual bytes, only objects which are 8-byte aligned by default. Because of this, the lowest 3 bits of a heap reference will always be 0. By lowering the resolution of 32-bit references to 8 byte blocks, the addressable space can be increased to 32 GB. This significantly reduces memory use compared to using 64-bit references as Java uses references much more than some languages like C++. Java 8 supports larger alignments such as 16-byte alignment to support up to 64 GB with 32-bit references.{{Citation needed|date=July 2019}}

==Split bytecode verification==

Before executing a class, the Sun JVM verifies its Java bytecodes (see bytecode verifier). This verification is performed lazily: classes' bytecodes are only loaded and verified when the specific class is loaded and prepared for use, and not at the beginning of the program. However, as the Java class libraries are also regular Java classes, they must also be loaded when they are used, which means that the start-up time of a Java program is often longer than for C++ programs, for example.

A method named split-time verification, first introduced in the Java Platform, Micro Edition (J2ME), is used in the JVM since Java version 6. It splits the verification of Java bytecode in two phases:{{cite web

|url = https://jdk.dev.java.net/verifier.html

|title = New Java SE 6 Feature: Type Checking Verifier

|publisher = Java.net

|access-date = January 18, 2011

}}{{dead link|date=November 2017 |bot=InternetArchiveBot |fix-attempted=yes }}

  • Design-time – when compiling a class from source to bytecode
  • Runtime – when loading a class.

In practice this method works by capturing knowledge that the Java compiler has of class flow and annotating the compiled method bytecodes with a synopsis of the class flow information. This does not make runtime verification appreciably less complex, but does allow some shortcuts.{{Citation needed|date=February 2010}}

==Escape analysis and lock coarsening==

{{Further|Lock (computer science)|Escape analysis}}

Java is able to manage multithreading at the language level. Multithreading allows programs to perform multiple processes concurrently, thus improving the performance for programs running on computer systems with multiple processors or cores. Also, a multithreaded application can remain responsive to input, even while performing long running tasks.

However, programs that use multithreading need to take extra care of objects shared between threads, locking access to shared methods or blocks when they are used by one of the threads. Locking a block or an object is a time-consuming operation due to the nature of the underlying operating system-level operation involved (see concurrency control and lock granularity).

As the Java library does not know which methods will be used by more than one thread, the standard library always locks blocks when needed in a multithreaded environment.

Before Java 6, the virtual machine always locked objects and blocks when asked to by the program, even if there was no risk of an object being modified by two different threads at once. For example, in this case, a local {{code|Vector}} was locked before each of the add operations to ensure that it would not be modified by other threads ({{code|Vector}} is synchronized), but because it is strictly local to the method this is needless:

public String getNames() {

final Vector v = new Vector<>();

v.add("Me");

v.add("You");

v.add("Her");

return v.toString();

}

Starting with Java 6, code blocks and objects are locked only when needed,{{cite web

|url=http://www-128.ibm.com/developerworks/java/library/j-jtp10185/

|title=Java theory and practice: Synchronization optimizations in Mustang

|publisher=IBM

|author=Brian Goetz

|date=October 18, 2005

|access-date=January 26, 2013}} so in the above case, the virtual machine would not lock the Vector object at all.

Since version 6u23, Java includes support for escape analysis.{{cite web

|url=http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html#escapeAnalysis

|title=Java HotSpot Virtual Machine Performance Enhancements

|publisher=Oracle Corporation

|quote=Escape analysis is a technique by which the Java Hotspot Server Compiler can analyze the scope of a new object's uses and decide whether to allocate it on the Java heap. Escape analysis is supported and enabled by default in Java SE 6u23 and later.

|access-date=January 14, 2014}}

==Register allocation improvements==

Before Java 6, allocation of registers was very primitive in the client virtual machine (they did not live across blocks), which was a problem in CPU designs which had fewer processor registers available, as in x86s. If there are no more registers available for an operation, the compiler must copy from register to memory (or memory to register), which takes time (registers are significantly faster to access). However, the server virtual machine used a color-graph allocator and did not have this problem.

An optimization of register allocation was introduced in Sun's JDK 6;[http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6320351 Bug report: new register allocator, fixed in Mustang (JDK 6) b59] it was then possible to use the same registers across blocks (when applicable), reducing accesses to the memory. This led to a reported performance gain of about 60% in some benchmarks.[http://weblogs.java.net/blog/2005/11/10/mustangs-hotspot-client-gets-58-faster Mustang's HotSpot Client gets 58% faster!] {{Webarchive|url=https://web.archive.org/web/20120305215143/http://weblogs.java.net/blog/2005/11/10/mustangs-hotspot-client-gets-58-faster |date=March 5, 2012 }} in Osvaldo Pinali Doederlein's Blog at java.net

==Class data sharing==

Class data sharing (called CDS by Sun) is a mechanism which reduces the startup time for Java applications, and also reduces memory footprint. When the JRE is installed, the installer loads a set of classes from the system JAR file (the JAR file holding all the Java class library, called rt.jar) into a private internal representation, and dumps that representation to a file, called a "shared archive". During subsequent JVM invocations, this shared archive is memory-mapped in, saving the cost of loading those classes and allowing much of the JVM's metadata for these classes to be shared among multiple JVM processes.[http://java.sun.com/j2se/1.5.0/docs/guide/vm/class-data-sharing.html Class Data Sharing] at java.sun.com

The corresponding improvement in start-up time is more obvious for small programs.[http://www.artima.com/forums/flat.jsp?forum=121&thread=56613 Class Data Sharing in JDK 1.5.0] in Java Buzz Forum

at [http://www.artima.com/ artima developer]

History of performance improvements

{{Update|section|date=April 2023|reason=The most recently mentioned version in this section, Java 7, is over a decade old; as of writing, Java 20 is the current version}}

{{Further|Java version history}}

Apart from the improvements listed here, each release of Java introduced many performance improvements in the JVM and Java application programming interface (API).

JDK 1.1.6: First just-in-time compilation (Symantec's JIT-compiler){{Cite web|last1=Mckay|first1=Niali|url=http://linxdigital.ca/java-four-times-faster-symantec-compiler.html|title=Java gets four times faster with new Symantec just-in-time compiler}}

J2SE 1.2: Use of a generational collector.

J2SE 1.3: Just-in-time compiling by HotSpot.

J2SE 1.4: See [http://java.sun.com/j2se/1.4.2/performance.guide.html here], for a Sun overview of performance improvements between 1.3 and 1.4 versions.

Java SE 5.0: Class data sharing[http://java.sun.com/performance/reference/whitepapers/5.0_performance.html Sun overview of performance improvements between 1.4 and 5.0 versions.]

Java SE 6:

Other improvements:

  • Java OpenGL Java 2D pipeline speed improvements[http://weblogs.java.net/blog/campbell/archive/2005/07/strcrazier_perf.html STR-Crazier: Performance Improvements in Mustang] {{webarchive|url=https://web.archive.org/web/20070105224757/http://weblogs.java.net/blog/campbell/archive/2005/07/strcrazier_perf.html |date=January 5, 2007 }} in Chris Campbell's Blog at java.net
  • Java 2D performance also improved significantly in Java 6See [https://web.archive.org/web/20070112101848/http://jroller.com/page/dgilbert?entry=is_java_se_1_6 here] for a benchmark showing a performance boost of about 60% from Java 5.0 to 6 for the application [http://www.jfree.org JFreeChart]

See also 'Sun overview of performance improvements between Java 5 and Java 6'.[http://java.sun.com/performance/reference/whitepapers/6_performance.html Java SE 6 Performance White Paper] at http://java.sun.com

=Java SE 6 Update 10=

  • Java Quick Starter reduces application start-up time by preloading part of JRE data at OS startup on disk cache.{{Cite web

| url=http://java.sun.com/developer/technicalArticles/javase/consumerjre#Quickstarter

| title=Consumer JRE: Leaner, Meaner Java Technology

| publisher=Sun Microsystems

|last=Haase|first=Chet

| quote=At the OS level, all of these megabytes have to be read from disk, which is a very slow operation. Actually, it's the seek time of the disk that's the killer; reading large files sequentially is relatively fast, but seeking the bits that we actually need is not. So even though we only need a small fraction of the data in these large files for any particular application, the fact that we're seeking all over within the files means that there is plenty of disk activity.

|date= May 2007| access-date=July 27, 2007}}

  • Parts of the platform needed to execute an application accessed from the web when JRE is not installed are now downloaded first. The full JRE is 12 MB, a typical Swing application only needs to download 4 MB to start. The remaining parts are then downloaded in the background.{{Cite web

| url=http://java.sun.com/developer/technicalArticles/javase/consumerjre#JavaKernel

| title=Consumer JRE: Leaner, Meaner Java Technology

| publisher=Sun Microsystems

| last=Haase|first=Chet

|date= May 2007| access-date=July 27, 2007}}

  • Graphics performance on Windows improved by extensively using Direct3D by default,{{Cite web|url=http://java.sun.com/developer/technicalArticles/javase/consumerjre#Performance|title=Consumer JRE: Leaner, Meaner Java Technology|publisher=Sun Microsystems|last=Haase|first=Chet|date= May 2007|access-date=July 27, 2007}} and use shaders on graphics processing unit (GPU) to accelerate complex Java 2D operations.{{Cite web|url=https://weblogs.java.net/blog/campbell/archive/2007/04/faster_java_2d.html|title=Faster Java 2D Via Shaders|last=Campbell|first=Chris|date=April 7, 2007|access-date=January 18, 2011|archive-url=https://web.archive.org/web/20110605111343/http://weblogs.java.net/blog/campbell/archive/2007/04/faster_java_2d.html|archive-date=June 5, 2011|url-status=dead|df=mdy-all}}

=Java 7=

Several performance improvements have been released for Java 7:

Future performance improvements are planned for an update of Java 6 or Java 7:{{Cite web

| url=http://java.sun.com/developer/technicalArticles/javase/consumerjre

| title=Consumer JRE: Leaner, Meaner Java Technology

| publisher=Sun Microsystems

|last=Haase|first=Chet

|date= May 2007| access-date=July 27, 2007}}

| url=http://www.jcp.org/en/jsr/detail?id=292

| title=JSR 292: Supporting Dynamically Typed Languages on the Java Platform

| publisher=jcp.org

| access-date=May 28, 2008}}

| url=http://www.ibm.com/developerworks/java/library/j-jtp03048.html?ca

| title=Java theory and practice: Stick a fork in it, Part 2

| last=Goetz|first=Brian

| website=IBM

| date=March 4, 2008

| access-date=March 9, 2008}}{{Cite web

| url=http://www.infoq.com/news/2008/03/fork_join

| title=Parallelism with Fork/Join in Java 7

| last=Lorimer|first=R.J.

| publisher=infoq.com

| date=March 21, 2008

| access-date=May 28, 2008}}

  • Allow the JVM to use both the client and server JIT compilers in the same session with a method called tiered compiling:{{Cite web

| url=http://developers.sun.com/learning/javaoneonline/2006/coreplatform/TS-3412.pdf

| title=New Compiler Optimizations in the Java HotSpot Virtual Machine

| publisher=Sun Microsystems

|date= May 2006| access-date=May 30, 2008}}

  • The client would be used at startup (because it is good at startup and for small applications),
  • The server would be used for long-term running of the application (because it outperforms the client compiler for this).
  • Replace the existing concurrent low-pause garbage collector (also called concurrent mark-sweep (CMS) collector) by a new collector called Garbage First (G1) to ensure consistent pauses over time.{{Cite web

| url=http://www.infoq.com/news/2008/05/g1

| title=JavaOne: Garbage First

| publisher=infoq.com

| last=Humble|first=Charles

| date=May 13, 2008

| access-date=September 7, 2008

}}

{{Cite web

| url=http://blogs.oracle.com/theplanetarium/entry/java_vm_trying_a_new

| title=Java VM: Trying a new Garbage Collector for JDK 7

| last=Coward

| first=Danny

| date=November 12, 2008

| access-date=November 15, 2008

| archive-url=https://web.archive.org/web/20111208114910/http://blogs.oracle.com/theplanetarium/entry/java_vm_trying_a_new

| archive-date=December 8, 2011

| url-status=dead

| df=mdy-all

}}

Comparison to other languages

Objectively comparing the performance of a Java program and an equivalent one written in another language such as C++ needs a carefully and thoughtfully constructed benchmark which compares programs completing identical tasks. The target platform of Java's bytecode compiler is the Java platform, and the bytecode is either interpreted or compiled into machine code by the JVM. Other compilers almost always target a specific hardware and software platform, producing machine code that will stay virtually unchanged during execution{{citation needed|reason=What are the real world, non-theoretical implications of this?|date=May 2016}}. Very different and hard-to-compare scenarios arise from these two different approaches: static vs. dynamic compilations and recompilations, the availability of precise information about the runtime environment and others.

Java is often compiled just-in-time at runtime by the Java virtual machine, but may also be compiled ahead-of-time, as is C++. When compiled just-in-time, the micro-benchmarks of The Computer Language Benchmarks Game indicate the following about its performance:

{{Cite web

| url=http://benchmarksgame.alioth.debian.org/u32q/which-programs-are-fastest.html

| title=Computer Language Benchmarks Game

| publisher=benchmarksgame.alioth.debian.org

| access-date=June 2, 2011

| archive-url=https://web.archive.org/web/20150125100238/http://benchmarksgame.alioth.debian.org/u32q/which-programs-are-fastest.html

| archive-date=January 25, 2015

| url-status=dead

| df=mdy-all

}}

  • slower than compiled languages such as C or C++,{{Cite web

| url=http://benchmarksgame.alioth.debian.org/u64q/java.html

| title=Computer Language Benchmarks Game

| publisher=benchmarksgame.alioth.debian.org

| access-date=June 2, 2011

| archive-url=https://web.archive.org/web/20150113040554/http://benchmarksgame.alioth.debian.org/u64q/java.html

| archive-date=January 13, 2015

| url-status=dead

}}

  • similar to other just-in-time compiled languages such as C#,{{Cite web

| url=http://benchmarksgame.alioth.debian.org/u64q/csharp.html

| title=Computer Language Benchmarks Game

| publisher=benchmarksgame.alioth.debian.org

| access-date=June 2, 2011

| archive-url=https://web.archive.org/web/20150110034032/http://benchmarksgame.alioth.debian.org/u64q/csharp.html

| archive-date=January 10, 2015

| url-status=dead

}}

  • much faster than languages without an effective native-code compiler (JIT or AOT), such as Perl, Ruby, PHP and Python.{{Cite web

| url=http://benchmarksgame.alioth.debian.org/u64q/python.html

| title=Computer Language Benchmarks Game

| publisher=benchmarksgame.alioth.debian.org

| access-date=June 2, 2011

| archive-url=https://web.archive.org/web/20150102034407/http://benchmarksgame.alioth.debian.org/u64q/python.html

| archive-date=January 2, 2015

| url-status=dead

}}

=Program speed=

Benchmarks often measure performance for small numerically intensive programs. In some rare real-life programs, Java out-performs C. One example is the benchmark of Jake2 (a clone of Quake II written in Java by translating the original GPL C code). The Java 5.0 version performs better in some hardware configurations than its C counterpart.: 260/250 frame/s versus 245 frame/s (see [http://www.bytonic.de/html/benchmarks.html benchmark]) While it is not specified how the data was measured (for example if the original Quake II executable compiled in 1997 was used, which may be considered bad as current C compilers may achieve better optimizations for Quake), it notes how the same Java source code can have a huge speed boost just by updating the VM, something impossible to achieve with a 100% static approach.

For other programs, the C++ counterpart can, and usually does, run significantly faster than the Java equivalent. A benchmark performed by Google in 2011 showed a factor 10 between C++ and Java.{{Cite journal |last1=Hundt |first1=Robert |title=Loop Recognition in C++/Java/Go/Scala |journal=Scala Days 2011 |location=Stanford, California |access-date=March 23, 2014 |url=https://days2011.scala-lang.org/sites/days2011/files/ws3-1-Hundt.pdf}} At the other extreme, an academic benchmark performed in 2012 with a 3D modelling algorithm showed the Java 6 JVM being from 1.09 to 1.91 times slower than C++ under Windows.{{cite web

| url= http://www.best-of-robotics.org/pages/publications/gherardi12java.pdf

| title=A Java vs. C++ performance evaluation: a 3D modeling benchmark

| publisher=University of Bergamo

|author1=L. Gherardi |author2=D. Brugali |author3=D. Comotti | year= 2012

| quote=Using the Server compiler, which is best tuned for long-running applications, have instead demonstrated that Java is from 1.09 to 1.91 times slower(...)In conclusion, the results obtained with the server compiler and these important features suggest that Java can be considered a valid alternative to C++

| access-date= March 23, 2014}}

Some optimizations that are possible in Java and similar languages may not be possible in certain circumstances in C++:{{Cite web|url=http://scribblethink.org/Computer/javaCbenchmark.html |title=Performance of Java versus C++ |publisher=Computer Graphics and Immersive Technology Lab, University of Southern California| author=Lewis, J.P. |author2=Neumann, Ulrich}}

  • C-style pointer use can hinder optimizing in languages that support pointers,
  • The use of escape analysis methods is limited in C++, for example, because a C++ compiler does not always know if an object will be modified in a given block of code due to pointers,Contention of this nature can be alleviated in C++ programs at the source code level by employing advanced methods such as custom allocators, exploiting precisely the kind of low-level coding complexity that Java was designed to conceal and encapsulate; however, this approach is rarely practical if not adopted (or at least anticipated) while the program remains under primary development.
  • Java can access derived instance methods faster than C++ can access derived virtual methods due to C++'s extra virtual-table look-up. However, non-virtual methods in C++ do not suffer from v-table performance bottlenecks, and thus exhibit performance similar to Java.

The JVM is also able to perform processor specific optimizations or inline expansion. And, the ability to deoptimize code already compiled or inlined sometimes allows it to perform more aggressive optimizations than those performed by statically typed languages when external library functions are involved.{{Cite web

| url=http://java.sun.com/developer/technicalArticles/Networking/HotSpot/inlining.html

| title=The Java HotSpot Performance Engine: Method Inlining Example

| publisher=Oracle Corporation

| access-date=June 11, 2011}}{{Cite web

| url=http://blog.headius.com/2008/05/power-of-jvm.html

| title=The Power of the JVM

| date=May 3, 2008

| last=Nutter|first=Charles

| quote=What happens if you've already inlined A's method when B comes along? Here again the JVM shines. Because the JVM is essentially a dynamic language runtime under the covers, it remains ever-vigilant, watching for exactly these sorts of events to happen. And here's the really cool part: when situations change, the JVM can deoptimize. This is a crucial detail. Many other runtimes can only do their optimization once. C compilers must do it all ahead of time, during the build. Some allow you to profile your application and feed that into subsequent builds, but once you've released a piece of code it's essentially as optimized as it will ever get. Other VM-like systems like the CLR do have a JIT phase, but it happens early in execution (maybe before the system even starts executing) and doesn't ever happen again. The JVM's ability to deoptimize and return to interpretation gives it room to be optimistic...room to make ambitious guesses and gracefully fall back to a safe state, to try again later.

| access-date=June 11, 2011}}

Results for microbenchmarks between Java and C++ highly depend on which operations are compared. For example, when comparing with Java 5.0:

  • 32- and 64-bit arithmetic operations,{{Cite web

| url=http://www.ddj.com/java/184401976?pgno=2

| title=Microbenchmarking C++, C#, and Java: 32-bit integer arithmetic

| publisher=Dr. Dobb's Journal

| date=July 1, 2005

| access-date=January 18, 2011}}{{Cite web

| url=http://www.ddj.com/java/184401976?pgno=12

| title=Microbenchmarking C++, C#, and Java: 64-bit double arithmetic

| publisher=Dr. Dobb's Journal

| date=July 1, 2005

| access-date=January 18, 2011}} file input/output, {{Cite web

| url=http://www.ddj.com/java/184401976?pgno=15

| title=Microbenchmarking C++, C#, and Java: File I/O

| publisher=Dr. Dobb's Journal

| date=July 1, 2005

| access-date=January 18, 2011}} and exception handling{{Cite web

| url=http://www.ddj.com/java/184401976?pgno=17

| title=Microbenchmarking C++, C#, and Java: Exception

| publisher=Dr. Dobb's Journal

| date=July 1, 2005

| access-date=January 18, 2011}} have a similar performance to comparable C++ programs

  • Operations on arrays{{Cite web

| url=http://www.ddj.com/java/184401976?pgno=19

| title=Microbenchmarking C++, C#, and Java: Array

| publisher=Dr. Dobb's Journal

| date=July 1, 2005

| access-date=January 18, 2011}} have better performance in C.

| url=http://www.ddj.com/java/184401976?pgno=19

| title=Microbenchmarking C++, C#, and Java: Trigonometric functions

| publisher=Dr. Dobb's Journal

| date=July 1, 2005

| access-date=January 18, 2011}}

----

;Notes

{{Reflist|group=note}}

=Multi-core performance=

The scalability and performance of Java applications on multi-core systems is limited by the object allocation rate. This effect is sometimes called an "allocation wall".Yi Zhao, Jin Shi, Kai Zheng, Haichuan Wang, Haibo Lin and Ling Shao, [http://portal.acm.org/citation.cfm?id=1640116 Allocation wall: a limiting factor of Java applications on emerging multi-core platforms], Proceedings of the 24th ACM SIGPLAN conference on Object oriented programming systems languages and applications, 2009. However, in practice, modern garbage collector algorithms use multiple cores to perform garbage collection, which to some degree alleviates this problem. Some garbage collectors are reported to sustain allocation rates of over a gigabyte per second,{{Cite web |url=http://www.azulsystems.com/sites/default/files/images/c4_paper_acm_0.pdf |title=C4: The Continuously Concurrent Compacting Collector |access-date=October 29, 2013 |archive-date=August 9, 2014 |archive-url=https://web.archive.org/web/20140809222603/http://www.azulsystems.com/sites/default/files/images/c4_paper_acm_0.pdf |url-status=dead }} and there exist Java-based systems that have no problems scaling to several hundreds of CPU cores and heaps sized several hundreds of GB.[https://www.theregister.co.uk/2007/06/15/azul_releases_7200_systems/ Azul bullies Java with 768 core machine]

Automatic memory management in Java allows for efficient use of lockless and immutable data structures that are extremely hard or sometimes impossible to implement without some kind of a garbage collection.{{citation needed|date=September 2018}} Java offers a number of such high-level structures in its standard library in the java.util.concurrent package, while many languages historically used for high performance systems like C or C++ are still lacking them.{{citation needed|date=September 2017}}

=Startup time=

Java startup time is often much slower than many languages, including C, C++, Perl or Python, because many classes (and first of all classes from the platform Class libraries) must be loaded before being used.

When compared against similar popular runtimes, for small programs running on a Windows machine, the startup time appears to be similar to Mono's and a little slower than .NET's.{{Cite web

| url=http://www.codeproject.com/KB/dotnet/RuntimePerformance.aspx

| title=Benchmark start-up and system performance for .Net, Mono, Java, C++ and their respective UI

| date=September 2, 2010}}

It seems that much of the startup time is due to input-output (IO) bound operations rather than JVM initialization or class loading (the rt.jar class data file alone is 40 MB and the JVM must seek much data in this big file). Some tests showed that although the new split bytecode verification method improved class loading by roughly 40%, it only realized about 5% startup

improvement for large programs.{{Cite web

|url = http://forums.java.net/jive/thread.jspa?messageID=94530

|title = How fast is the new verifier?

|date = February 7, 2006

|access-date = May 9, 2007

|url-status = dead

|archive-url = https://web.archive.org/web/20060516011057/http://forums.java.net/jive/thread.jspa?messageID=94530

|archive-date = May 16, 2006

|df = dmy-all

}}

Albeit a small improvement, it is more visible in small programs that perform a simple operation and then exit, because the Java platform data loading can represent many times the load of the actual program's operation.

Starting with Java SE 6 Update 10, the Sun JRE comes with a Quick Starter that preloads class data at OS startup to get data from the disk cache rather than from the disk.

Excelsior JET approaches the problem from the other side. Its Startup Optimizer reduces the amount of data that must be read from the disk on application startup, and makes the reads more sequential.

In November 2004, Nailgun, a "client, protocol, and server for running Java programs from the command line without incurring the JVM startup overhead" was publicly released.[http://martiansoftware.com/nailgun/ Nailgun] introducing for the first time an option for scripts to use a JVM as a daemon, for running one or more Java applications with no JVM startup overhead. The Nailgun daemon is insecure: "all programs are run with the same permissions as the server". Where multi-user security is needed, Nailgun is inappropriate without special precautions. Scripts where per-application JVM startup dominates resource use, see one to two order of magnitude runtime performance improvements.The Nailgun [http://martiansoftware.com/nailgun/background.html Background] page demonstrates "best case scenario" speedup of 33 times (for scripted "Hello, World!" programs i.e., short-run programs).

=Memory use=

{{Disputed section|Most_of_the_memory_use_section_is_really_odd_nitpicks|date=August 2019}}

Java memory use is much higher than C++'s memory use because:

  • There is an overhead of 8 bytes for each object and 12 bytes for each array{{Cite web|url=http://www.javamex.com/tutorials/memory/object_memory_usage.shtml|title = How to calculate the memory usage of Java objects}} in Java. If the size of an object is not a multiple of 8 bytes, it is rounded up to next multiple of 8. This means an object holding one byte field occupies 16 bytes and needs a 4-byte reference. C++ also allocates a pointer (usually 4 or 8 bytes) for every object which class directly or indirectly declares virtual functions.{{cite web |url=http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=195 |title=InformIT: C++ Reference Guide > the Object Model |access-date=June 22, 2009 |url-status=dead |archive-url=https://web.archive.org/web/20080221131118/http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=195 |archive-date=February 21, 2008 |df=dmy-all }}
  • Lack of address arithmetic makes creating memory-efficient containers, such as tightly spaced structures and XOR linked lists, currently impossible (the OpenJDK Valhalla project aims to mitigate these issues, though it does not aim to introduce pointer arithmetic; this cannot be done in a garbage collected environment).
  • Contrary to malloc and new, the average performance overhead of garbage collection asymptotically nears zero (more accurately, one CPU cycle) as the heap size increases.https://www.youtube.com/watch?v=M91w0SBZ-wc : Understanding Java Garbage Collection - a talk by Gil Tene at JavaOne
  • Parts of the Java Class Library must load before program execution (at least the classes used within a program).{{Cite web|url=http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html|title = .: ToMMTi-Systems :: Hinter den Kulissen moderner 3D-Hardware}} This leads to a significant memory overhead for small applications.{{citation needed|date=January 2012}}
  • Both the Java binary and native recompilations will typically be in memory.
  • The virtual machine uses substantial memory.
  • In Java, a composite object (class A which uses instances of B and C) is created using references to allocated instances of B and C. In C++ the memory and performance cost of these types of references can be avoided when the instance of B and/or C exists within A.

In most cases a C++ application will consume less memory than an equivalent Java application due to the large overhead of Java's virtual machine, class loading and automatic memory resizing. For programs in which memory is a critical factor for choosing between languages and runtime environments, a cost/benefit analysis is needed.

=Trigonometric functions=

Performance of trigonometric functions is bad compared to C, because Java has strict specifications for the results of mathematical operations, which may not correspond to the underlying hardware implementation.{{Cite web

| url= http://java.sun.com/javase/6/docs/api/java/lang/Math.html

| title= Math (Java Platform SE 6)

| publisher= Sun Microsystems

| access-date=June 8, 2008

}} On the x87 floating point subset, Java since 1.4 does argument reduction for sin and cos in software,

{{Cite web

| url=http://blogs.oracle.com/jag/entry/transcendental_meditation

| title=Transcendental Meditation

| access-date=June 8, 2008

| date=July 27, 2005

| first=James

| last=Gosling

| archive-url=https://web.archive.org/web/20110812023545/http://blogs.oracle.com/jag/entry/transcendental_meditation

| archive-date=August 12, 2011

| url-status=dead

| df=mdy-all

}}

causing a big performance hit for values outside the range.{{Cite web

| url=http://www.osnews.com/story/5602&page=3

| title=Nine Language Performance Round-up: Benchmarking Math & File I/O

| last=Cowell-Shah

| first=Christopher W.

| date=January 8, 2004

| access-date=June 8, 2008

| archive-url=https://web.archive.org/web/20181011222232/http://www.osnews.com/story/5602%26page%3D3

| archive-date=October 11, 2018

| url-status=dead

}}{{clarify|date=April 2016}}

=Java Native Interface=

The Java Native Interface invokes a high overhead, making it costly to cross the boundary between code running on the JVM and native code.{{Cite web

| url=http://java.sun.com/docs/books/performance/1st_edition/html/JPNativeCode.fm.html

| title=JavaTM Platform Performance: Using Native Code

| last=Wilson|first=Steve

|author2=Jeff Kesselman

| publisher=Sun Microsystems

| year=2001

| access-date=February 15, 2008}}

{{Cite web

|url = http://janet-project.sourceforge.net/papers/jnibench.pdf

|title = Efficient Cooperation between Java and Native Codes - JNI Performance Benchmark

|last = Kurzyniec

|first = Dawid

|author2 = Vaidy Sunderam

|access-date = February 15, 2008

|url-status = dead

|archive-url = https://web.archive.org/web/20050214080519/http://janet-project.sourceforge.net/papers/jnibench.pdf

|archive-date = February 14, 2005

|df = dmy-all

}}{{sfn|Bloch|2018|loc=Chapter §11 Item 66: Use native methods judiciously|p=285}} Java Native Access (JNA) provides Java programs easy access to native shared libraries (dynamic-link library (DLLs) on Windows) via Java code only, with no JNI or native code. This functionality is comparable to Windows' Platform/Invoke and Python's ctypes. Access is dynamic at runtime without code generation. But it has a cost, and JNA is usually slower than JNI.{{Cite web

|url = https://jna.dev.java.net/#performance

|title = How does JNA performance compare to custom JNI?

|publisher = Sun Microsystems

|access-date = December 26, 2009

}}{{dead link|date=November 2017 |bot=InternetArchiveBot |fix-attempted=yes }}

=User interface=

Swing has been perceived as slower than native widget toolkits, because it delegates the rendering of widgets to the pure Java 2D API. However, benchmarks comparing the performance of Swing versus the Standard Widget Toolkit, which delegates the rendering to the native GUI libraries of the operating system, show no clear winner, and the results greatly depend on the context and the environments.{{Cite web

|url = http://cosylib.cosylab.com/pub/CSS/DOC-SWT_Vs._Swing_Performance_Comparison.pdf

|title = SWT Vs. Swing Performance Comparison

|first = Križnar

|last = Igor

|publisher = cosylab.com

|date = May 10, 2005

|quote = It is hard to give a rule-of-thumb where SWT would outperform Swing, or vice versa. In some environments (e.g., Windows), SWT is a winner. In others (Linux, VMware hosting Windows), Swing and its redraw optimization outperform SWT significantly. Differences in performance are significant: factors of 2 and more are common, in either direction

|access-date = May 24, 2008

|archive-url = https://web.archive.org/web/20080704103309/http://cosylib.cosylab.com/pub/CSS/DOC-SWT_Vs._Swing_Performance_Comparison.pdf

|archive-date = July 4, 2008

|url-status = dead

|df = dmy-all

}} Additionally, the newer JavaFX framework, intended to replace Swing, addresses many of Swing's inherent issues.

=Use for high performance computing=

Some people believe that Java performance for high performance computing (HPC) is similar to Fortran on compute-intensive benchmarks, but that JVMs still have scalability issues for performing intensive communication on a grid computing network.{{Cite web

| url=http://hal.inria.fr/inria-00312039/en

| title=Current State of Java for HPC

| quote=We first perform some micro benchmarks for various JVMs, showing the overall good performance for basic arithmetic operations(...). Comparing this implementation with a Fortran/MPI one, we show that they have similar performance on computation intensive benchmarks, but still have scalability issues when performing intensive communications.

|author1=Brian Amedro |author2=Vladimir Bodnartchouk |author3=Denis Caromel |author4=Christian Delbe |author5=Fabrice Huet |author6=Guillermo L. Taboada | publisher=INRIA

|date= August 2008 |access-date=September 9, 2008}}

However, high performance computing applications written in Java have won benchmark competitions. In 2008,{{Cite web

|url = http://developer.yahoo.net/blogs/hadoop/2008/07/apache_hadoop_wins_terabyte_sort_benchmark.html

|title = Apache Hadoop Wins Terabyte Sort Benchmark

|quote = This is the first time that either a Java or an open source program has won.

|author = Owen O'Malley - Yahoo! Grid Computing Team

|date = July 2008

|access-date = December 21, 2008

|url-status = dead

|archive-url = https://web.archive.org/web/20091015215436/http://developer.yahoo.net/blogs/hadoop/2008/07/apache_hadoop_wins_terabyte_sort_benchmark.html

|archive-date = October 15, 2009

|df = dmy-all

}}

and 2009,{{Cite web

| url=http://developer.yahoo.net/blogs/hadoop/2009/05/hadoop_sorts_a_petabyte_in_162.html

| archive-url=https://web.archive.org/web/20090516184005/http://developer.yahoo.net/blogs/hadoop/2009/05/hadoop_sorts_a_petabyte_in_162.html

| url-status=dead

| archive-date=May 16, 2009

| title=Hadoop Sorts a Petabyte in 16.25 Hours and a Terabyte in 62 Seconds

| date=May 11, 2009

| quote=The hardware and operating system details are:(...)Sun Java JDK (1.6.0_05-b13 and 1.6.0_13-b03) (32 and 64 bit)

| access-date=September 8, 2010

| publisher=CNET.com}}

{{Cite web

| url=http://news.cnet.com/8301-13846_3-10242392-62.html

| title=Hadoop breaks data-sorting world records

| date=May 15, 2009

| access-date=September 8, 2010

| publisher=CNET.com}}

an Apache Hadoop (an open-source high performance computing project written in Java) based cluster was able to sort a terabyte and petabyte of integers the fastest. The hardware setup of the competing systems was not fixed, however.{{cite web

| url=http://sortbenchmark.org/

| title=Sort Benchmark Home Page

|author1=Chris Nyberg |author2=Mehul Shah | access-date=November 30, 2010

}}{{cite web

| url=http://googleblog.blogspot.com/2008/11/sorting-1pb-with-mapreduce.html

| title=Sorting 1PB with MapReduce

| date=November 21, 2008

| access-date=December 1, 2010

| first=Grzegorz

| last=Czajkowski

}}

=In programming contests=

Programs in Java start slower than those in other compiled languages.{{Cite web |url=http://topcoder.com/home/tco10/2010/06/08/algorithms-problem-writing/ |title=TCO10 |access-date=June 21, 2010 |archive-url=https://web.archive.org/web/20101018212921/http://topcoder.com/home/tco10/2010/06/08/algorithms-problem-writing/ |archive-date=October 18, 2010 |url-status=dead |df=dmy-all }}{{cite web | url=http://acm.timus.ru/help.aspx?topic=java&locale=en | title=How to write Java solutions @ Timus Online Judge }} Thus, some online judge systems, notably those hosted by Chinese universities, use longer time limits for Java programs{{cite web | url=http://acm.pku.edu.cn/JudgeOnline/faq.htm#q11 | title=FAQ }}{{Cite web |url=http://acm.tju.edu.cn/toj/faq.html#qj |title=FAQ | TJU ACM-ICPC Online Judge |access-date=May 25, 2010 |archive-url=https://web.archive.org/web/20100629135921/http://acm.tju.edu.cn/toj/faq.html#qj |archive-date=June 29, 2010 |url-status=dead |df=mdy-all }}{{cite web | url=http://www.codechef.com/wiki/faq#How_does_the_time_limit_work | title=FAQ | CodeChef }}{{Cite web |url=http://acm.xidian.edu.cn/land/faq |title=HomePage of Xidian Univ. Online Judge |access-date=November 13, 2011 |archive-url=https://web.archive.org/web/20120219004452/http://acm.xidian.edu.cn/land/faq |archive-date=February 19, 2012 |url-status=dead |df=dmy-all }}{{cite web | url=http://poj.org/faq.htm#q9 | title=FAQ }} to be fair to contestants using Java.

See also

{{Portal|Computer programming}}

Citations

{{Reflist|2|refs=

{{cite news |url=http://www.cnet.com/news/short-take-apple-licenses-symantecs-just-in-time-compiler/

| title=Short Take: Apple licenses Symantec's just-in-time compiler |publisher= cnet.com

| date= May 12, 1998 |access-date= November 15, 2015}}

}}

References

  • {{cite book |last=Bloch| first=Joshua| title= "Effective Java: Programming Language Guide" | publisher=Addison-Wesley | edition=third | isbn=978-0134685991| date=2018}}