Reputation: 33
I have a long method that consists of about 350 lines. It's a routine method that another method uses in a cycle and it uses it many times (I'm talking here about numbers greater than 100 000).
The routine method has many conditional branches dependent on some computations. The computations in each branch look like this:
...
double cosValue = Math.cos(finalAngle);
double sinValue = Math.sin(finalAngle);
double baFirst_rotated_x = b.getA().getFirst().getxCentroid() * cosValue - b.getA().getFirst().getyCentroid() * sinValue;
...
So the heap space needed is not of a small size. The application works fine, but it's really slow.
Weird thing is that when I add a System call, for example, it runs much faster (like 10x faster). So when I put this
System.out.println("..."); or System.gc();
inside the routine method, it performs better. It looks like the System call somehow unpauses the application. Could someone please give me a hint why this might be happening? I know this is a very indefinite question and I apologise for that. I just think that there must be an explanation of what's actually happening and I can't move on without knowing it.
Upvotes: 2
Views: 3771
Reputation: 124646
To find the bottleneck, I recommend to try the VisualVM tool that is part of the SDK since Java 6u7. It's ridiculously easy to try, and it seriously helped me to achieve massive improvements on several occasions.
Here's what you do:
$JAVA_HOME/bin/jvisualm
Click on Snapshot and examine the threads: look at the Time and Time (CPU) columns. The interesting threads are the ones where Time (CPU) is bigger than 0 and less than Time. Usually you can ignore any AWT and Database threads, and probably other types too, depending on your application. That should leave you with just a few threads to look at. Expand them to drill down to the time consuming methods.
Using this technique, you will have the exact locations where your application is spending its time. And I must say, the results surprised me every time: even in a code full of inefficient code, there are usually just a few pain points that really matter, and they are usually not obvious at all, until you find them with at this tool. Give it a try!
The docs: http://docs.oracle.com/javase/7/docs/technotes/guides/visualvm/
Upvotes: 1