I have an application that I'm building in Swing. It has a scrollable and zoomable chart component which I can pan and zoom in. The whole thing is smooth except that sometimes the UI will pause for about 750 ms and I don't know why. This doesn't always happen - but sometimes something happens in the application and it starts pausing like this once every 6-8 seconds.
It seems pretty clear that there's some event being placed on the EDT that's taking 750 ms or so to run, which shouldn't be happening.
How do I profile the EDT specifically like this? What I would really like to do is get something that will output to a log or System.out every time an event runs on the EDT with the total amount of time the event took. Is there a way to do this?
Or is there some tool that will do this for me and give me a log of everything that runs on the EDT and how long it takes?
I'd like to go through this log, look at everything that's taking a long time, and find the problem.
Take a look at this question. It describes a simple log on the EDT.
Create a class like this:
public class TimedEventQueue extends EventQueue {
@Override
protected void dispatchEvent(AWTEvent event) {
long startNano = System.nanoTime();
super.dispatchEvent(event);
long endNano = System.nanoTime();
if (endNano - startNano > 50000000)
System.out.println(((endNano - startNano) / 1000000)+"ms: "+event);
}
}
Then replace the default EventQueue with the custom class:
Toolkit.getDefaultToolkit().getSystemEventQueue().push(new TimedEventQueue());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With