commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hamm...@apache.org
Subject cvs commit: jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/test MemoryLeakClientTest.java MemoryLeakImpl.java TestInterfaceImpl.java
Date Sun, 31 Mar 2002 21:24:29 GMT
hammant     02/03/31 13:24:29

  Modified:    altrmi/src/java/org/apache/commons/altrmi/server
                        MethodInvocationHandler.java
               altrmi/src/java/org/apache/commons/altrmi/server/impl
                        DefaultMethodInvocationHandler.java
               altrmi/src/java/org/apache/commons/altrmi/server/impl/adapters
                        InvocationHandlerAdapter.java
                        PublicationAdapter.java
               altrmi/src/java/org/apache/commons/altrmi/test
                        MemoryLeakClientTest.java MemoryLeakImpl.java
                        TestInterfaceImpl.java
  Added:       altrmi/src/java/org/apache/commons/altrmi/server/impl
                        AltrmiSession.java
  Log:
  Distributed Garbage Collection is working ... but still leaking a little memory.
  
  Revision  Changes    Path
  1.3       +15 -15    jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/server/MethodInvocationHandler.java
  
  Index: MethodInvocationHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/server/MethodInvocationHandler.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MethodInvocationHandler.java	30 Mar 2002 08:55:45 -0000	1.2
  +++ MethodInvocationHandler.java	31 Mar 2002 21:24:28 -0000	1.3
  @@ -8,9 +8,8 @@
    */
   package org.apache.commons.altrmi.server;
   
  -
  -
  -import org.apache.commons.altrmi.common.AltrmiInvocationHandler;
  +import org.apache.commons.altrmi.common.AltrmiReply;
  +import org.apache.commons.altrmi.common.MethodRequest;
   
   
   /**
  @@ -18,9 +17,20 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.3 $
    */
  -public interface MethodInvocationHandler extends AltrmiInvocationHandler {
  +public interface MethodInvocationHandler {
  +
  +    /**
  +     * Method handleMethodInvocation
  +     *
  +     *
  +     * @param request
  +     *
  +     * @return
  +     *
  +     */
  +    AltrmiReply handleMethodInvocation(MethodRequest request);
   
       /**
        * Method addImplementationBean
  @@ -50,14 +60,4 @@
        *
        */
       Long getOrMakeReferenceIDForBean(Object implBean);
  -
  -    /**
  -     * Method garbageCollectionRequest
  -     *
  -     *
  -     * @param sessionID
  -     * @param refereinceID
  -     *
  -     */
  -    void garbageCollectionRequest(Long sessionID, Long refereinceID);
   }
  
  
  
  1.7       +33 -32    jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/server/impl/DefaultMethodInvocationHandler.java
  
  Index: DefaultMethodInvocationHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/server/impl/DefaultMethodInvocationHandler.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- DefaultMethodInvocationHandler.java	30 Mar 2002 08:55:45 -0000	1.6
  +++ DefaultMethodInvocationHandler.java	31 Mar 2002 21:24:28 -0000	1.7
  @@ -26,9 +26,11 @@
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.Collection;
  +import java.util.WeakHashMap;
   
   import java.lang.reflect.Method;
   import java.lang.reflect.InvocationTargetException;
  +import java.lang.ref.WeakReference;
   
   import java.io.Serializable;
   
  @@ -39,16 +41,17 @@
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
    * @author Vinay Chandrasekharan <a href="mailto:vinay_chandran@users.sourceforge.net">vinay_chandran@users.sourceforge.net</a>
  - * @version $Revision: 1.6 $
  + * @version $Revision: 1.7 $
    */
   public class DefaultMethodInvocationHandler implements MethodInvocationHandler {
   
  -    private HashMap mRefBeans = new HashMap();
  -    private HashMap mBeanRefs = new HashMap();
  +    private WeakHashMap mRefBeans = new WeakHashMap();
  +    private WeakHashMap mBeanRefs = new WeakHashMap();
       private HashMap mMethodMap;
       private static int NEXT_REFERENCE = 1;
       private AltrmiPublisher mAltrmiPublisher;
       private String mPublishedThing;
  +    private Object mBeanImpl;
   
       /**
        * Constructor DefaultMethodInvocationHandler
  @@ -76,7 +79,7 @@
        *
        */
       public String toString() {
  -        return "DIH:" + mPublishedThing;
  +        return "DMIH:" + mPublishedThing;
       }
   
       /**
  @@ -88,7 +91,11 @@
        *
        */
       public void addImplementationBean(Long referenceID, Object beanImpl) {
  -        mRefBeans.put(referenceID, beanImpl);
  +        if (referenceID.equals(new Long(0))) {
  +            mBeanImpl = beanImpl;
  +        }
  +        WeakReference wr = new WeakReference(beanImpl);
  +        mRefBeans.put(referenceID, new WeakReference(beanImpl));
           mBeanRefs.put(beanImpl, referenceID);
       }
   
  @@ -102,17 +109,17 @@
        */
       public void replaceImplementationBean(Object implBean, Object withImplBean) {
   
  -        Collection coll = mRefBeans.keySet();
  +        Long ref = (Long) mBeanRefs.get(implBean);
   
  -        for (Iterator iterator = coll.iterator(); iterator.hasNext(); ) {
  -            Long ref = (Long) iterator.next();
  +        mRefBeans.put(ref, new WeakReference(withImplBean));
  +        mBeanRefs.remove(implBean);
  +        mBeanRefs.put(withImplBean, ref);
   
  -            if (mRefBeans.get(ref).equals(implBean)) {
  -                mRefBeans.put(ref, withImplBean);
  -                mBeanRefs.remove(implBean);
  -                mBeanRefs.put(withImplBean, ref);
  -            }
  +        if (mBeanImpl == implBean) {
  +            mBeanImpl = withImplBean;
           }
  +
  +
       }
   
       /**
  @@ -141,7 +148,7 @@
       }
   
       /**
  -     * Method handleInvocation
  +     * Method handleMethodInvocation
        *
        *
        * @param request
  @@ -149,10 +156,9 @@
        * @return
        *
        */
  -    public AltrmiReply handleInvocation(AltrmiRequest request) {
  +    public AltrmiReply handleMethodInvocation(MethodRequest request) {
   
  -        MethodRequest mr = (MethodRequest) request;
  -        String methodSignature = mr.getMethodSignature();
  +        String methodSignature = request.getMethodSignature();
   
           if (!mMethodMap.containsKey(methodSignature)) {
               return new ExceptionReply(
  @@ -160,24 +166,22 @@
           }
   
           Method method = (Method) mMethodMap.get(methodSignature);
  -        Object rv = null;
   
           try {
  -            Object beanImpl = mRefBeans.get(mr.getReferenceID());
  +            WeakReference wr = (WeakReference) mRefBeans.get(request.getReferenceID());
  +            Object beanImpl = wr.get();
   
               if (beanImpl == null) {
  -                System.out.println("Missin for " + mr.getReferenceID());
  +                System.out.println("Missin for " + request.getReferenceID());
   
  -                return new NoSuchReferenceReply(mr.getReferenceID());
  +                return new NoSuchReferenceReply(request.getReferenceID());
               }
   
  -            Object[] args = mr.getArgs();
  -
  -            correctArgs(mr, args);
  +            Object[] args = request.getArgs();
   
  -            Object replyVal = method.invoke(beanImpl, mr.getArgs());
  +            correctArgs(request, args);
   
  -            return new MethodReply(replyVal);
  +            return new MethodReply(method.invoke(beanImpl, request.getArgs()));
           } catch (InvocationTargetException ite) {
               Throwable t = ite.getTargetException();
   
  @@ -200,10 +204,6 @@
           }
       }
   
  -    public void garbageCollectionRequest(Long sessionID, Long refereinceID) {
  -        //TODO
  -    }
  -
       private void correctArgs(MethodRequest mr, Object[] args) {
   
           for (int i = 0; i < args.length; i++) {
  @@ -211,11 +211,12 @@
               // TODO find a faster way to do this....
               if (args[i] instanceof FacadeRefHolder) {
                   FacadeRefHolder frh = (FacadeRefHolder) args[i];
  -                DefaultMethodInvocationHandler asih =
  +                DefaultMethodInvocationHandler methodInvocationHandler =
                       (DefaultMethodInvocationHandler) mAltrmiPublisher
                           .getMethodInvocationHandler(frh.getObjectName());
   
  -                args[i] = asih.mRefBeans.get(frh.getReferenceID());
  +                WeakReference wr = (WeakReference) methodInvocationHandler.mRefBeans.get(frh.getReferenceID());
  +                args[i] = wr.get();
               }
           }
       }
  
  
  
  1.1                  jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/server/impl/AltrmiSession.java
  
  Index: AltrmiSession.java
  ===================================================================
  
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.commons.altrmi.server.impl;
  
  
  
  import java.util.HashMap;
  
  
  /**
   * Class AltrmiSession
   *
   *
   * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
   * @version $Revision: 1.1 $
   */
  public class AltrmiSession {
  
      private Long mSession;
      private HashMap mBeansInUse = new HashMap();
  
      /**
       * Constructor AltrmiSession
       *
       *
       * @param session
       *
       */
      public AltrmiSession(Long session) {
          mSession = session;
      }
  
      /**
       * Method getSession
       *
       *
       * @return
       *
       */
      public Long getSession() {
          return mSession;
      }
  
      /**
       * Method addBeanInUse
       *
       *
       * @param referenceID
       * @param bean
       *
       */
      public void addBeanInUse(Long referenceID, Object bean) {
          mBeansInUse.put(referenceID, bean);
      }
  
      /**
       * Method removeBeanInUse
       *
       *
       * @param referenceID
       *
       */
      public void removeBeanInUse(Long referenceID) {
          mBeansInUse.remove(referenceID);
      }
  }
  
  
  
  1.7       +20 -9     jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/server/impl/adapters/InvocationHandlerAdapter.java
  
  Index: InvocationHandlerAdapter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/server/impl/adapters/InvocationHandlerAdapter.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- InvocationHandlerAdapter.java	30 Mar 2002 08:55:45 -0000	1.6
  +++ InvocationHandlerAdapter.java	31 Mar 2002 21:24:28 -0000	1.7
  @@ -42,9 +42,11 @@
   import org.apache.commons.altrmi.server.impl.classretrievers.NoClassRetriever;
   import org.apache.commons.altrmi.server.impl.DefaultAuthenticator;
   import org.apache.commons.altrmi.server.impl.AbstractServer;
  +import org.apache.commons.altrmi.server.impl.AltrmiSession;
   
   import java.util.Vector;
   import java.util.Iterator;
  +import java.util.HashMap;
   
   
   /**
  @@ -52,14 +54,14 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.6 $
  + * @version $Revision: 1.7 $
    */
   public class InvocationHandlerAdapter extends PublicationAdapter
           implements AltrmiInvocationHandler {
   
       private static int SESSION = 0;
       private Long mLastSession = new Long(0);
  -    private Vector mSessions = new Vector();
  +    private HashMap mSessions = new HashMap();
       private boolean mSuspend = false;
       private ClassRetriever mClassRetriever = new NoClassRetriever();
       private AltrmiAuthenticator mAltrmiAuthenticator = new DefaultAuthenticator();
  @@ -111,10 +113,10 @@
                   return new NotPublishedReply();
               }
   
  -            AltrmiInvocationHandler ih =
  -                (AltrmiInvocationHandler) mPublishedObjects.get(publishedThing);
  +            MethodInvocationHandler methodInvocationHandler =
  +                (MethodInvocationHandler) mPublishedObjects.get(publishedThing);
   
  -            return ih.handleInvocation(mReq);
  +            return methodInvocationHandler.handleMethodInvocation(mReq);
           } else if (request.getRequestCode() == AltrmiRequest.METHODFACADEREQUEST) {
               MethodFacadeRequest mFacReq = (MethodFacadeRequest) request;
               String publishedThing = mFacReq.getPublishedServiceName() + "_" + mFacReq.getObjectName();
  @@ -130,7 +132,7 @@
   
               MethodInvocationHandler methodInvocationHandler =
                   (MethodInvocationHandler) mPublishedObjects.get(publishedThing);
  -            AltrmiReply ar = methodInvocationHandler.handleInvocation(mFacReq);
  +            AltrmiReply ar = methodInvocationHandler.handleMethodInvocation(mFacReq);
   
               if (ar.getReplyCode() == AltrmiReply.EXCEPTIONREPLY) {
                   return ar;
  @@ -163,6 +165,9 @@
   
                   //TODO a decent ref number for main?
                   Long newRef = methodInvocationHandler2.getOrMakeReferenceIDForBean(beanImpl);
  +                // make sure the bean is not garbage collected.
  +                AltrmiSession sess = (AltrmiSession) mSessions.get(mFacReq.getSession());
  +                sess.addBeanInUse(newRef, beanImpl);
   
                   //long newRef2 = asih2.getOrMakeReferenceIDForBean(beanImpl);
                   return new MethodFacadeReply(newRef);
  @@ -189,6 +194,9 @@
                           refs[i] = null;
                       } else {
                           refs[i] = methodInvocationHandler2.getOrMakeReferenceIDForBean(beanImpls[i]);
  +
  +                        AltrmiSession sess = (AltrmiSession) mSessions.get(mFacReq.getSession());
  +                        sess.addBeanInUse(refs[i], beanImpls[i]);
                       }
   
                   }
  @@ -207,9 +215,12 @@
                       new AltrmiInvocationException("TODO - you dirty rat/hacker"));
               }
   
  +            AltrmiSession sess = (AltrmiSession) mSessions.get(gcr.getSession());
  +            sess.removeBeanInUse(gcr.getReferenceID());
  +
               MethodInvocationHandler methodInvocationHandler =
                   (MethodInvocationHandler) mPublishedObjects.get(publishedThing);
  -            methodInvocationHandler.garbageCollectionRequest(gcr.getSession(), gcr.getReferenceID());
  +
               return new GarbageCollectionReply();
           } else if (request.getRequestCode() == AltrmiRequest.LOOKUPREQUEST) {
               LookupRequest lr = (LookupRequest) request;
  @@ -237,7 +248,7 @@
           } else if (request.getRequestCode() == AltrmiRequest.OPENCONNECTIONREQUEST) {
               Long session = getNewSession();
   
  -            mSessions.add(session);
  +            mSessions.put(session, new AltrmiSession(session));
   
               return new OpenConnectionReply(mAltrmiAuthenticator.getTextToSign(), session);
           } else if (request.getRequestCode() == AltrmiRequest.PINGREQUEST) {
  @@ -276,7 +287,7 @@
               // buffer last session for performance.
               return true;
           } else {
  -            if (mSessions.contains(session)) {
  +            if (mSessions.containsKey(session)) {
                   mLastSession = session;
   
                   return true;
  
  
  
  1.6       +4 -4      jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/server/impl/adapters/PublicationAdapter.java
  
  Index: PublicationAdapter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/server/impl/adapters/PublicationAdapter.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- PublicationAdapter.java	30 Mar 2002 08:55:45 -0000	1.5
  +++ PublicationAdapter.java	31 Mar 2002 21:24:28 -0000	1.6
  @@ -28,7 +28,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.5 $
  + * @version $Revision: 1.6 $
    */
   public class PublicationAdapter extends AbstractMethodHandler implements AltrmiPublisher
{
   
  @@ -79,10 +79,10 @@
   
           // add method maps for main lookup-able service.
           HashMap mainMethodMap = new HashMap();
  -        MethodInvocationHandler mainAsih = new DefaultMethodInvocationHandler(this,
  +        MethodInvocationHandler mainMethodInvocationHandler = new DefaultMethodInvocationHandler(this,
                                                  asName + "_Main", mainMethodMap);
   
  -        mainAsih.addImplementationBean(new Long(0), impl);
  +        mainMethodInvocationHandler.addImplementationBean(new Long(0), impl);
   
           for (int x = 0; x < interfacesToExpose.length; x++) {
               Method methods[] = interfacesToExpose[x].getMethods();
  @@ -98,7 +98,7 @@
           }
   
           // as the main service is lookup-able, it has a prexisting impl.
  -        mPublishedObjects.put(asName + "_Main", mainAsih);
  +        mPublishedObjects.put(asName + "_Main", mainMethodInvocationHandler);
   
           // add method maps for all the additional facades.
           for (int x = 0; x < additionalFacades.length; x++) {
  
  
  
  1.4       +5 -9      jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/test/MemoryLeakClientTest.java
  
  Index: MemoryLeakClientTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/test/MemoryLeakClientTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MemoryLeakClientTest.java	28 Mar 2002 06:17:11 -0000	1.3
  +++ MemoryLeakClientTest.java	31 Mar 2002 21:24:29 -0000	1.4
  @@ -26,7 +26,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.3 $
  + * @version $Revision: 1.4 $
    */
   public class MemoryLeakClientTest {
   
  @@ -40,23 +40,19 @@
       public MemoryLeakClientTest(MemoryLeak ml) {
   
           long start = System.currentTimeMillis();
  -
           for (int i = 0; i < 100000000; i++) {
               MemoryLeak tmpMl = ml.getNewMemoryLeak();
  -            ml.getHugeString();
  +            ml.getHugeString(); // this does not seem to affect speed.
   
               System.gc(); // pointless ?
               if (i%100 == 0) {
  -                long end = System.currentTimeMillis();
                   System.out.println("Iter "
                           + i + ", "
  -                        + (end-start) / 1000
  -                        + " seconds, Tot Mem :"
  -                        + Runtime.getRuntime().totalMemory()
  -                        + ", Free Mem :"
  +                        + ( System.currentTimeMillis() - start) / 1000
  +                        + " seconds, Free Mem :"
                           + Runtime.getRuntime().freeMemory()
                   );
  -                end = start;
  +                start = System.currentTimeMillis();
               }
           }
       }
  
  
  
  1.3       +7 -1      jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/test/MemoryLeakImpl.java
  
  Index: MemoryLeakImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/test/MemoryLeakImpl.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MemoryLeakImpl.java	27 Mar 2002 20:54:55 -0000	1.2
  +++ MemoryLeakImpl.java	31 Mar 2002 21:24:29 -0000	1.3
  @@ -15,7 +15,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.2 $
  + * @version $Revision: 1.3 $
    */
   public class MemoryLeakImpl implements MemoryLeak {
   
  @@ -61,4 +61,10 @@
       public String getHugeString() {
           return mHugeStr;
       }
  +
  +    protected void finalize() throws Throwable {
  +        super.finalize();
  +        //System.out.println("impl finalized");
  +    }
  +
   }
  
  
  
  1.5       +6 -1      jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/test/TestInterfaceImpl.java
  
  Index: TestInterfaceImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/altrmi/src/java/org/apache/commons/altrmi/test/TestInterfaceImpl.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestInterfaceImpl.java	6 Mar 2002 12:52:07 -0000	1.4
  +++ TestInterfaceImpl.java	31 Mar 2002 21:24:29 -0000	1.5
  @@ -22,7 +22,7 @@
    *
    *
    * @author Paul Hammant <a href="mailto:Paul_Hammant@yahoo.com">Paul_Hammant@yahoo.com</a>
  - * @version $Revision: 1.4 $
  + * @version $Revision: 1.5 $
    */
   public class TestInterfaceImpl implements TestInterface {
   
  @@ -239,5 +239,10 @@
           mTestObjects[0] = new TestObject("aAa");
           mTestObjects[1] = new TestObject("bBb");
           mTestObjects[2] = new TestObject("cCc");
  +    }
  +
  +    protected void finalize() throws Throwable {
  +        super.finalize();
  +        System.out.println("impl finalized");
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message