View unanswered posts | View active topics It is currently Thu Feb 23, 2017 4:57 pm



Reply to topic  [ 4 posts ] 
 Memory leak when add/ remove JPanel with miglayout 
Author Message

Joined: Wed Oct 03, 2012 12:57 pm
Posts: 2
Post Memory leak when add/ remove JPanel with miglayout
Here is an example application that reproduces a memory leak situation in LinkHanler.
The MigLayout refereces are never removed from LAYOUTS list.

I noticed that the cleanup is done when LinkHandler.getValue is called but in this scenario is never called.

public class Clazz extends JFrame {

private void initialize() {
this.setLocation(300, 200);
this.setTitle("MigLayout memory leak");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setMinimumSize(new Dimension(300,150));
this.getContentPane().setLayout(new BorderLayout());
JPanel mainPanel = new JPanel();
mainPanel.setVisible(true);
this.getContentPane().add(mainPanel, BorderLayout.CENTER);
this.getContentPane().add(createButton(mainPanel), BorderLayout.SOUTH);
addMigPanel(mainPanel);
}

private JButton createButton(final JPanel panel) {

JButton button = new JButton("Create and clear 1000 mig panels");
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

for (int i = 0; i < 1000; i++) {
panel.removeAll();
addMigPanel(panel);
}
}
});

return button;
}

private void addMigPanel(JPanel panel) {
JPanel migPanel = new JPanel(new MigLayout());
panel.add(migPanel);
this.revalidate();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Clazz testFrame = new Clazz();
testFrame.initialize();
}
});
}
}


Wed Oct 03, 2012 1:13 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Memory leak when add/ remove JPanel with miglayout
Ok, thanks. I hope it is solvable. It is in the work queue for the next version.

Cheers,
Mikael


Wed Oct 03, 2012 6:10 pm
Profile WWW

Joined: Wed Oct 03, 2012 12:57 pm
Posts: 2
Post Re: Memory leak when add/ remove JPanel with miglayout
A simple solution is cleaning the LAYOUTS list on every setBounds. Not sure about the perforormance penalty.
Oherwise the garbage collector can be monitored with a simple trick:

private static WeakReference<GcDetector> gcDetector = new WeakReference<GcDetector>(new GcDetector());


...................

This code can be added in setBounds

//Activate the detector
if (gcDetector.get() == null) {
gcDetector = new WeakReference<GcDetector>(new GcDetector());
}

This is the detector
private static class GcDetector {

public void finalize() {
System.out.println("GcDetector.finalize - "+LAYOUTS.size()+" layouts:");
clearLinkHandler();
}
}


And...

private synchronized static void clearLinkHandler()
{
System.out.println("LinkHandler.clearUp()");

for (int i = LAYOUTS.size() - 1; i >= 0; i--) {

if (LAYOUTS.get(i).get() == null) {
LAYOUTS.remove(i);
VALUES.remove(i);
VALUES_TEMP.remove(i);
}
}
}

Hope it helps :)
Anyway I dont know the implementation details of MigLayout... may be there is a better solution.

Regards,
Pavel


Thu Oct 04, 2012 10:41 am
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Memory leak when add/ remove JPanel with miglayout
Hello,

This is fixed in the next version out in about a week.

Cheers,
Mikael


Fri Feb 22, 2013 4:17 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.