United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4759312 : JInternalFrame Not Being Finalized After Closing

Details
Type:
Bug
Submit Date:
2002-10-07
Status:
Closed
Updated Date:
2013-10-31
Project Name:
JDK
Resolved Date:
2013-05-31
Component:
client-libs
OS:
generic,windows_xp
Sub-Component:
javax.swing
CPU:
x86,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
7u2
Fixed Versions:
7u40 (b28)

Related Reports
Backport:
Relates:

Sub Tasks

Description
Name: jk109818			Date: 10/07/2002


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

and

java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

FULL OPERATING SYSTEM VERSION :
Windows XP, Service Pack 1

A DESCRIPTION OF THE PROBLEM :
Related to Bug ID 4748141.

If a single JInternalFrame is added to a JDesktopPane and
then closed, the instance is not available for garbage
collection UNTIL a new JInternalFrame is added to the
desktop.  It seems like even though the JInternalFrame has
been "disposed", a reference is still maintained to the
JInternalFrame as the selected window.  When the new
JInternalFrame is added, the reference is shifted allowing
the "disposed" JInternalFrame to be garbage collected.

In essence, this is a memory leak.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the code as shown in the Source Code section.  Click
the "Add Frame" button, close the initial frame, and click
the "Garbage Collect" button (sets the state to what is
described in bug ID 4748141).  Click the "Add Frame"
button, close the new frame, and click the "Garbage
Collect" button.  Note in the output window that the
println() statement in the finalize() method of the custom
JInternalFrame class does not trigger.  Now click the "Add
Frame" button followed by the "Garbage Collect" button.
You will see the println() statement in the finalize()
method of the original custom JInternalFrame() instance
trigger.
NOW, the original JInternalFrame is allowed to garbage
collect.

EXPECTED VERSUS ACTUAL BEHAVIOR :
Should expect to see the finalize() method for the
JInternalFrame class to trigger after it has been disposed
of and the garbage collector fires.

REPRODUCIBILITY :
This bug can be reproduced always.

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

public class InternalFrameTest
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Internal Frame Test");
        frame.getContentPane().setLayout(new BorderLayout());
        final JDesktopPane desktopPane = new JDesktopPane();
        desktopPane.setDesktopManager(new DefaultDesktopManager());
        frame.getContentPane().add(desktopPane, BorderLayout.CENTER);
        
        InternalFrameListener iFrameListener = new InternalFrameAdapter() {
            public void internalFrameClosed(InternalFrameEvent evt)
            {
                System.out.println("Number of Frames Remaining=" +
desktopPane.getAllFrames().length);
            }
        };
        
//        for (int i = 0; i < 10; i++) {
//            JInternalFrame iFrame = new CustomInternalFrame("Frame " + i);
//            iFrame.setSize(200, 200);
//            iFrame.addInternalFrameListener(iFrameListener);
//            desktopPane.add(iFrame);
//            iFrame.setVisible(true);
//        }
       
        JButton button = new JButton("Add Frame");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt)
            {
                System.out.println("Adding new internal frame!");
                CustomInternalFrame iFrame = new CustomInternalFrame("Dummy
Frame");
                iFrame.setSize(200, 200);
                desktopPane.add(iFrame);
                iFrame.setVisible(true);
            }
        });
        frame.getContentPane().add(button, BorderLayout.NORTH);
        
        button = new JButton("Garbage Collect");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt)
            {
                System.out.println("Firing garbage collection!");
                System.gc();
            }
        });
        frame.getContentPane().add(button, BorderLayout.SOUTH);
        
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
        System.runFinalizersOnExit(true);
    }
    
    
    public static class CustomInternalFrame extends JInternalFrame
    {
        private static int iFrameIdCounter = 0;
        
        private int iFrameId;
        
        public CustomInternalFrame(String title)
        {
            super(title, true, true, true, true);
            iFrameId = iFrameIdCounter++;
        }
        
        protected void finalize()
        {
            System.out.println("Discarding frame " + iFrameId);
        }
    }
}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
None Identified
(Review ID: 165240) 
======================================================================

                                    

Comments
URL:   http://hg.openjdk.java.net/jdk7u/jdk7u/jdk/rev/8e6e356df9bd
User:  lana
Date:  2013-06-03 19:46:42 +0000

                                     
2013-06-03
URL:   http://hg.openjdk.java.net/jdk7u/jdk7u-dev/jdk/rev/8e6e356df9bd
User:  vkarnauk
Date:  2013-05-23 13:07:43 +0000

                                     
2013-05-23
EVALUATION

This bug may have been fixed by the fix for
    4683398: WindowsDesktopManager mantains a strong reference to currentFrame
Need to verify this in Dolphin.
                                     
2006-06-13
EVALUATION

Contribution-forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=11168


As ###@###.### posted on that message, this bug (4759312)
should be closed as a DUP of 4836639.
                                     
2006-02-02
EVALUATION

Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?messageID=11168&forumID=1463
                                     
2006-02-02
EVALUATION

Same evaluation as 4748141.
###@###.### 2002-10-18
                                     
2002-10-18



Hardware and Software, Engineered to Work Together