Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By pavon

Thread: Creating UI became very slow in Java 1.7

  1. #1
    pavon is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Creating UI became very slow in Java 1.7

    Some software I am maintaining has a JPanel containing a large number of JLabels. When run in JRE 1.6, the UI is created and displayed in a few seconds, but when run in JRE 1.7 it takes a couple *minutes* to display. When I profile or debug the code, it looks like the biggest change is that it is now spending a ton of time in Component.updateZOrder() which is called by Component.addNotify() which is recursively called on every component in the JFrame once it is made visible. In JRE 1.6 this particular function did not exist and the equivalent stacking order code in addNotify() doesn't seem to have any performance problems; in particular ContainerPeer.isRestackSupported() always returns false, so restack() is never called.

    Has anyone run into this problem before? Is there a work-around?

  2. #2
    pavon is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Creating UI became very slow in Java 1.7

    Here is a simple example that demonstrates the problem.
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    /**
     * This example starts immediately in Java 1.6 but takes several seconds to load in Java 1.7
     */
    public class ManyLabelExample extends JPanel {
        private static final int ROWS = 128;
        private static final int COLS = 512;
        private static final int ROW_HEIGHT = 12;
        private static final int COL_WIDTH = 4 * 6 + 20;
               
        public ManyLabelExample () {
            setLayout(null);            
            for (int r = 0; r < ROWS; r++) {
                for (int c = 0; c < COLS; c++ ) {
                    JLabel label = new JLabel();
                    label.setText(r +","+ c);
                    label.setBounds(c*COL_WIDTH, r*ROW_HEIGHT, COL_WIDTH, ROW_HEIGHT);
                    add(label);
                }
            }        
        }
        
        public static void main (String[] args) {
            JFrame frame = new JFrame();
            JPanel panel = new ManyLabelExample();
            frame.add(panel);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            long startTime = System.nanoTime();
            frame.setVisible(true);        
            System.out.println((System.nanoTime() - startTime)/1e9);
        }
    
    }
    I should note that once the UI is finally displayed the real application works and performs just fine in both 1.6 an 1.7.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Creating UI became very slow in Java 1.7

    All Swing constructors and methods (with a few exceptions) should be invoked on the EDT. Do you find a difference if you wrap the code in your main(...) method in a SwingUtilities#invokeLater(...) ?

    db

    edit No, it doesn't make any significant difference
    Last edited by DarrylBurke; 12-13-2012 at 04:54 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Creating UI became very slow in Java 1.7

    I think you need to adopt an approach that uses a renderer component: JTable or JList. For example, this uses a JList:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.*;
    
    public class ManyLabelExample extends JPanel {
    
      private static final int ROWS = 128;
      private static final int COLS = 512;
      private static final int ROW_HEIGHT = 12;
      private static final int COL_WIDTH = 4 * 6 + 20;
    
      public ManyLabelExample() {
        setLayout(new BorderLayout());
        DefaultListModel model = new DefaultListModel();
        for (int r = 0; r < ROWS; r++) {
          for (int c = 0; c < COLS; c++) {
            model.addElement(r + "," + c);
          }
        }
        JList list = new JList(model);
        list.setVisibleRowCount(ROWS);
        list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        list.setCellRenderer(new DefaultListCellRenderer() {
    
          final Dimension preferredSize = new Dimension(COL_WIDTH, ROW_HEIGHT);
    
          @Override
          public Component getListCellRendererComponent(JList list, Object value,
                  int index, boolean isSelected, boolean cellHasFocus) {
            return super.getListCellRendererComponent(list, value, index, false, false);
          }
    
          @Override
          public Dimension getPreferredSize() {
            return preferredSize;
          }
        });
        add(new JScrollPane(list));
      }
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            JFrame frame = new JFrame();
            JPanel panel = new ManyLabelExample();
            frame.add(panel);
            frame.setSize(800, 800);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            long startTime = System.nanoTime();
            frame.setVisible(true);
            System.out.println((System.nanoTime() - startTime) / 1e9);
          }
        });
      }
    }
    db
    Last edited by DarrylBurke; 12-13-2012 at 05:15 AM. Reason: Added getListCellRendererComponent override
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    pavon is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Creating UI became very slow in Java 1.7

    While the simple example I posted has everything aligned in a simple grid, this isn't the case with the real application, so JTable or JList won't work there. I have verified that using a LayoutManager has no effect on the start up performance and significantly decreases the run-time performance of the application, so that isn't a solution either. If push comes to shove, I could abandon using JLabels/JPanels altogether and draw everything directly onto a single canvas, although that is definitely not desirable; the code is much cleaner this way.

    And of course, we really don't want to completely rewrite a bunch of code that was working perfectly fine yesterday if we can instead find a simple work-around to this performance regression.
    Last edited by pavon; 12-13-2012 at 06:26 PM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Creating UI became very slow in Java 1.7

    The only workaround I can think of is to bundle a JRE 1.6 with your application, along with a launcher that ensures ot is used regardless of a later version being already installed. Or, and note that I've never tried anything remotely like that, maybe including a JRE6 rt.jar on the classpath would do it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    pavon is offline Member
    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Default Re: Creating UI became very slow in Java 1.7

    What I ended up doing to solve this without a complete rewrite was to group the display into a bunch of sub-panels. For example in the simple code I posted, putting all the JLabels in a single column into their own JPanel. That way instead of having (M*N)^2 Z-Order comparisons, you would end up with M*(N^2)+M^2 comparisons. Again, my real application wasn't arranged in a grid, but there was a natural way to hierarchically nest the components that drastically decreased the total number of components in each container, and made performance reasonable again.
    DarrylBurke likes this.

Similar Threads

  1. java event handling is too slow
    By yemista in forum AWT / Swing
    Replies: 4
    Last Post: 05-16-2012, 04:58 AM
  2. java 1.7.0 update 2 very slow?
    By Matthew Doucette in forum Advanced Java
    Replies: 0
    Last Post: 02-02-2012, 08:40 PM
  3. System Freeze/Slow-down using Java
    By Grunker in forum New To Java
    Replies: 12
    Last Post: 06-22-2011, 03:03 PM
  4. Why my java lan game programm slow!
    By arnelpogs in forum Networking
    Replies: 2
    Last Post: 12-07-2009, 04:00 PM
  5. Why JAVA is so slow?
    By aRTx in forum New To Java
    Replies: 2
    Last Post: 05-11-2009, 10:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •