4

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.

Lii
  • 11,553
  • 8
  • 64
  • 88
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • Once I got the code below, all I had to was wait for the problem to happen again. Turns out I had an infinite loop where a method on the EDT was just placing another method on the EDT and so on and so on. The code pointed me right to the problem, and now it's fixed. Probably never would have found it without this. Thanks! – Erick Robertson Apr 04 '11 at 18:21

3 Answers3

9

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());
Community
  • 1
  • 1
rancidfishbreath
  • 3,944
  • 2
  • 30
  • 44
  • 1
    +1 Awesome. I pasted in the code I ended up using. Short and sweet, reports all events which take more than 50 ms to complete. – Erick Robertson Apr 04 '11 at 18:10
0

Make sure you are running only GUI related operations in EDT and there are no long running tasks in EDT. There is one awesome tool called SwingExplorer it has one feature to monitor EDT operations. Hope this will help.

Umesh K
  • 13,436
  • 25
  • 87
  • 129
0

I'd say it's likely this isn't actually something on the EDT but actually garbage collection.

Assuming that's the case I don't know of any solution I'm afraid. I've actually never written anything in Swing which didn't have this type of behavior occasionally.

To try & debug you could subclass EventQueue & install a new one using:

Toolkit.getDefaultToolkit().getSystemEventQueue().push(xxx);

This should allow you to at least see what events are being processed by the EDT.