Veja o código:
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
/**
*
* @author Giovanni
*/
public class Desktop extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
private JDesktopPane desktopPane;
public Desktop() throws Exception {
initComponents();
}
private void initComponents() throws Exception {
desktopPane = new JDesktopPane();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().add(desktopPane, java.awt.BorderLayout.CENTER);
}
public JDesktopPane getDesktopPane() {
return desktopPane;
}
public JInternalFrame openModule(final Class frameClass, final String name, final ImageIcon icon) throws Exception {
for (int i = 0; i < 50; i++) {
final int cont = i;
JInternalFrame frame = new JInternalFrame(){
int[] i = new int[1000000];
@Override
protected void finalize() throws Throwable {
System.err.println("finalize-"+cont);
super.finalize();
}
};
// JInternalFrame frame = (JInternalFrame) frameClass.newInstance();
frame.setSize(800,500);
frame.setTitle(name);
// frame.setFrameIcon(icon);
// Dimension desktopSize = desktopPane.getSize();
// Dimension frameSize = frame.getSize();
// Point location = new Point(
// (desktopSize.width - frameSize.width)/2,
// (desktopSize.height - frameSize.height)/2 );
// location.x = Math.max(0, location.x);
// location.y = Math.max(0, location.y);
// frame.setLocation(location);
//[b]linha 57:[/b] desktopPane.add(frame);
frame.setVisible(true);
Thread.sleep(10);
// desktopPane.remove(frame);
frame.doDefaultCloseAction();
frame = null;
System.gc();
}
// return frame;
return null;
}
public static void main(String[] args) {
try {
final Desktop desktop = new Desktop();
desktop.setSize(800,600);
desktop.setVisible(true);
SwingUtilities.invokeLater(new Runnable(){
public void run() {
try {
desktop.openModule(null, "", null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
O código acima coleta normalmente a memoria, mas se descomentar a linha 57 (desktopPane.add(frame);), a coleta para de funcionar, alguem sabe porque.
Java SE 6
Windows XP SP2
no Java SE 5 tambem dá o problema.