BEA Home | Events | Solutions | Partners | Products | Services | Download | Developer Center | WebSUPPORT
| WebLogic Server Doc Home | WebLogic jCOM Reference Guide | Previous Topic | Next Topic | Contents | View as PDF

Garbage Collection and Reference Counting

On COM objects referenced from Java clients

The Java Virtual Machine will perform garbage collection on Java references to a COM object when such references can no longer be accessed.

When such a reference is garbage collected, WebLogic jCOM adds the DCOM object details to an internal list of DCOM object references which should be released. Every ten seconds a WebLogic jCOM daemon thread releases these batched DCOM object references through garbage collection.

If you would prefer to explicitly release an object reference yourself, then call the com.bea.jcom.Cleaner.release(...) method, passing the object reference as a parameter.

When your JVM is about to shut down you should call com.bea.jcom.Cleaner.releaseAll(). This will release any COM object references that have not already been released through garbage collection. Once you have called this method you will no longer be able to make use of any COM object accessed via WebLogic jCOM.

When running in DCOM mode the WebLogic jCOM runtime sends DCOM ping messages per the DCOM protocol to tell the COM server that the client is still alive.

On Java objects referenced from COM clients

When COM clients hold references to a Java object the WebLogic jCOM runtime maintains a reference inside the JVM to that Java object on behalf of the COM clients. It also keeps count of the number of COM references exported to that Java object and releases its reference when the COM reference count reaches zero. When running in DCOM mode WebLogic jCOM receives DCOM ping messages informing it that a COM client is still alive. If no such ping messages are received for six minutes (per the DCOM specification) then the WebLogic jCOM runtime releases all unpinged Java objects.

If you wish to be notified when Java objects are no longer referenced by COM clients, you can call the following method passing in a reference to an instance of a Java class you create that implements the com.bea.jcom.Unreferenced interface:

>     com.bea.jcom.Cleaner.addUnreferencedListener(com.bea.jcom.Unrefer
>     enced listener)

The Unreferenced interface looks like this:

>     public interface Unreferenced {
>     public void objectUnreferenced(Object o);
>     }

When you no longer wish to be notified, call:

>     public static void removeUnreferencedListener(Unreferenced
>     listener)

This is a small example:

>     public class MyJvm {

>     public static void main(String[] args) throws Exception {
>     com.bea.jcom.Jvm.register("firstjvm");

>     MyUnreferencedListener l = new MyUnreferencedListener();
>     com.bea.jcom.Cleaner.addUnreferencedListener(l);

>     Thread.sleep(6000000); // Sleep for an hour

>     com.bea.jcom.Cleaner.removeUnreferencedListener(l);
>     }
>     }

>     class MyUnreferencedListener implements com.bea.jcom.Unreferenced
>     {
>     public void objectUnreferenced(Object o) {
>     System.out.println("** Object no longer referenced: " + o);
>     }
>     }

Copyright © 2001 BEA Systems, Inc. All rights reserved. Required browser: Netscape 4.0 or higher, or Microsoft Internet Explorer 4.0 or higher.