Slashdot Mirror


Eye on Java performance Improvements

An anonymous reader writes "Performance. It's the one aspect of the Java platform that continually takes abuse. But the overwhelming success of the platform on other fronts makes performance issues worth serious investigation. In this article, Intrepid optimizers Jack Shirazi and Kirk Pepperdine, Director and CTO of JavaPerformanceTuning.com, look at compilation speed, exceptions, and heap size tuning."

25 of 84 comments (clear)

  1. "Level: Introductory" by jtheory · · Score: 4, Informative

    Level: Introductory
    Just a note -- don't bother reading if you've ever looked into Java tuning before.

    I admit I only skimmed the article... but the tips I saw are all simple suggestions that have been around for years now.

    Use Jikes for quick compilations? Jikes has been around since before the Java 1.2 days.

    Only throw Exceptions in "exceptional" cases, because they will slow things down? Again, advice I've been hearing since the early days.

    Nary a word about exploiting the new IO classes, or evaluating performance of XML vs. custom formats, etc. etc.

    --
    There are only 10 types of people: those who understand decimal, those who don't, and, uh, 8 other types I forget.
    1. Re:"Level: Introductory" by WolfWithoutAClause · · Score: 2, Insightful
      Only throw Exceptions in "exceptional" cases, because they will slow things down?

      Actually, if you read the article carefully, you find that it says that you should only create exceptions in exceptional cases; that's the slow step, throwing and catching are fairly fast.

      So, presumably, if you create a single exception, and keep throwing and catching it, the performance would be much better. Of course the stack data will be wrong, since it will represent the place where it was created, but for many applications that may not matter.

      --

      -WolfWithoutAClause

      "Gravity is only a theory, not a fact!"
    2. Re:"Level: Introductory" by Ian+Jefferies · · Score: 3, Interesting

      Only throw Exceptions in "exceptional" cases, because they will slow things down? Again, advice I've been hearing since the early days.

      One tip from Java Performance Tuning by Jack Shirazi is to create the Exception once, store it statically, and throw the object as many times as you need to.

      Ugly? Sure. Hacky? Definitely.

      But if you're after that extra bit of performance without leaving Java, need to use exceptions, and don't care about the stack trace, then it saves many cycles.

      One thing that I've found with hig performance numerical routines is the cost of array lookup (with all of its runtime range checking) is something that can really slow a routine down. I've taken a routine that ran in 500ms and tuned it down to about 80ms (running under the Hotspot compiler). A comparable hand optimized assembler routine doing the same job took 20ms.

      One example doesn't make a trend... but when you know what you're doing the performance is there for the taking.

      Ian.

      --
      A physicist is an atom's way of thinking about atoms
    3. Re:"Level: Introductory" by Ian+Jefferies · · Score: 2, Insightful

      I'm not totally surprised, but I am a little. Hotspot can often do range checking at compile time and remove the run time checks. However, for it to do that successfully may depend on subtle features of the code, so it won't always succeed. Additionally it may well depend on which version of the jre you are working with.

      Range checking at compile time is probably best done by the javac compiler. Hotspot is busy compiling and converting java bytecodes at runtime, so it's unlikely it can do the complicated flow analysis required to determine if the runtime range check can be removed. You start from the position that the range check can't be removed, and find a reason to do so... not the other way around.

      You're right about version dependency on the JRE for performance. Shirazi's book (linked in my original post) brings this out in great detail. More importantly you have to remove the effect of the hotspot compilation: either tune on the interpreted code only, or let the program run long enough for the hotspots to be found and compiled before looking at performance data. When the hotspot compiled code kicks in all of the statistics gathered up to that point get badly skewed.

      Ian.

      --
      A physicist is an atom's way of thinking about atoms
    4. Re:"Level: Introductory" by Ian+Jefferies · · Score: 3, Informative

      There's a technical paper on HotSpot technology that covers much of the ground. It does mention range checking removal, but also some of the complications: dynamic loading of classes, runtime reflective method calling, and adherence to the Java security model. From a quick read it suggests that flow analysis is appropriate for inlining virtual method calls, and that de-optimization has to happen when the environment changes. Complicated stuff.

      I know enough about compilation optimization to appreciate how complicated a subject it is, and that many optimizations can appear to be counter-productive. I'm always going to have an interest in it, but don't think I'm ever going to be working on it from the inside by doing software research.

      Cheers,

      Ian.

      --
      A physicist is an atom's way of thinking about atoms
  2. Optimize your way to ugly APIs by brlewis · · Score: 4, Insightful

    They talk about how expensive it is to create an exception, and caution against it. Beware undue aversion to exceptions! For example, java.io.File.getLastModified(), if called on a file that doesn't exist, will return 0. An exception really ought to be thrown, but who's going to change the API now?

    People emphasize performance too much. Look how successful PHP has been despite its slowness. Deal with functionality first, and only worry about performance if and when it's a problem.

    1. Re:Optimize your way to ugly APIs by pmz · · Score: 2, Funny

      Deal with functionality first, and only worry about performance if and when it's a problem.

      But, then, how can we have flamewars about each J2EE vs. .NET report that comes out?!?!?

  3. Article doesn't say much by cxvx · · Score: 5, Informative
    There are better articles on the IBM site discussing java performance:

    Knowing when to optimize is more important than knowing how to optimize
    Urban performance legends

    --
    If only I could come up with a good sig ...
  4. Free Java Performance Tips by LarryRiedel · · Score: 3, Funny

    • Try never to use java.lang.String.
    • Try not to use synchronized.
    • Try not to use java.io.* or java.net.* if there is something in java.nio.* that be used instead.
    • Try not to create objects.

    Larry

    1. Re:Free Java Performance Tips by El+Neepo · · Score: 2, Informative

      looks like you need to read this

      http://www-106.ibm.com/developerworks/java/libra ry /j-jtp04223.html

    2. Re:Free Java Performance Tips by Golthar · · Score: 2, Informative

      *Depends, when the data is suposed to be inmutable, you should use Stings (constants)
      If you are constructing your data, then a Stringbuffer is indeed best (In fact, Javac will try to use a Stringbuffer internaly)

      *Synchonisation is getting better with every release, now ofcourse you should not synch more than needed, but if you don't synch properly, you will find that deadlocks will be a far worse performance problem.

      *Yes, but sometimes it needs to be able to run on lower than 1.4 versions.
      Also, the NIO classes have been a moving target (the first implementation was not very platform independant, had small differences between VM's.
      I suggest you detect the VM version and load a class that implements your loading based on the available methods.

      *Try not to create more than needed.
      Also, try not to hold on to these objects too long.
      The VM works with different spaces in memory and short lived objects will be GCed and reused with ease.
      This rule does not apply to hard to create objects (like a Database connection, which needs to be created at some point and should be pooled)

  5. java and net by raffe · · Score: 2, Informative

    Here is a recent study about java and .net.
    The result ?

    "The Middleware Company has released a J2EE and .NET Performance case study, the latest study (an MDA productivity study was released a few weeks ago) based on their Application Server Baseline Spec. Except for the web services test, the two platforms came out mostly equal in performance. "

  6. Slow is relative by bwt · · Score: 2, Insightful

    I always get amused when open source people spout of about how slow Java supposedly is and how much they love perl/python/php in the other. Java is pretty much faster than all of those. Yet nobody ever dogs on python (say) for being slow. Why?

    1. Re:Slow is relative by Slick_Snake · · Score: 2, Insightful
      I always get amused when open source people spout of about how slow Java supposedly is and how much they love perl/python/php in the other. Java is pretty much faster than all of those.

      There are different standards for "compiled languages" than there are for "interpreted languages." Plus you have to look at what the language is intended for. Java touts itself as an application language when perl/python/php are more of utility languages. Yes I know that you can do more than just simple parsing and cgi with perl/python/php, but that is not the point. The point is the type of language and what it is typically used for. You don't see many word processors written in an interpreted language.

    2. Re:Slow is relative by Dan+Ost · · Score: 4, Interesting

      Why?

      Because if performance is an issue, you find your bottleneck and replace the
      bottleneck Python code with a C module.

      Suddenly your Python "Script" runs 99% as fast as if you'd written it in
      C in the first place.

      That's why nobody ever dogs on Python for being slow: Python makes it simple
      to get the performance you'd expect from C while only requiring a minimal
      amount of actual C code to be written.

      --

      *sigh* back to work...
    3. Re:Slow is relative by Arandir · · Score: 2, Insightful

      Python people aren't claiming that Python is greyhound of performance. But I have seen more than one article by a Java advocate making the strange claim that Java is faster than C.

      --
      A Government Is a Body of People, Usually Notably Ungoverned
    4. Re:Slow is relative by smallpaul · · Score: 2, Informative

      First, compare the ease of implementation to Python...especially when it comes to garbage collection and threading. Second, Python folks won't look down on you for using C where it is appropriate. Sun has this big marketing campaign about how code should be "100% Java". Third, Python people have worked much harder on tools to make integrating C easy: Pyrex, Distutils, SWIG, CTypes, win32com and pyxpcom.

  7. Quick useful user-level approach by 0x0d0a · · Score: 2, Informative

    From a user's point of view, here's what's important WRT Java.

    * Java still uses a lot more memory and cycles than C/Pascal/C++/etc. Generally, if there's a Java program and a C equivalent, you want to use the C equivalent.

    * The IBM JDK is the fastest current way to run Java on Linux.

    * Eclipse is the free Java IDE that everyone loves.

    * No, the Freenet people still haven't made a C version.

    1. Re:Quick useful user-level approach by OSSMKitty · · Score: 2, Interesting

      I'm going to disagree that the IBM JDK is the fastest. In my experience (enterprise server apps), the BEA JVM, JRockit, is the fastest. We ran some tests that showed something like the IBM 1.4.1 VM was 2/3 faster than the Sun 1.4.2 VM, but BEA 1.4.1 was more than twice as fast as Sun's. We watched the execution speed over time, and JRockit took very few iterations to reach top speed. Sun and IBM both took many more iterations. What's particularly nice about BEA is the builtin monitoring.

    2. Re:Quick useful user-level approach by Javagator · · Score: 2, Interesting

      Its unfortunate that Sun's JVM is about the slowest one out there. Hotspot has never lived up to its potential. The poor performance of the reference implementation is one of the reasons Java has a reputation of being slow.

  8. Yeah, yeah by jtheory · · Score: 2, Interesting

    I was summarizing quickly (yes, I know it's the creation that's costly) -- the point is that these are details about the language that any Java developer probably already knows or can intuit.

    I just wanted to warn off experienced developers who were expecting the "serious investigation" that was promised in the posting.

    By the way, I would NOT suggest reusing Exceptions. What you should strive to do is ensure that, in normal usage of your application, Exceptions will not be thrown (hence your program will be operating at peak speed). When there are errors, though, throw Exceptions then -- because it doesn't help anyone for your app to speedily come to a false result because the errors was eaten somewhere along the way.

    I.e., don't throw Exceptions as part of parsing user input -- it's part of your application's job to check input and report faulty entries to the user. You're handling it. BUT you probably should throw an Exception if a file you are using disappears.

    A slightly better example -- if you wrote a SETI-at-home style application that checked if data was within certain bounds, it would be bad to have a method that threw an Exception when data was outside those bounds... and just keep catching those Exceptions until you found the data you wanted.

    Sounds stupid, but newbies do this kind of thing sometimes as a simple way to bail out when you finish processing early in the method body.

    --
    There are only 10 types of people: those who understand decimal, those who don't, and, uh, 8 other types I forget.
  9. Re:Question for Java and Perl developers by LunaticLeo · · Score: 2, Informative

    I think Perl. Use POE; see poe.perl.org. Fast well structured code. POE is gods gift to Perl programmers (or at least Rocco Caputo's Gift).

    Otherwise use java.nio. Unfotunately, since it is a new api there is only one shitty application framwork built around it called SEDA. At first, I thought SEDA was cool, then I used it, found problems, tried to report problems, got no response, noticed there have been no updates in nearly a year. Fuckers.

    If you like Python there is a feature rich, event loop style app framwork called TwistedPython. Haven't used it but it looks good. Check out www.twistedmatrix.com .

    --
    -- I am not a fanatic, I am a true believer.
  10. No, no Re:Yeah, yeah by WolfWithoutAClause · · Score: 2, Interesting
    yes, I know it's the creation that's costly

    Frankly, I didn't. I knew that exceptions were expensive, but I didn't know why; and somewhat moronic summaries like yours (no offense, yours was about par for the course in fact) didn't help me understand why.

    By the way, I would NOT suggest reusing Exceptions.

    I would, where appropriate. I've just benchmarked it on jdk1.4.1 running Windows 2000, and I've found that exceptions run 20x faster if you do that (throwing an exception 1 million times took 10 seconds, reusing took 0.5 seconds on a 650 Mhz Intel- and frankly, a million is a heck of a lot of exceptions!)

    What you should strive to do is ensure that, in normal usage of your application, Exceptions will not be thrown (hence your program will be operating at peak speed).

    Most of the time speed isn't anything because the program is fast enough. It's only important to run fast where speed is insufficient.

    Sounds stupid, but newbies do this kind of thing sometimes as a simple way to bail out when you finish processing early in the method body.

    It doesn't sound necessarily stupid. Return or break statements are usually more appropriate though. In some situations (like recursion) an exception is quite possibly the best thing to do; and in that case reusing an exception should be done.

    --

    -WolfWithoutAClause

    "Gravity is only a theory, not a fact!"
  11. Java is Fast Enough, Now Sort out Memory Usage by rimu+guy · · Score: 2, Insightful

    I used to stress about Java performance. I'm not sure it was ever warranted. Certainly, things are plenty fast enough now.

    Not only have the JVMs improved - e.g. hotspot - but the servers and PCs I'm using now are 3 times faster (2.4Ghz vs. 800Mhz a few years back).

    What I think they need to concentrate on now, is getting the memory usage back down. 100MB to run an eclipse IDE is just too fat. And it's hard being a JSP Host when each Servlet engine chews up 60MB of memory on my host servers.

    FWIW, the 1.5 release sounds like memory will get a good looking at. Let's hope so.

  12. More comments by jtheory · · Score: 4, Insightful

    ...with less attitude (sorry, somehow I'm picking up the popular slashdot i-know-more-than-god tone... gotta watch that).

    The article in question does do a good job of explaining why Exception creation is expensive.

    I'm still dead-set against reuse of Exception objects, though, for some pretty good reasons. I've seen the suggestion before, and it bothers me because as optimizations go, it's very short-sighted.

    I'm sure you've heard the suggestion to "code for the human, not for the machine", because you code *will* need to be maintained, unless your project fails. ...and you may have also heard that this is nonsense and a great way to *ensure* that your project will fail because it's unuseable.

    As usual, the best answer is somewhere in-between. Every programmer should have the human reader in mind while designing and coding, but they should *also* design for efficiency. It has to be an informed compromise.

    Reusing Exceptions is one of those examples that I like to use of how *not* to optimize. It's a basic misuse of an integral part of the Java idiom. Everyone understands how Exceptions work, and how to use the stacktraces to find your error. It's dependable. The poor sucker who's on call when a customer calls because this application keeps crashing takes stacktraces for granted, and is going to be beating his head on his desk after 2 hours trying to figure out what is going on.

    This is a big cost. Okay, sometimes a cost is worth it, in optimizations. If this is a core data-crunching library that absolutely must perform at peak efficiency, maybe this is worth the painful maintenance cost.

    In this case, though, I can't think of any benefit to balance this cost, except that a programmer somewhere is pleased with herself for using an optimization she read in a book. If this data-crunching library is throwing Exceptions left and right in its normal processing, it's using Exceptions wrong.

    I hadn't thought about using the pre-made Exception hack to escape recursion -- it seems like a quicker way to get out than unwinding the recursion, after all -- but thinking about that more, I wouldn't suggest it even then. NOTE: comments below are not performance-tested, so if you really want to use recursion in a performance-critical part of an application, you should run some tests first.

    To begin with, recursion isn't a very efficient process in Java. Each time the method calls itself, all of the current local variables are stored, a new copy of the method is made (with new locals), and execution continues in the new copy.

    When you unwind the recursion, each of those stack frames and associated variables will need to be removed from the stack/heap and cleaned up, whether you exit normally or via an Exception.

    If you do exit via a reused Exception, you're misusing the functionality (as discussed above), and the benefits -- which will be slim, if any -- aren't worth it, because there are better options.

    If you're writing performance-critical code, you should use iteration, all in one method, instead of recursion, because it will perform better than even recursion with an Exception escape (probably in this case the cost is some readability.. recursion can be much more elegant).

    If it *isn't* performance-critical code, then you shouldn't be worried about unwinding the recursion. And I still don't like misusing basic Java concepts, but you could even throw a new Exception to escape w/o worrying about performance -- after all, 1 million new Exceptions in 10 seconds is still pretty darn fast when you're only talking about throwing one.

    -----
    Wow, I hope someone reads this... it was a lot of work to write out!

    --
    There are only 10 types of people: those who understand decimal, those who don't, and, uh, 8 other types I forget.