JDK-4869906 : Each instance of GridBagLayout unnecessarily leaks 12K of memory
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.1
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2003-05-27
  • Updated: 2003-05-28
  • Resolved: 2003-05-28
Related Reports
Duplicate :  
Description

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) 
======================================================================

Comments
EVALUATION Sounds like the same issue as 4254022. ###@###.### 2003-05-27 Quite right - this is a duplicate of 4254022. ###@###.### 2003-05-28
27-05-2003