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."

84 comments

  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 leifm · · Score: 1

      Introductory is even pushing it. I took one Java class, and I am by no means a Java expert and I found this article lacking. I hadn't heard any of these tips before, and then about the time I get into the article it ends.

      --

      "Windows Me offers tremendous reliability and stability improvements..." -- Paul Thurott
    2. 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!"
    3. 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
    4. Re:"Level: Introductory" by WolfWithoutAClause · · Score: 1
      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).

      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.

      --

      -WolfWithoutAClause

      "Gravity is only a theory, not a fact!"
    5. 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
    6. Re:"Level: Introductory" by WolfWithoutAClause · · Score: 1

      Actually I'm pretty sure the range checking is best done by Hotspot; the compiler doesn't get to see what calls what, Hotspot sees what functions get called by what other functions and is able to do a much better job of that kind of optimisation- it can even optimise into libraries, which the compiler never normally sees at all. Mind you, this is partly theoretical on my part, I best know about an experimental Sun language called Self which definitely works this way; but Self is what the Hotspot technology is based upon, and there are some wrinkles in Java that may preclude this technique from working quite so well.

      --

      -WolfWithoutAClause

      "Gravity is only a theory, not a fact!"
    7. 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
    8. Re:"Level: Introductory" by cakoose · · Score: 1

      One problem with "javac" doing range checking may be that the JRE doesn't trust .class files. Everything is verified first. Unless it's easy to verify a correct bounds-check removal, the runtime compiler will have to duplicate the original analysis anyway. Maybe the bytecode could be extended to allow for compilation hints.

      Instead, "javac" could structure the output in such a way that it is trivial for the HotSpot compiler to figure out that the check can be eliminated. I believe HDL compilers do something similar.

  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?!?!?

    2. Re:Optimize your way to ugly APIs by I8TheWorm · · Score: 1

      I agree to the extent that exceptions should rarely be ignored, unless that's somehow built into the functionality of your app. Teaching someone bad coding practices because of a limitation in the development platform is bad advice.

      --
      Saying Android is a family of phones is akin to saying Linux is a family of PCs.
    3. Re:Optimize your way to ugly APIs by Anonymous Coward · · Score: 0

      Or...

      Why not just have a constructor for the base Exception object that has a stack trace depth option. Then the stack trace could be presented relative to where the exception is generated. I'd say 90% of the time I don't look beyond the first line of the stack trace anyways.

      In your example, wouldn't:

      Exception in thread "main" java.io.FileNotFoundException
      at java.io.File.getLastModified(compiled code)
      at my.broken.Code.thatDoesNotCallExistsBeforehand(com piled code)

      contain every bit of useful information that a full stack trace would contain?

      There'd be much less performance hit due to exception generation since you're not capturing the entire stack and you'd get everything you need from Exception handling.

    4. Re:Optimize your way to ugly APIs by Nevyn · · Score: 1
      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?

      Oh, yeh that's right, because files not existing is really "exceptional" isn't it? This is an anoying wart for FileInputStream IMO, because now instead of doing all your relevant work in the call (Ie. does the file exist) you have to mix into all the real exceptional conditions like a NULL exception, out of memory, API errors like IllegalArgumentException (and even other non-exceptional things like the file being a directory).

      Although I'll admit having both an exceptions API and a none exceptions API is probably worse than just either. EOFException is just as bad in that regard.

      --
      ustr: Managed string API with ave. 44% overhead over strdup(), for 0-20B
  3. Quick summary by addaon · · Score: 0

    Quick summary: Our software is good, thrashing is bad, and exceptions are still screwy.

    --

    I've had this sig for three days.
  4. 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 ...
  5. Re:Question for Java and Perl developers by cxvx · · Score: 1

    Perl if you never have to look at it again.
    If you want others to be able to read your code, well ... :)

    --
    If only I could come up with a good sig ...
  6. 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)

    3. Re:Free Java Performance Tips by LarryRiedel · · Score: 1

      looks like you need to read this http://www-106.ibm.com/developerworks/java/library /j-jtp04223.html

      I do not so much think that excessive use of synchronization must have a significant performance impact as that it will have a negative performance impact. Nevertheless I do disagree with that article to the extent it suggests that synchronization and object creation/destruction do not significantly affect application performance.

      Larry

    4. Re:Free Java Performance Tips by (H)elix1 · · Score: 1

      Try never to use java.lang.String
      One of the sins of my past was concatenating Strings together rather than using a StringBuffer. For example...

      String str = "foo"; str+="bar";

      rather than...

      StringBuffer str = new StringBuffer("foo"); str.append("bar");

      Mostly in building HTML in Servlets. I had not even thought about what was happening under the covers, and not thinking is what will bite you in the ass with any language. I was shocked to see how much faster StringBuffer concatenation was - but did not believe one of my co-workers until I ran some quick benchmarks.

      An older, but still valid book is Practical Java by Peter Haggar. A must read if you are looking for this type of info.

    5. Re:Free Java Performance Tips by Anonymous Coward · · Score: 0
      How about:
      String str = "foobar";
      (Just kidding)
    6. Re:Free Java Performance Tips by Anonymous Coward · · Score: 0

      Didn't know this. (since I'm a newb). Thanks.

  7. 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. "

  8. 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 Anonymous Coward · · Score: 1, Informative

      *cough* JNI *cough*

      public native void slowInJava(...);

      Care to try again?

    5. Re:Slow is relative by Anonymous Coward · · Score: 0

      But I have seen more than one article by a Java advocate making the strange claim that Java is faster than C.

      Granted that in most instances, C will be faster. However it isn't out of the realm of possibility that Java will be faster in certain circumstances. The use of a VM allows all sorts of optimizations based on runtime profiling that cannot be done for C. For processes that run for extended periods of time, it's not inconceivable that Java would outperform C. These circumstances are pretty much limitted to very specific applications, but they do exist.

    6. 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. Re:Slow is relative by AndersDahlberg · · Score: 1

      But I have seen more than one article by a Java advocate making the strange claim that Java is faster than C. Well, maybe because it's a valid claim: in almost every case I've seen of fft implementations in java and c - java is faster (since 1.3.1). Thus, if you're only interested in that case java is actaully faster ;)

      yes, in the majority of cases c is faster - but not by a very wide margin (depending on the actual case of course) and then you have the occasional situation where the java vm is faster.

    8. Re:Slow is relative by Arandir · · Score: 1

      In some cases, yes, java is faster. And you named one of them: loop intensive math, where a runtime optimizer can do its best work.

      But to say C is not faster by a wide margin in most cases is ludicrous. It's precisely this attitude that caused this entire article to begin with. All the Java people say Java is fast, but keep wondering why it has an "unfair" reputation for sluggish performance. If Java is just as fast, then show me the Java office suite that was promised me ten years ago. I don't need one that's MS compatible, just one that works and is responsive enough to be usable.

      --
      A Government Is a Body of People, Usually Notably Ungoverned
    9. Re:Slow is relative by AndersDahlberg · · Score: 1

      "Yeah, and because there are no successful office suits written in java that means java are slower than C/C++?"
      "So, can you show me any office suits written in assembly or machine code that are used successfully on todays computers, or do you mean that micro code is slower than C/C++?"
      "Have you stopped beating your wife?"
      Notice the similarity in the questions above? :)
      MUUUUU! (logic error)

      And yes, I still claim java not to be slower by a *wide margin* than C on most stuff that are programmed in an object oriented notion (micro-benchmarks/1000line wonder + virtual machine = all bets are off...) - the bigger and more object oriented code, the better java performs (or any virtual machine based platform which can dynamically optimize hot spots) whereas C/C++ is just the opposite :)

      This is also one of the reason java is used a lot on the server side - that's where performance, ease of use, maintainability and scalability is really important.

      Notice that I'm not saying that java is generally faster. Just that it's not generally slower by a *wide margin* which is a BIG difference.

      (i.e. if you do a straight port of a procedural architected C program to java you can have a laugh at "how sloooow java is" - but then I would call any person doing that stupid in the first place, "use what works", if C/C++ is ok - then why not use it :)

    10. Re:Slow is relative by Golthar · · Score: 1

      So just because people look down on it, you refuse to use JNI?
      I will use JNI whenever I need to and if portability is possible, you need to provide a base fall through Java solution (for example if you want to do some hardcore number crunching, you make an extra version just in Java)

      I find JNI relatively easy to use when you need it

  9. 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 WolfWithoutAClause · · Score: 1
      * Java still uses a lot more memory and cycles than C/Pascal/C++/etc.

      Depending on what you mean by 'a lot more cycles' this may be either true or false. A well designed Java program is a percentage slower. Depending on whether you are spending longer waiting for the program to run or longer writing the program that tells you whether you should be writing in Java or C. But some things like floating point in Java can be as fast as C in some cases, so it's not clearcut.

      Generally, if there's a Java program and a C equivalent, you want to use the C equivalent.

      Perhaps, but some studies have shown that the C equivalent is twice as expensive to develop, and so may well cost twice as much to buy, but probably isn't twice as good.

      --

      -WolfWithoutAClause

      "Gravity is only a theory, not a fact!"
    2. Re:Quick useful user-level approach by Anonymous Coward · · Score: 0

      Perhaps, but some studies have shown that the C equivalent is twice as expensive to develop, and so may well cost twice as much to buy, but probably isn't twice as good.

      That seems to be an odd thing to worry about. I mean so what if the development is more expense, if the end product requires performance; it seems as though the upfront cost of development is justified. Otherwise the next release of the product inevitably must address performance (if even possible at that point), so you may get it out the door faster but it's just 'sooner to market, sooner to rewrite'.

    3. Re:Quick useful user-level approach by timiscool999 · · Score: 1

      Virtual machine type of controlled execution environments like Java or .NET have two advantages over C that will probably never be matched:

      1. Java and .NET can compile code optimized for your system on the fly. Got a nice P4-3.06 Ghz. Great, but most code is compiled for the lowest common denominator CPU so that it will run on all systems. Java's HotSpot can optimize the bytecode to compile to native code to take full advantage of your system.

      2. Java and .NET make it almost impossible to exploit a buffer overflow in your code. First of all, the environments try to protect your code from having buffer overflows, and if they do exist and they are exploited, they run in a "sandbox"-like environment where it is extremely difficult for a malicious user to do anything to your system. There are libraries for C to help prevent buffer overflows, but they aren't as widely used and they still don't have the benefits of a sandbox execution environment.

    4. Re:Quick useful user-level approach by timiscool999 · · Score: 1

      Also let's not forget that you can write poorly written C (which isn't all that hard), and you can write beautiful Java, and the latter could be much faster and easier to maintain than your crap C program.

    5. 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.

    6. Re:Quick useful user-level approach by 0x0d0a · · Score: 1

      1 is a theoretical advantage that has never managed to pull a VM-based language up to speed.

      2 is true, though I argue that it's more because the languages are bounds-checked than because they run in a VM.

    7. Re:Quick useful user-level approach by WolfWithoutAClause · · Score: 1
      I mean so what if the development is more expense

      The development cost divided by the number of units sold is the absolute minimum price to break even. If the development cost goes too high- you don't break even.

      --

      -WolfWithoutAClause

      "Gravity is only a theory, not a fact!"
    8. 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.

  10. Memory Isssue by Slick_Snake · · Score: 1, Interesting

    At my work we have a program that is needed for information gathering. It was a custom job done by an outside agency. They chose Java as their language of choice. The program runs lots of processes and many threads. The average thread size is something like 52 MB. The total memory use of this program is something like 1.5 GB with about 500 MB of that resident in memory.
    Can anyone explain why java uses so much memory or is it just bad programming by the contractor we used.

    1. Re:Memory Isssue by kevin_conaway · · Score: 1

      Could be that they started the app with special parameters to the JVM regarding heapsize, garbage collection etc. Maybe they overshot this?

    2. Re:Memory Isssue by 0x0d0a · · Score: 1

      Java uses a lot of memory. Each object, no matter how small, needs to have associated type information, GC information, probably permissions, etc, etc, etc.

    3. Re:Memory Isssue by Admiral+Burrito · · Score: 1

      http://www.javaworld.com/javaworld/javatips/jw-jav atip130.html

      An object with no fields requires 8 bytes of memory. I would guess that four bytes are a reference to the class, and the other four are related to synchronization.

  11. Re:Question for Java and Perl developers by Slick_Snake · · Score: 0

    Python is you want to be able to read it later.
    There is a great example in "Programming Python 2nd Ed." by O'reilly. You can make it a Threaded or Forked server with just the examples in the book.

  12. Re:Question for Java and Perl developers by v_1matst · · Score: 1, Insightful

    ...or C. If you want to hack something together, use Perl. If you want lots of reusable code/objects/classes/whatever use java. If you want whatever your working on to be stable, tight and fast then use C for a small project like this...

  13. 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.
  14. - Try reading the article Re:Free Java Performance by WolfWithoutAClause · · Score: 1
    * Try never to use java.lang.String.

    This is covered in the article. You're talking utter nonsense.

    * Try not to use synchronized.

    Yeah right. This is also covered in the article. Hey, have you read the article?

    * Try not to create objects.

    Guess not since that was also covered.

    Do you know how ridiculous that would be? An object oriented language where you don't create objects? Sure, you can make everything 'static', but what are you going to do about arrays, in Java they are objects? Nearly everything in Java involves objects. Get real!

    You should almost never try to not create objects. You may want to, at most, minimise the rate of creation.

    --

    -WolfWithoutAClause

    "Gravity is only a theory, not a fact!"
  15. 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.
  16. If performance is an issue, why use either? by 0x0d0a · · Score: 1

    If performance is an issue, why would anyone *use* J2EE *or* .NET?

    1. Re:If performance is an issue, why use either? by Anonymous Coward · · Score: 1, Informative

      because writing web applications in C/C++ takes too long.

  17. 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!"
    1. Re:No, no Re:Yeah, yeah by dkf · · Score: 1
      (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!)
      Mind you, if you've got code that throws a million exceptions in a day (I'd start worrying if it happened that often in a month), you've probably got a good reason to try to use some other error handling mechanism. Exceptions should be for when the shit hits the fan, not for dropping out of a loop where the programmer was too lazy to provide a normal exit mechanism.
      --
      "Little does he know, but there is no 'I' in 'Idiot'!"
    2. Re:No, no Re:Yeah, yeah by WolfWithoutAClause · · Score: 1
      It's never necessary to use exceptions; but it's nearly always a good idea.

      I figure when I'm being paid to do some work; it's more or less my job to do it the quick/short/lazy way rather than the long way (provided it doesn't do horrible things to the code maintenance or otherwise make the code unnecessarily brittle to changes.)

      Doing otherwise does nasty things to the profitability of the company- and I and the rest of the people I'm working with can end up out of a job.

      --

      -WolfWithoutAClause

      "Gravity is only a theory, not a fact!"
  18. 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.

    1. Re:Java is Fast Enough, Now Sort out Memory Usage by j3110 · · Score: 1

      Eclipse will run in 4M of ram on my machine. I have to point this out every time Java is mentioned, and it's getting old.

      Try this:
      java -Xmx8M -cp startup.jar org.eclipse.core.launcher.Main

      That will limit Java's heap size to 8M. It will start and run just fine, but windows will say that you are using somewhere around 30-60M. Memory reporting under windows usually is flawed when it comes to Java. Java runs on embedded systems (I put a neural net on one once). I know it doesn't use that much RAM or CPU.

      It did use a lot of CPU in my case because I was using floating point. Java forces IEEE compliant floating point math. This means don't try to test the FLOPS of a Java system :) If you have something that requires floating point math, make a C library for your floating point intensive sections. The embedded system didn't have an FPU anyhow, so it probably wouldn't have amounted to any significant gains if it wasn't constrained to IEEE.

      On another side topic, are there any Servlet/JSP/J2EE containers/servers that don't require seperate VM's?

      My idea recently was to implement your own security archeticture and Vhosting in Jetty, and launch them all in the same VM. This will let them share stale objects and heap space without compromising security. You couldn't hook it up to Apache like Tomcat, but Jetty is faster and smaller than Tomcat. The best solution is in the works, but it keeps getting pushed off. Shared VM will do this automatically for you(except for the VHost issue obviously). Sharing the heap and JIT of core objects will help performance and memory usage by a pretty significant amount.

      I hear that Orion my do all this for you, but I've not seen it myself... who has any experience with this?

      --
      Karma Clown
    2. Re:Java is Fast Enough, Now Sort out Memory Usage by Anonymous Coward · · Score: 0

      It will start and run just fine, but windows will say that you are using somewhere around 30-60M. Memory reporting under windows usually is flawed when it comes to Java
      </quote>

      Windows' report memory usage correctly. 8m is the size of your heap, it doesn't include the size of the java runtime which is growing with every release. Sun should move some of the APIs in the core rt.jar to extensions lib/ext (ie. xml).

      <quote>
      Sharing the heap and JIT of core objects will help performance and memory usage by a pretty significant amount.
      </quote>

      Apple's already doing this, Sun will do it in 1.5

    3. Re:Java is Fast Enough, Now Sort out Memory Usage by j3110 · · Score: 1

      All objects are created on the heap, even all of the API's. Every API that eclipse loads fits in that 16M. (I installed too many plugins and upgraded Eclipse such that now it requires around 16M.)

      I will have to write a simple program that catches the out of memory exception and dumps the heap usage and waits for me to check windows memory reporting before I can confirm that it is actually the Java executable and dll's that are taking up nearly 30M. I just don't believe that though. Only the native libraries (maybe SWT is in that 30M), windows dll's, the interpreter, and the jitc is in their. I have a hard time believing that it can take up that much space.

      I hope it shows up in 1.5, but I thought it got pushed of even further. Have you found any information from SUN that actually promises to do this in 1.5?

      --
      Karma Clown
  19. 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.
    1. Re:More comments by WolfWithoutAClause · · Score: 1
      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.

      So what you are basically saying is 'nobody does it that way' so 'nobody ought to do it that way'?

      To begin with, recursion isn't a very efficient process in Java.

      It's exactly the same as any Java function call. If you prefer, consider a program with 10 functions that call each other in turn and you realise you need to get information or a signal from the inner one all the way to the outer one; you may have to change hundreds of parts of the code. Using an exception can help localise the changes necessary. That's really what they are for, no?

      If you do exit via a reused Exception

      If you mean that the user gets to see a reused Exception, then don't do that. You should only use reused exceptions where you know you are going to catch them- that's the whole point, it's purely an internal signalling device that is lightweight from a source code point of view. But you must comment and document it very well because it is uncommon.

      If you're writing performance-critical code, you should use iteration, all in one method, instead of recursion,

      Yeah, well, you've clearly never written many tree search algorithms. Doing such a thing is often very horrible.

      And I still don't like misusing basic Java concepts

      It's not a basic Java concept, it's an advanced Java concept, and there is no misuse.

      after all, 1 million new Exceptions in 10 seconds is still pretty darn fast when you're only talking about throwing one.

      Yes, but that's a best case, the stack size in the benchmark was tiny; the reuse case stays much the same speed whilst the conventional approach slows down. It wouldn't surprise me at all to see a normal throw/catch taking milliseconds on a big system.

      --

      -WolfWithoutAClause

      "Gravity is only a theory, not a fact!"
    2. Re:More comments by MillionthMonkey · · Score: 1

      There are cases involving callback mechanisms where throwing an Exception is the only appropriate way to stop some process that is not under the control of your own code. Say you're parsing an XML document, and you are only interested in the information in the header elements. An exception is a good way to stop a SAX parser from reading past the header information as it parses the file.
      If you had to scan the headers of many XML documents, it might even make sense to create a workhorse SAXException for this purpose.

    3. Re:More comments by jtheory · · Score: 1

      I think we're actually arguing pretty close to the same thing here, for the important bits.

      But you must comment and document it very well because it is uncommon.

      This (in your words) is a big part of my main point -- if you're going to do something that might be hard for the reader to grasp, you should be aware of this cost.

      So what you are basically saying is 'nobody does it that way' so 'nobody ought to do it that way'?

      I wouldn't say "nobody should do it" -- I'm saying pretty much what you're saying -- if it's uncommon I should comment it really well, and if I can avoid it easily, I should. I even mentioned that implementing recursion as iteration can have a cost of its own -- and if that cost were too large, it would outweigh the cost of possible confusion because of the uncommon Exception use.

      I'll agree that there may be a few cases where an advanced programmer might need to reuse Exceptions to solve a sticky problem. But I still don't think that Exception re-use should be in a list of suggested optimizations for the average programmer. I've personally never had a need for this optimization (of course YMMV). Yes, I've used a SAXException to escape XML processing (like another poster mentioned), but it was never a performance hotspot (only needed to escape if the input was malformed), so there was no call for exception reuse even then.

      As far as recursion efficiency -- you will read in plenty of optimization guides that calling lots of methods in a tight loop won't scale particularly well for speed-sensitive code. If you really need to optimize a chunk of code, you will avoid recursion if possible, keep it all in one ugly method, use bitshifting instead of standard arithmetic where possible, etc. etc., and have a LOT of comments all over it so that any maintainer will have some hope of surviving in the mess. And before you did any of this you'd better have your performance profiler out to see how much each optimization actually gained you, because each one will have a big cost for maintainability that might not be worth it.

      Again, understand that I've never had to do this kind of thing (in 7 years of Java coding), and I don't even talk about these kinds of optimizations when I'm helping someone learn the language. It's much more common that the database queries need optimzation, in my experience. Optimization tachniques can be dangerous things...

      I'm not concerned about you reusing an Exception when you're optimizing some frequently used, high-performance tree search algorithm where this special use is well-commented and entirely contained. What I worry about is the developer who sees the technique in a book, and does his whole project that way because it's a cool idea and he's proud to show off his new Java knowledge.

      There are a lot of general approaches to design that will help us come up with efficient programs, that don't include possibly costly optimizations. There is a place for the more "dangerous" ones, but only after careful consideration, and when the performance of the application needs it.

      I think you'd agree with that at a high level....

      I'll agree to disagree on the idea of using pre-constructed Exceptions as a "an internal signalling device that is lightweight". Personally, I'd only use that kind of construct in an extreme case where it was required for optimization.

      Consider a program with 10 functions that call each other in turn and you realise you need to get information or a signal from the inner one all the way to the outer one; you may have to change hundreds of parts of the code. Using an exception can help localise the changes necessary.

      When I realize that I've screwed up an interface (and I need more info out of those objects than I'd realized), yeah, I change all that code. This kind of refactoring is surprisingly quick and safe now with IDEs like idea and eclipse. Besides, before I could throw that exception all the way out, I'd have to r

      --
      There are only 10 types of people: those who understand decimal, those who don't, and, uh, 8 other types I forget.
    4. Re:More comments by WolfWithoutAClause · · Score: 1

      Whatever.

      --

      -WolfWithoutAClause

      "Gravity is only a theory, not a fact!"
    5. Re:More comments by cakoose · · Score: 1
      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.

      A recursive call is just as fast as any other procedure call. You make it seem like a whole bunch of extra work is being done. "Storing" current local variables and making "a new copy of the method" often involves merely incrementing a register (the stack pointer). Function calls are usually slowed down because of parameters and storing things onto the stack.

    6. Re:More comments by qui_tollis · · Score: 1

      It's [recursion] exactly the same as any Java function call.

      True, but treating recursive calls in the same way as any other function is inefficient. Other languages (e.g. lisp) treat tail-recursive calls differently, removing the need to clear up the stack when the function terminates.

    7. Re:More comments by jtheory · · Score: 1

      A recursive call is just as fast as any other procedure call.

      True; I should have pointed that out.

      My point was that if we are talking about maximum optimization of an area of code, any construct that will call lots of methods in a tight loop isn't ideal, because of that overhead.

      Note: you'd only even consider this if you're trying to eek out every last drop of performance... don't screw up your code's maintainability for a gain that no one will notice.

      --
      There are only 10 types of people: those who understand decimal, those who don't, and, uh, 8 other types I forget.
    8. Re:More comments by cakoose · · Score: 1

      This can also be done for regular tail calls (not just for recursive calls) though it may be more work (depending on the underlying architecture).

  20. Why do WebLogic/Sphere use so much memory? by Anonymous Coward · · Score: 0

    I'm a Java newbie who has tried IBM's WebSphere and BEA's WebLogic. My box has 512M, but both of these IDEs use all 512k and take minutes to load a simple "hello world" app. "javaw.exe" seems to take all of the memory and CPU. I am doing something dumb or do I really need more than 512M?

  21. Performance, the one aspect? by Anonymous+Brave+Guy · · Score: 1
    Performance. It's the one aspect of the Java platform that continually takes abuse.

    Oh no, there are plenty more after that... :o)

    --
    If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  22. Eternal Java/C++ flameware by RisingSon · · Score: 1
    The Java/C++ flameware will burn eternally because there will be areses like myself that hear "Java" and think - "Those young punks like to overwrite they're array bounds. Thats going to cost them...Oh yes...that will cost them...Are they scared of my C pointers? I deal only in System V shared memory. I write my own hash tables. Of course your new fangled Java crap is slower than my code."

    I dunno...there can be enough hate between C and C++ that I don't think Java and C will ever walk hand in hand. Therefore, Java will always be slammed for its speed.

    1. Re:Eternal Java/C++ flameware by Anonymous Coward · · Score: 0

      C/C++ could just as easily be slammed for its unwieldiness, lack of prebuilt high level components (e.g. String methods in Java), and the ease with which it allows you to shoot yourself in the foot in various ways. However, Java programmers are not so insecure that they /need/ to beat up on C for these things.
      Java programmers are typically secure and comfortable with the language's position. Insecure C programmers are the only ones slamming Java for the speed issue. Which as we see from the optimization articles above, is really not much of an issue anyway.

  23. The StringBuffer Myth by red_gnom · · Score: 1

    I really recommend this reading: The StringBuffer Myth

    1. Re:The StringBuffer Myth by (H)elix1 · · Score: 1

      Interesting read. With all things, there is a balance between performance and clarity. The Strings I was concatenating was usually in a loop, so that may have skewed my results. In my case, it was a substantial gain. YMMV

  24. How about by iamacat · · Score: 1

    bully Sun to add -Xnotrace option to disable stack trace in exceptions and make them efficient? Its nonsense to change class design because some feature that could be fast happens to be slow.

    Consider someone writting a low-level library that treats some condition (say, a math overflow) as an error and throws exception. Later someone else writes a program that generates a lot of overflows and handles them. Do you really want them to rewrite perfectly working code?

    1. Re:How about by WolfWithoutAClause · · Score: 1
      bully Sun to add -Xnotrace option to disable stack trace in exceptions and make them efficient?

      Nasty! So it throws an exception and you don't know where it came from; ever? YUCK .

      Its nonsense to change class design because some feature that could be fast happens to be slow.

      No, or yes, depending on what you mean here. The right thing to do is to create a new exception type that doesn't fill in the exceptions- for this kind of use.

      --

      -WolfWithoutAClause

      "Gravity is only a theory, not a fact!"
    2. Re:How about by iamacat · · Score: 1

      Nasty! So it throws an exception and you don't know where it came from; ever?

      No, you turn off -Xnotrace and next time you see the complete call stack. Actually, C++ exceptions never record the call stack and it doesn't cause many problems. Why would you be opposed to the configuration option, turned on by the user of the application, when s\he is more interested in performance then debugging?

    3. Re:How about by WolfWithoutAClause · · Score: 1
      Because you turn it off for performance reasons, ship it, and get horrible information back from the field about what messed up IRL?

      Just because C++ did something one particular way, doesn't mean it's a good idea. In fact, quite the contrary in my experience.

      --

      -WolfWithoutAClause

      "Gravity is only a theory, not a fact!"