views:

95

answers:

1

I have a java application I wrote that loads up a TTF font and uses the drawString method from Graphics2D. This gets called every 50ms with the x and y positions changing each time to make the text move. When I run the program on Windows, I get 0-1% CPU usage, but on Mac I get about 75% usage. This Windows machine does have a better CPU but there's no way there should be that big of a difference. I think it has to do with Hardware Acceleration and I want to know how enable it. I found some Mac specific Java properties, but none of them lowered my CPU usage. Any ideas how to increase Java 2D performance on OS X? Thanks.

EDIT1: I thought that these properties would help but they didn't.

System.setProperty("sun.java2d.opengl", "true"); System.setProperty("apple.awt.graphics.UseQuartz","true"); System.setProperty("apple.awt.graphics.EnableQ2DX","true");

EDIT2: You can download the project source and byte code here: http://drop.io/ExampleScreenSaver

+1  A: 

Run the profiler in "jvisualvm" to identify where the time goes.

Thorbjørn Ravn Andersen
I ran jvisualvm and used the profiler on the CPU. It's exactly what I expected: the draw method self time is 89% and the next highest is the paint method (I used @override) is the next highest at 7%. This is expected because the I have a swing timer that calls the paint method every 30ms, and for each call to the paint method the draw method is called on every object to make them move a couple pixels. But back to my original question, how can I make this run like it does on Windows (with very low CPU usage)?
styfle
Can you make a small self-contained example I can play with on my Mac?
Thorbjørn Ravn Andersen
Ok I made a small example with just the letter Z moving across the screen. The results are identical: 70-90% CPU usage on the Mac and 0-2% CPU usage on Windows XP.
styfle
Post the example so we can look at what might be wrong.
I82Much
I had a look at the code in the example linked to in the question. I see the same behaviour as you do on a Core 2 Duo MacBook Pro. Various hints to the JVM did not improve performance. My guess right now is that JVM has to convert the pixels sent to the screen hence slowing down.
Thorbjørn Ravn Andersen
@I82Much: I put the example in the original post.@Thorbjørn Ravn Andersen: Thanks for looking. I just need to find a way to force Java to use the video card to take the load off the CPU.
styfle
Sorry I couldn't help. I am not that much into Graphics2D.
Thorbjørn Ravn Andersen