diff --git a/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingMemoryLeakExample.java b/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingMemoryLeakExample.java index 8902328..fcea03d 100644 --- a/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingMemoryLeakExample.java +++ b/examples/src/main/java/edu/umd/cs/piccolo/examples/pswing/PSwingMemoryLeakExample.java @@ -57,14 +57,13 @@ /** Default serial version UID. */ private static final long serialVersionUID = 1L; - /** Active Instances Counter. */ - private final PText activeInstanceCounter; + /** Active instances. */ + private final PText active; - /** Garbage Collected Counter. */ - private final PText gcInstanceCounter; + /** Finalized instances. */ + private final PText finalized; - - /** Memory. */ + /** Free memory. */ private final PText freeMemory; /** Total memory. */ @@ -87,9 +86,9 @@ super("PSwing memory leak example"); PText label0 = new PText("Number of active PSwingCanvases:"); - activeInstanceCounter = new PText("0"); + active = new PText("0"); PText label4 = new PText("Number of finalized PSwingCanvases:"); - gcInstanceCounter = new PText("0"); + finalized = new PText("0"); PText label1 = new PText("Total memory:"); totalMemory = new PText("0"); PText label2 = new PText("Free memory:"); @@ -98,9 +97,9 @@ usedMemory = new PText("0"); label0.offset(20.0d, 20.0d); - activeInstanceCounter.offset(label0.getFullBounds().getWidth() + 50.0d, 20.0d); + active.offset(label0.getFullBounds().getWidth() + 50.0d, 20.0d); label4.offset(20.0d, 40.0d); - gcInstanceCounter.offset(label4.getFullBounds().getWidth() + 50.0d, 40.0d); + finalized.offset(label4.getFullBounds().getWidth() + 50.0d, 40.0d); label1.offset(20.0d, 60.0d); totalMemory.offset(label1.getFullBounds().getWidth() + 40.0d, 60.0d); label2.offset(20.0d, 80.0d); @@ -110,19 +109,19 @@ canvas = new PCanvas(); canvas.getCamera().addChild(label0); - canvas.getCamera().addChild(activeInstanceCounter); + canvas.getCamera().addChild(active); canvas.getCamera().addChild(label4); - canvas.getCamera().addChild(gcInstanceCounter); + canvas.getCamera().addChild(finalized); canvas.getCamera().addChild(label1); canvas.getCamera().addChild(totalMemory); canvas.getCamera().addChild(label2); canvas.getCamera().addChild(freeMemory); canvas.getCamera().addChild(label3); canvas.getCamera().addChild(usedMemory); - canvas.setPreferredSize(new Dimension(400, 100)); + canvas.setPreferredSize(new Dimension(400, 110)); mainPanel = new JPanel(); - mainPanel.setPreferredSize(new Dimension(400, 300)); + mainPanel.setPreferredSize(new Dimension(400, 290)); mainPanel.setLayout(new FlowLayout()); getContentPane().setLayout(new BorderLayout()); @@ -138,8 +137,9 @@ JLabel label = new JLabel("Label" + id); PSwing pswing = new PSwing(label); PSwingCanvas pswingCanvas = new PSwingCanvas() { + /** {@inheritDoc} */ public void finalize() { - gcInstanceCounter.setText(String.valueOf(Integer.parseInt(gcInstanceCounter.getText())+1)); + incrementFinalized(); } }; pswingCanvas.getLayer().addChild(pswing); @@ -150,7 +150,7 @@ mainPanel.repaint(); id++; - incrementCounter(); + incrementActive(); } }); add.setDelay(5); @@ -169,10 +169,7 @@ mainPanel.invalidate(); mainPanel.validate(); mainPanel.repaint(); - decrementCounter(); - - System.gc(); - System.runFinalization(); + decrementActive(); } } }); @@ -199,21 +196,30 @@ } /** - * Increment counter. + * Increment active. */ - private void incrementCounter() { - int count = Integer.parseInt(activeInstanceCounter.getText()); + private void incrementActive() { + int count = Integer.parseInt(active.getText()); count++; - activeInstanceCounter.setText(String.valueOf(count)); + active.setText(String.valueOf(count)); } /** - * Decrement counter. + * Decrement active. */ - private void decrementCounter() { - int count = Integer.parseInt(activeInstanceCounter.getText()); + private void decrementActive() { + int count = Integer.parseInt(active.getText()); count--; - activeInstanceCounter.setText(String.valueOf(count)); + active.setText(String.valueOf(count)); + } + + /** + * Increment finalized. + */ + private void incrementFinalized() { + int count = Integer.parseInt(finalized.getText()); + count++; + finalized.setText(String.valueOf(count)); } /** @@ -221,6 +227,8 @@ */ private void updateMemory() { System.gc(); + System.runFinalization(); + long total = Runtime.getRuntime().totalMemory(); totalMemory.setText(String.valueOf(total)); long free = Runtime.getRuntime().freeMemory(); diff --git a/extras/src/main/java/edu/umd/cs/piccolox/pswing/PSwingCanvas.java b/extras/src/main/java/edu/umd/cs/piccolox/pswing/PSwingCanvas.java index c36a195..1d8989f 100644 --- a/extras/src/main/java/edu/umd/cs/piccolox/pswing/PSwingCanvas.java +++ b/extras/src/main/java/edu/umd/cs/piccolox/pswing/PSwingCanvas.java @@ -41,7 +41,6 @@ * @author Sam R. Reid * @author Lance E. Good */ - public class PSwingCanvas extends PCanvas { public static final String SWING_WRAPPER_KEY = "Swing Wrapper"; private SwingWrapper swingWrapper; @@ -80,19 +79,35 @@ swingWrapper.remove(pSwing.getComponent()); } + /** + * JComponent wrapper for a PSwingCanvas. Used by PSwingRepaintManager. + */ static class SwingWrapper extends JComponent { + + /** PSwingCanvas to wrap. */ private PSwingCanvas pSwingCanvas; - public SwingWrapper(PSwingCanvas pSwingCanvas) { - this.pSwingCanvas = pSwingCanvas; + + /** + * Create a new JComponent wrapper for the specified PSwingCanvas. + * + * @param canvas PSwingCanvas to wrap + */ + SwingWrapper(final PSwingCanvas canvas) { + this.canvas = canvas; setSize(new Dimension(0, 0)); setPreferredSize(new Dimension(0, 0)); putClientProperty(SWING_WRAPPER_KEY, SWING_WRAPPER_KEY); } - public PSwingCanvas getPSwingCanvas() { - return pSwingCanvas; + + /** + * Return the PSwingCanvas this JComponent wrapper is wrapping. + * + * @return the PSwingCanvas this JComponent wrapper is wrapping + */ + PSwingCanvas getPSwingCanvas() { + return canvas; } } - } \ No newline at end of file diff --git a/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingCanvasTest.java b/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingCanvasTest.java index 54b6824..64fb97e 100644 --- a/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingCanvasTest.java +++ b/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingCanvasTest.java @@ -1,9 +1,40 @@ +/* + * Copyright (c) 2008-2009, Piccolo2D project, http://piccolo2d.org + * Copyright (c) 1998-2008, University of Maryland + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions + * and the following disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * None of the name of the University of Maryland, the name of the Piccolo2D project, or the names of its + * contributors may be used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ package edu.umd.cs.piccolox.pswing; import javax.swing.JPanel; import junit.framework.TestCase; +/** + * Unit test for PSwingCanvas. + */ public class PSwingCanvasTest extends TestCase { protected int finalizerCallCount;