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

Swing memory leak

XMLWordPrintable

    • 03
    • x86
    • windows_nt

        Platform: WindowsNT4.0
        JDK: JDK1.3.1

        Sample program don't release memory. This promblem was checked by using
        OptimizeIt. After sample program started, please click the diplayed box. One
        object don't release memory when other box was selected. After many clicking,
        one object has so many resouce.
        Work around of similar bug "4396433" is applied, but improvement didn't show
        up.

        --------------------------------------------------------------------
        import java.awt.*;
        import java.awt.event.*;
        import java.util.*;
        import javax.swing.*;
        import javax.swing.event.*;
        import javax.swing.tree.*;

        public class TestAction extends JFrame implements TreeSelectionListener{
            Runtime runtime = Runtime.getRuntime();
            JMenu fileMenu = null;
            MyNode root = null;
            DefaultTreeModel treeModel = null;
            JTree tree = null;

            public TestAction(){
        super("TestAction");

        JMenuBar menuBar = new JMenuBar();
        fileMenu = new JMenu("FILE");
        menuBar.add(fileMenu);

        setJMenuBar(menuBar);

        root = new MyNode();
        for(int i = 0; i < 20; i++)
        root.add(new MyNode());

        treeModel = new DefaultTreeModel(root);
        tree = new JTree(treeModel);
        tree.addTreeSelectionListener(this);
        tree.setSelectionRow(0);

        Container content = getContentPane();
        content.setLayout(new BorderLayout());
        content.add(tree);

        addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
        System.exit(0);
        }
        });

        pack();
        setVisible(true);
            }

            public void valueChanged(TreeSelectionEvent e){
        TreePath path = e.getNewLeadSelectionPath();
        Object[] nodes = path.getPath();
        if (nodes[nodes.length - 1] instanceof MyNode){
        MyNode node = (MyNode)nodes[nodes.length - 1];
        fileMenu.removeAll();
        for(int i = 0; i < node.actions.size(); i++)
        fileMenu.add((Action)node.actions.get(0));

        System.gc();
        System.out.println("REBUILD MENU: FREE(" + (double)((double)runtime.freeMemory() / (double)runtime.totalMemory() * 100.0) + ")");
        }
            }

            public static void main(String args[]){
        new TestAction();
            }
        }

        class MyNode extends DefaultMutableTreeNode{
            Vector actions = new Vector();

            MyNode(){
        super("MyNode");
        for(int i = 0; i < 30; i++)
        actions.add(new MyAction());
            }
        }

        class MyAction extends AbstractAction{
            MyAction(){
        super("MyAction");
            }

            public void actionPerformed(ActionEvent e){
            }
        }
        --------------------------------------------------------------------
        masaaki.kamito@Japan 2001-08-07

              peterz Peter Zhelezniakov
              mkamito Masaaki Kamito (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: