commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 28918] New: - SoftReferenceObjectPool broken?
Date Wed, 12 May 2004 02:23:45 GMT
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: commons-dev@jakarta.apache.org
        ReportedBy: tech@craftbits.com


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/commons-dev@jakarta.apache.org/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: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message