Why does my Java application run so slowly in full

2019-01-28 23:46发布

问题:

I'm working on a Java program that is intended to run in full-screen mode. It uses numerous of customized Swing components and Java2D-drawn components that need to be updated and repainted several times a second. And it was working relatively fine on my underpowered work PC.

But then I tried it out at home on my much more powerful PC. And it ran noticeably slower. Triggering an event that should have instantly updated about 20 different screen elements instead caused an effect where each element seemed to take at least a quarter second each to repaint itself. So instead of instantaneous changes it was taking 5 seconds to complete each screen change.

I thought that maybe I was trying to repaint too often or in the wrong manner. But after experimenting a bit with other ideas, on a hunch I let the application start up in a windowed mode instead of in full-screen mode. And with that one change, everything started to work perfectly fast and smooth.

So I suppose there are really two issues here: Why does full-screen mode cause this problem? And why is it only causing this problem on my faster computer? I do suspect there's an OS-related bug. My slow work computer is Windows XP while the home one is Windows 7. I saw in other threads that Aero on Win7 can cause Java speed issues so I tried disabling it. That did cause a small speed improvement but it still wasn't as smooth as when I ran in windowed mode. Has anyone else had performance issues running full-screen Java apps on Win7? And if so, is there a work around?

回答1:

I found the solution. I stumbled upon Oracle's Java System properties page (http://docs.oracle.com/javase/1.5.0/docs/guide/2d/flags.html) and figured there might be something useful there. While using the trace command I noticed that there were a lot of Direct3D references. Since nothing in my app is 3D, I thought that strange. So I added -Dsun.java2d.d3d=false to my Java command line. As the page says that option will "turn off the Java 2D system's use of Direct3D". And it worked like a charm. Now it runs perfectly smoothly on my Windows 7 machine in full screen mode.