Eye on Java performance Improvements 84
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."
"Level: Introductory" (Score:5, Informative)
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.
Re:"Level: Introductory" (Score:2)
Re:"Level: Introductory" (Score:3, Insightful)
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 applicat
Yeah, yeah (Score:3, Interesting)
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
No, no Re:Yeah, yeah (Score:3, Interesting)
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,
More comments (Score:5, Insightful)
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.
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!
Re:More comments (Score:2)
So what you are basically sa
Re:More comments (Score:2)
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
Re:More comments (Score:2)
Re:More comments (Score:1)
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.
Re:More comments (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).
Re:More comments (Score:2)
If you had to scan the headers of many XML documents, it might even make sense to create a workhorse SAXException for this
Re:More comments (Score:1)
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)
Re:More comments (Score:2)
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.
Re:No, no Re:Yeah, yeah (Score:1)
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 no
Re:No, no Re:Yeah, yeah (Score:2)
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.
How about (Score:2)
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?
Re:How about (Score:2)
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.
Re:How about (Score:2)
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?
Re:How about (Score:2)
Just because C++ did something one particular way, doesn't mean it's a good idea. In fact, quite the contrary in my experience.
Re:"Level: Introductory" (Score:3, Interesting)
One tip from Java Performance Tuning [oreilly.com] by Jack Shirazi [javaperfor...tuning.com] 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
Re:"Level: Introductory" (Score:2)
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 i
Re:"Level: Introductory" (Score:2, Insightful)
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 f
Re:"Level: Introductory" (Score:2)
Re:"Level: Introductory" (Score:3, Informative)
I know enough about compilation optimization to appreciate how c
Re:"Level: Introductory" (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 s
Optimize your way to ugly APIs (Score:5, 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.
Re:Optimize your way to ugly APIs (Score:3, Funny)
But, then, how can we have flamewars about each J2EE vs.
Re:Optimize your way to ugly APIs (Score:1)
Re:Optimize your way to ugly APIs (Score:1)
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 l
Quick summary (Score:1)
Article doesn't say much (Score:5, Informative)
Knowing when to optimize is more important than knowing how to optimize [ibm.com]
Urban performance legends [ibm.com]
Re:Question for Java and Perl developers (Score:1)
If you want others to be able to read your code, well
Re:Question for Java and Perl developers (Score:1, Insightful)
Re:Question for Java and Perl developers (Score:3, Informative)
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
Free Java Performance Tips (Score:4, Funny)
Larry
Re:Free Java Performance Tips (Score:2, Informative)
http://www-106.ibm.com/developerworks/java/libr
Re:Free Java Performance Tips (Score:2)
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
Re:Free Java Performance Tips (Score:3, Informative)
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 cl
- Try reading the article Re:Free Java Performance (Score:2)
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
Re:Free Java Performance Tips (Score:2)
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
The StringBuffer Myth (Score:2)
I really recommend this reading: The StringBuffer Myth [pastiche.org]
Re:The StringBuffer Myth (Score:2)
java and net (Score:3, Informative)
The result ?
"The Middleware Company has released a J2EE and
If performance is an issue, why use either? (Score:2)
Re:If performance is an issue, why use either? (Score:1, Informative)
Slow is relative (Score:3, Insightful)
Re:Slow is relative (Score:2, Insightful)
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
Re:Slow is relative (Score:5, Interesting)
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.
Re:Slow is relative (Score:1, Informative)
public native void slowInJava(...);
Care to try again?
Re:Slow is relative (Score:3, Informative)
Re:Slow is relative (Score:2)
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
Re:Slow is relative (Score:2, Insightful)
Re:Slow is relative (Score:1)
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.
Re:Slow is relative (Score:2)
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 th
Re:Slow is relative (Score:1)
"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
Quick useful user-level approach (Score:3, Informative)
* 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.
Re:Quick useful user-level approach (Score:2)
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 a
Re:Quick useful user-level approach (Score:2)
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.
Re:Quick useful user-level approach (Score:1)
1. Java and
2. Java and
Re:Quick useful user-level approach (Score:2)
2 is true, though I argue that it's more because the languages are bounds-checked than because they run in a VM.
Re:Quick useful user-level approach (Score:1)
Re:Quick useful user-level approach (Score:2, Interesting)
Re:Quick useful user-level approach (Score:2, Interesting)
Memory Isssue (Score:1, Interesting)
Can anyone explain why java uses so much memory or is it just bad programming by the contractor we used.
Re:Memory Isssue (Score:2)
Re:Memory Isssue (Score:2)
Re:Memory Isssue (Score:2)
http://www.javaworld.com/javaworld/javatips/jw-jav atip130.html [javaworld.com]
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.
Java is Fast Enough, Now Sort out Memory Usage (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 [rimuhosting.com] when each Servlet engine chews up 60MB of memory on my host servers.
FWIW, th
Re:Java is Fast Enough, Now Sort out Memory Usage (Score:2)
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.
Re:Java is Fast Enough, Now Sort out Memory Usage (Score:2)
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 libra
Performance, the one aspect? (Score:2)
Oh no, there are plenty more after that... :o)
Eternal Java/C++ flameware (Score:1)
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. Th