You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2008/04/24 01:57:56 UTC

svn commit: r651114 - in /commons/proper/beanutils/trunk: pom.xml src/test/org/apache/commons/beanutils/memoryleaktests/MemoryLeakTestCase.java

Author: niallp
Date: Wed Apr 23 16:57:54 2008
New Revision: 651114

URL: http://svn.apache.org/viewvc?rev=651114&view=rev
Log:
BEANUTILS-291 Improve memory leak test - thanks to Clebert Suconic for the patch.

Modified:
    commons/proper/beanutils/trunk/pom.xml
    commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/memoryleaktests/MemoryLeakTestCase.java

Modified: commons/proper/beanutils/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/pom.xml?rev=651114&r1=651113&r2=651114&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/pom.xml (original)
+++ commons/proper/beanutils/trunk/pom.xml Wed Apr 23 16:57:54 2008
@@ -171,6 +171,8 @@
           <artifactId>maven-surefire-plugin</artifactId>
             <configuration>
               <forkMode>pertest</forkMode>
+              <!-- limit memory size see BEANUTILS-291 -->
+              <argLine>-Xmx25M</argLine>
               <includes>
                 <include>**/*TestCase.java</include>
               </includes>

Modified: commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/memoryleaktests/MemoryLeakTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/memoryleaktests/MemoryLeakTestCase.java?rev=651114&r1=651113&r2=651114&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/memoryleaktests/MemoryLeakTestCase.java (original)
+++ commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/memoryleaktests/MemoryLeakTestCase.java Wed Apr 23 16:57:54 2008
@@ -16,6 +16,7 @@
  */ 
 package org.apache.commons.beanutils.memoryleaktests;
 
+import java.lang.ref.SoftReference;
 import java.lang.ref.WeakReference;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -421,21 +422,28 @@
      */
     private void forceGarbageCollection() throws Exception {
         // Fill up memory
-        java.util.ArrayList list = new java.util.ArrayList();
-        try {
-            long i = 0;
-            while (true) {
-                list.add("A Big String A Big String A Big String A Big String A Big String A Big String A Big String A Big String A Big String A Big String " + (i++));
+        SoftReference ref = new SoftReference(new Object());
+        int count = 0;
+        while(ref.get() != null && count++ < 5) {
+            java.util.ArrayList list = new java.util.ArrayList();
+            try {
+                long i = 0;
+                while (true && ref.get() != null) {
+                    list.add("A Big String A Big String A Big String A Big String A Big String A Big String A Big String A Big String A Big String A Big String " + (i++));
+                }
+            } catch (Throwable ignored) {
             }
-        } catch (Throwable e) {
+            list.clear();
+            list = null;
+            // System.out.println("Count " + count + " : " + getMemoryStats());
+            System.gc(); 
+            Thread.sleep(1000);
+        }
+        // System.out.println("After GC: " + getMemoryStats());
+        
+        if (ref.get() != null) {
+            throw new IllegalStateException("Your JVM is not releasing SoftReference, try running the testcase with less memory (-Xmx)");
         }
-
-        list.clear();
-        list = null;
-
-        // Prompt garbage collector
-        System.gc();
-        Thread.sleep(1000);
     }
 
     /**
@@ -498,5 +506,20 @@
             if (minorVersion.equals("4")) return true;
         }
         return false;
+    }
+
+    /**
+     * Get the total, free, used memory stats.
+     * @return the total, free, used memory stats
+     */
+    private String getMemoryStats() {
+        java.text.DecimalFormat fmt = new java.text.DecimalFormat("#,##0");
+        Runtime runtime = Runtime.getRuntime();
+        long free = runtime.freeMemory() / 1024;
+        long total = runtime.totalMemory() / 1024;
+        long used = total - free;
+        return "MEMORY - Total: " + fmt.format(total) + "k " + "Used: "
+                + fmt.format(used) + "k " + "Free: "
+                + fmt.format(free) + "k";
     }
 }