View unanswered posts | View active topics It is currently Sat May 25, 2013 12:40 am



Reply to topic  [ 7 posts ] 
 Memory Leak when disposing children of a SWT Composite 
Author Message

Joined: Tue Aug 16, 2011 5:08 pm
Posts: 3
Post Memory Leak when disposing children of a SWT Composite
Using MigLayout 3.7.1. I haven't tried a later version as the changelog didn't mention anything about a memory leak fix.

Using the code below, when you run and switch the combo to dispose a widget and display a new one, profiling with YourKit shows that the disposed widgets are not freed from memory. The attached png shows the paths to GC roots which is retaining the widgets.

Swapping MigLayout with swt's GridLayout as the layoutmanager does not display the same leak.

Code:
import net.miginfocom.swt.MigLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class MemoryLeakTest extends Composite {
   private Combo   combo;
   private Control   control;

   public MemoryLeakTest(Composite parent, int style, Layout layout) {
      super(parent, style);
      setLayout(layout);
      createControls();
   }

   private void createControls() {
      Label label = new Label(this, SWT.None);
      label.setText("Select Control:");

      combo = new Combo(this, SWT.READ_ONLY | SWT.DROP_DOWN);
      combo.setLayoutData("wrap");
      combo.add("Text Box");
      combo.add("Combo");
      combo.add("Radio Button");

      combo.addSelectionListener(new SelectionAdapter() {

         @Override
         public void widgetSelected(SelectionEvent e) {

            if (control != null)
               control.dispose();

            switch(combo.getSelectionIndex()) {
               case 0:
                  control = new Text(MemoryLeakTest.this, SWT.BORDER);
                  break;
               case 1:
                  control = new Combo(MemoryLeakTest.this, SWT.DROP_DOWN);
                  break;
               case 2:
                  control = new Button(MemoryLeakTest.this, SWT.RADIO);
                  break;
            }

            if (MemoryLeakTest.this.getLayout() instanceof GridLayout) {
               GridData data = new GridData(SWT.FILL, SWT.NONE, false, false);
               data.horizontalSpan = 2;
               control.setLayoutData(data);
            }
            else if (MemoryLeakTest.this.getLayout() instanceof MigLayout) {
               control.setLayoutData("spanx 2, grow");
            }

            MemoryLeakTest.this.layout(true);
         }
      });
   }

   public static void main(String[] args) {
      Display display = new Display();
      Shell shell = new Shell(display);
      shell.setText("SWT");
      shell.setLayout(new FillLayout());
      
      /**
       * Swap out GridLayout for MigLayout to profile and see resources cleaned up.
       */
      // GridLayout layout = new GridLayout(2, false);
      MigLayout layout = new MigLayout();
      new MemoryLeakTest(shell, SWT.NONE, layout);

      shell.open();

      // Set up the event loop.
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            // If no more entries in event queue
            display.sleep();
         }
      }

      display.dispose();
   }
}


Attachments:
PathsToGCRoot.png
PathsToGCRoot.png [ 10.81 KiB | Viewed 1628 times ]
Thu Aug 25, 2011 1:02 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Memory Leak when disposing children of a SWT Composite
Hello,

You're correct. SWT doesn't notify the layout manager when components are removed as Swing does. MigLayout for SWT doesn't properly handle this and erratically keeps components and constraints forever.

I fix will be out for the next version. I will release it shortly since memory leaks are critical.

Cheers,
Mikael


Thu Aug 25, 2011 2:03 pm
Profile WWW

Joined: Tue Aug 16, 2011 5:08 pm
Posts: 3
Post Re: Memory Leak when disposing children of a SWT Composite
MiG Support wrote:
Hello,

You're correct. SWT doesn't notify the layout manager when components are removed as Swing does. MigLayout for SWT doesn't properly handle this and erratically keeps components and constraints forever.

I fix will be out for the next version. I will release it shortly since memory leaks are critical.

Cheers,
Mikael


Awesome, thanks Mikael. Is there a list I can subscribe to for notifications of the release?


Thu Aug 25, 2011 2:24 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Memory Leak when disposing children of a SWT Composite
No, not currently.

When I get time I will move to java.net which has both JIRA support and Maven auto upload.

Cheers,
Mikael


Thu Aug 25, 2011 2:26 pm
Profile WWW

Joined: Tue Aug 16, 2011 5:08 pm
Posts: 3
Post Re: Memory Leak when disposing children of a SWT Composite
Hi Mikael,

Any estimate on a fix for this?

Cheers,
B


Fri Sep 02, 2011 4:01 pm
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Memory Leak when disposing children of a SWT Composite
We're having some technical issues with migrating the subversion repo to Git. Hopefully it will be done in a week or two.

Cheers,
Mikael


Fri Sep 09, 2011 12:23 am
Profile WWW
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: Memory Leak when disposing children of a SWT Composite
Hello,

Better late than never? :)

This is fixed in the upcoming version due in a a week or so.

Cheers,
Mikael


Fri Feb 22, 2013 1:16 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 7 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.