JAL-4102 update 2.12 from 2.11.2.6 patch release - JAL-3416 JAL-2353 JAL-4104 JAL...
[jalview.git] / test / jalview / gui / FreeUpMemoryTest.java
index 3a5a9e4..ba81c77 100644 (file)
@@ -1,8 +1,29 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.gui;
 
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
 
+
 import java.awt.event.MouseEvent;
 import java.io.File;
 import java.io.IOException;
@@ -91,7 +112,6 @@ public class FreeUpMemoryTest
    * maximum retained heap usage (in MB) for a passing test
    */
   private static int MAX_RESIDUAL_HEAP = 45;
-
   /**
    * Configure (read-only) Jalview property settings for test
    */
@@ -102,24 +122,51 @@ public class FreeUpMemoryTest
             new String[]
             { "-nonews", "-props", "test/jalview/testProps.jvprops" });
     String True = Boolean.TRUE.toString();
-    Cache.applicationProperties.setProperty("SHOW_ANNOTATIONS", True);
-    Cache.applicationProperties.setProperty("SHOW_QUALITY", True);
-    Cache.applicationProperties.setProperty("SHOW_CONSERVATION", True);
-    Cache.applicationProperties.setProperty("SHOW_OCCUPANCY", True);
-    Cache.applicationProperties.setProperty("SHOW_IDENTITY", True);
+    Cache.setPropertyNoSave("SHOW_ANNOTATIONS", True);
+    Cache.setPropertyNoSave("SHOW_QUALITY", True);
+    Cache.setPropertyNoSave("SHOW_CONSERVATION", True);
+    Cache.setPropertyNoSave("SHOW_OCCUPANCY", True);
+    Cache.setPropertyNoSave("SHOW_IDENTITY", True);
   }
 
+  /**
+   * A simple test that memory is released when all windows are closed.
+   * <ul>
+   * <li>generates a reasonably large alignment and loads it</li>
+   * <li>performs various operations on the alignment</li>
+   * <li>closes all windows</li>
+   * <li>requests garbage collection</li>
+   * <li>asserts that the remaining memory footprint (heap usage) is 'not large'
+   * </li>
+   * </ul>
+   * If the test fails, this suggests that a reference to some large object
+   * (perhaps the alignment data, or some annotation / Tree / PCA data) has
+   * failed to be garbage collected. If this is the case, the heap will need to
+   * be inspected manually (suggest using jvisualvm) in order to track down
+   * where large objects are still referenced. The code (for example
+   * AlignmentViewport.dispose()) should then be updated to ensure references to
+   * large objects are set to null when they are no longer required.
+   * 
+   * @throws IOException
+   */
   @Test(groups = "Memory")
   public void testFreeMemoryOnClose() throws IOException
   {
     File f = generateAlignment();
     f.deleteOnExit();
 
+    long expectedMin = MAX_RESIDUAL_HEAP;
+    long usedMemoryAtStart=getUsedMemory();
+    if (usedMemoryAtStart>expectedMin)
+    {
+      System.err.println("used memory before test is "+usedMemoryAtStart+" > "+expectedMin+"MB .. adjusting minimum.");
+      expectedMin = usedMemoryAtStart;
+    }
     doStuffInJalview(f);
 
-    Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.getInstance().closeAll_actionPerformed(null);
 
-    checkUsedMemory(MAX_RESIDUAL_HEAP);
+    checkUsedMemory(expectedMin);
   }
 
   /**
@@ -133,17 +180,15 @@ public class FreeUpMemoryTest
     long availableMemory = Runtime.getRuntime().totalMemory();
     long freeMemory = Runtime.getRuntime().freeMemory();
     long usedMemory = availableMemory - freeMemory;
-
     return (int) (usedMemory / ONE_MB);
   }
-
   /**
    * Requests garbage collection and then checks whether remaining memory in use
    * is less than the expected value (in Megabytes)
    * 
    * @param expectedMax
    */
-  protected void checkUsedMemory(int expectedMax)
+  protected void checkUsedMemory(long expectedMax)
   {
     /*
      * request garbage collection and wait for it to run (up to 3 times);