PRB: COM Objects are not Being Released in Java

Last reviewed: January 29, 1998
Article ID: Q179062
The information in this article applies to:
  • SDK for Java, versions 1.51, 2.0, 2.01
  • Microsoft Visual J++, versions 1.0, 1.1

SYMPTOMS

When setting a COM object reference to null and possibly attempting to force garbage collection by calling gc(), COM objects may not be released in a timely or predictable fashion.

CAUSE

If you do not call ComLib.release, normal garbage-collection will attempt to perform the release for you. However, this mechanism is not guaranteed due to the threading limitations of many COM objects. That is, many COM objects can be called only on the thread on which they were created. Because garbage collection occurs at unpredictable times, the required thread may have expired or may be no longer responding to messages by the time garbage-collection reclaims the object. In addition, this unpredictability can obscure true memory leaks and/or tie up important system resources.

RESOLUTION

It is recommended that you use explicit releases by calling ComLib.release in order to free COM objects in a timely and predictable fashion.

MORE INFORMATION

For example, if you have the following code that creates a COM object in Java:

   import excel8.*;
   import com.ms.com.*;

   _Global globXL=null;
   _Application appXL=null;

   try
   {
      globXL = (_Global)new Global();
      appXL = (_Application)globXL.getApplication();
   }
   catch (ComFailException e)
   {
      System.out.println(e.getMessage());
   }

One would use this to release these COM objects (note all references to COM objects should be released):

   try
   {
      ComLib.release(appXL);
      appXL=null;
      ComLib.release(globXL);
      globXL=null;
   }
   catch (ComFailException e)
   {
      System.out.println(e.getMessage());
   }

REFERENCES

Please see the SDK2.0x for Java documentation for more information.

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:

   http://support.microsoft.com/support/visualj/
   http://support.microsoft.com/support/java/


Additional query words: JCOM garbage collection release com objects

Keywords : JCOM JVM
Technology : kbInetDev internet
Version : WINDOWS:1.0,1.1,1.51,2.0,2.01
Platform : WINDOWS
Issue type : kbprb


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 29, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.