DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=28918>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=28918

SoftReferenceObjectPool broken?

           Summary: SoftReferenceObjectPool broken?
           Product: Commons
           Version: 1.1 Final
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Major
          Priority: Other
         Component: Pool
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


Hello,

I have not been able to get SoftReferenceObjectPool to work as expected. Below 
you can find a test case that supports this assertion. Technically, when there 
is severe memory requirements on the JVM, the objects in the pool should be 
garbage collected, but they are not. Even when OutOfMemory exception is caught 
and the status of the pool examined, it still contains these objects.

I examined the source code but could not locate any obvious errors. However, I 
did find this message:

http://www.mail-archive.com/[EMAIL PROTECTED]/msg02174.html

which uses an ObjectHolder for the soft referenced objects. This approach 
seems to work, as can be tested by using the code in the post.

Regards,
Vikram

import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.SoftReferenceObjectPool;

import java.util.HashMap;

public class TestSoftRef extends TestCase {

        private SoftReferenceObjectPool pool;

  public static TestSuite suite() {
    return new TestSuite(TestSoftRef.class);
  }

  public static void main(String[] args) {
                String[] testClassName = { TestSoftRef.class.getName() };
    junit.textui.TestRunner.main(testClassName);
  }

  public TestSoftRef(String s) {
    super(s);
  }

        public void testOutOfMemory() throws Exception {

                Object obj = pool.borrowObject();
                pool.returnObject(obj);
                obj = null;

                assertEquals(1, pool.getNumIdle());

                try {
                        HashMap map = new HashMap();
                        int divisor = 150000;

                        for(int i = 0; i < 1000000; i++) {
                                map.put(new Integer(i), new String("Fred 
Flintstone" + i));
                        }
                }catch(OutOfMemoryError ex) {
                         assertEquals(0, pool.getNumIdle());
                }
        }

        public void setUp() throws Exception {
                this.pool = new SoftReferenceObjectPool(new 
PoolableObjectFactory() {
                         int counter;
                         public Object makeObject()                   { return 
String.valueOf(counter++); }
                         public void destroyObject( Object obj )      {}
                         public boolean validateObject( Object obj )  { return 
true; }
                         public void activateObject( Object obj )     {}
                         public void passivateObject( Object obj )    {}
                });
        }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to