>## 20. 7. 2011 12:32:02 ##<
> >## 20. 7. 2011 10:04:26 ##<
> > But do you have test which can prove that any of these fields cause
> > real memory leak?
> 
> Yes, I have a test. My whole motivation is to fix our tests. Here is one of
> them:
> http://hg.netbeans.org/ergonomics/file/f6cc96cf2c44/core.windows/test/unit/
> src/org/netbeans/core/windows/services/NbPresenterLeakTest.java

I've modified the above test to be standalone (see attachement). It fails in 
about 50% of cases 

---
Exception in thread "main" java.lang.AssertionError: Dialog disappears.
  
org.netbeans.core.windows.services.NbPresenterLeakTest.assertGC(NbPresenterLeakTest.java:181)
  
org.netbeans.core.windows.services.NbPresenterLeakTest.main(NbPresenterLeakTest.java:100)
---

on my computer. I am running Kubuntu 11.04 and using:

---
$ java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Server VM (build 20.1-b02, mixed mode)
---

Can we proceed with evaluation of my patch now?
-jt

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 * Other names may be trademarks of their respective owners.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * http://www.netbeans.org/cddl-gplv2.html
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 * specific language governing permissions and limitations under the
 * License.  When distributing the software, include this License Header
 * Notice in each file and include the License file at
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the GPL Version 2 section of the License file that
 * accompanied this code. If applicable, add the following below the
 * License Header, with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Contributor(s):
 *
 * The Original Software is NetBeans. The Initial Developer of the Original
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 * Microsystems, Inc. All Rights Reserved.
 *
 * If you wish your version of this file to be governed by only the CDDL
 * or only the GPL Version 2, indicate your decision by adding
 * "[Contributor] elects to include this software in this distribution
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 * single choice of license, a recipient has the option to distribute
 * your version of this file under either the CDDL, the GPL Version 2 or
 * to extend the choice of license to its licensees as provided above.
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 * Version 2 license, then the option applies only if the new code is
 * made subject to such option by the copyright holder.
 */

package org.netbeans.core.windows.services;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/** Tests issue 96282 - Memory leak in org.netbeans.core.windows.services.NbPresenter
 *
 * @author Jiri Rechtacek
 */
public class NbPresenterLeakTest  {


    public static void main(String... args) throws InterruptedException, InvocationTargetException {
        JButton wizardDescriptor = new JButton("Visible");
        Dialog dialog = new JDialog(new JFrame(), false);
        dialog.add(wizardDescriptor);
        WeakReference<Object> w = new WeakReference<Object>(wizardDescriptor);
        
        SwingUtilities.invokeAndWait (new EDTJob(dialog, true));
        assertShowing("button is visible", true, dialog);
        SwingUtilities.invokeAndWait (new EDTJob(dialog, false));
        assertShowing("button is no longer visible", false, dialog);
        Dialog d = new JDialog();
        
        // workaround for JDK bug 6575402
        JPanel p = new JPanel();
        d.setLayout(new BorderLayout());
        d.add(p, BorderLayout.CENTER);
        JButton btn = new JButton("Button");
        p.add(btn, BorderLayout.NORTH);
        
        SwingUtilities.invokeAndWait (new EDTJob(d, true));
        assertShowing("button is visible", true, btn);
        dialog.setBounds(findCenterBounds(dialog.getSize()));
        SwingUtilities.invokeAndWait (new EDTJob(d, false));
        assertShowing("button is no longer visible", false, btn);

        dialog = null;
        wizardDescriptor = null;
        
        assertGC ("Dialog disappears.", w);
        System.exit(0);
    }

    private static void assertShowing(String msg, boolean showing, final Component c)
    throws InterruptedException, InvocationTargetException {
        final boolean[] res = { false };
        for (int i = 0; i < 50; i++) {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    res[0] = c.isShowing();
                }
            });
            if (showing == res[0]) {
                break;
            }
            Thread.sleep(100);
        }
        assert showing == res[0] : msg;
    }
    
    private static class EDTJob implements Runnable {
        private Dialog d;
        private boolean visibility;
        
        EDTJob (Dialog d, boolean vis) {
            this.d = d;
            visibility = vis;
        }
        public void run() {
            d.setVisible(visibility);
            if (!visibility) {
                d.dispose();
            }
        }
    }
    
   
    private static Rectangle findCenterBounds(Dimension componentSize) {
        GraphicsConfiguration gconf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

        Rectangle bounds = gconf.getBounds();

        return new Rectangle(
                bounds.x + ((bounds.width - componentSize.width) / 2),
                bounds.y + ((bounds.height - componentSize.height) / 2), componentSize.width, componentSize.height);
    }
    
    public static void assertGC(final String text, final Reference<?> ref) {
        List<byte[]> alloc = new ArrayList<byte[]>();
        int size = 100000;
        for (int i = 0; i < 50; i++) {
            if (ref.get() == null) {
                return;
            }
            try {
                System.gc();
            } catch (OutOfMemoryError error) {
                // OK
            }
            try {
                System.runFinalization();
            } catch (OutOfMemoryError error) {
                // OK
            }
            try {
                alloc.add(new byte[size]);
                size = (int) (((double) size) * 1.3);
            } catch (OutOfMemoryError error) {
                size = size / 2;
            }
            try {
                if (i % 3 == 0) {
                    Thread.sleep(321);
                }
            } catch (InterruptedException t) {
                // ignore
            }
        }
        alloc = null;
        assert false : text;
    }
}
    

Reply via email to