Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4869906

Each instance of GridBagLayout unnecessarily leaks 12K of memory

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.4.1
    • client-libs
    • x86
    • windows_nt



      Name: jk109818 Date: 05/27/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
      Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)


      FULL OS VERSION :
      Windows NT Version 4.0

      A DESCRIPTION OF THE PROBLEM :
      GridBagLayout class in java.awt contains an inner class GridBagLayoutInfo
      which in turn maintains 4 arrays of int and double values:
      minWidth, minHeight, wieghtX, and weightY.

      Each new instance of GridBagLayout creates a new instance of GridBagLayoutInfo
      which allocates 12288 = 12Kbytes of memory in these 4 arrays before anything (component) in put into the layout manager. For a single instance of GridBagLayout 12K seems like a small number but given a large number of
      GridBagLayout instances in a sophisticated application that is memory sensitive, these number add up quickly.

      class GridBagLayoutInfo implements java.io.Serializable {
        int width, height; /* number of cells horizontally, vertically */
        int startx, starty; /* starting point for layout */
        int minWidth[]; /* largest minWidth in each column */
        int minHeight[]; /* largest minHeight in each row */
        double weightX[]; /* largest weight in each column */
        double weightY[]; /* largest weight in each row */

        GridBagLayoutInfo () {
          minWidth = new int[GridBagLayout.MAXGRIDSIZE];
          minHeight = new int[GridBagLayout.MAXGRIDSIZE];
          weightX = new double[GridBagLayout.MAXGRIDSIZE];
          weightY = new double[GridBagLayout.MAXGRIDSIZE];
        }
      }

      GridBagLayout.MAXGRIDSIZE = 512

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a JPanel and set a new instance of GridBagLayout into it. It will
      automatically allocate 12K of memory even if the container is empty.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Memory is allocated on as needed basis.
      ACTUAL -
      All memory that wil ever be needed is allocated at instantiation.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*
      import java.awt.*;

      public class test
      {

      public static void main(String [] args)
      {
        JFrame win = new JFrame();
        win.getContentPane().add(new JPanel(new GridBagLayout()));
        win.pack();
        win.setVisible(true);
      }

      }
      ---------- END SOURCE ----------
      (Review ID: 185960)
      ======================================================================

            bchristi Brent Christian
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: