The information in this article applies to:
SUMMARY
When you delete the last reference to a Java object, the object is not immediately freed. Java objects are removed only when the Java garbage collector runs. If your Java class contains limited resources (such as database connections, named pipes, sockets, and so forth), you may run out of resources even though you have only one reference to a limited resource. Contrast this behavior with C++, in which objects are immediately freed when you delete them. MORE INFORMATIONThe following file is available for download from the Microsoft
Software Library. Click the file name below to download the file: Gc.exe
WARNING: Running the Java garbage collector is very expensive. For some applications it may be better to manually clean up objects rather than use the garbage collector to clean up automatically. The disadvantage of manually cleaning up is that it breaks the object-oriented programming (OOP) paradigm. For example, another programmer who adds new resources to your class may clean them up in the finalize routine but forget to do the manual cleanup. The method shown in this sample lets you more closely mimic C++ cleanup. The sample shows how to clean up the object's constructor. You keep a static member that you increment in your constructor and decrement in your finalize method. In your constructor, you check to see if you have exceeded the limit of allocated objects. If you are over the limit, you call the garbage collector from your constructor. The doObjectCleanup() call usually cleans up orphaned objects with the current Microsoft virtual machine (Microsoft VM). NOTE: There is no guarantee that all future implementations of the Microsoft VM will behave similarly or that other virtual machine's will clean up all orphaned objects. The following two complete Java classes show how to limit the number of objects that are not collected by the garbage collector:
After building the GC project, run the program and click the Create button multiple times. The Objects field shows the number of objects in memory (not in the garbage collector), the Objects in array field shows the current number of objects that have not been released -- objects for which there are still references in memory. If you create several objects, the Objects and the Objects in array fields are be the same. Clicking the Delete button frees an object, but the system probably won't run the garbage collector, so the Objects field does not decrement. (If you are very low on virtual memory, the virtual machine may run the garbage collector).Clicking GC button makes a single call to System.gc(). Clicking the Finalize button makes a single call to System.runFinalization(). Clicking the GC/Finalize/GC button makes the calls shown in the doObjectCleanup() method shown earlier in this article. If you select the Create with GC button, the class constructor runs the garbage collector if m_cnt is high enough (more than 10). Note that calling System.gc() is not necessarily enough to clean up all dangling objects. After a single System.gc() call, some objects may simply be marked for deletion in the next garbage collection pass. Following is a better sequence:
REFERENCESFor the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java,
please see the following pages on the Microsoft Technical Support site: http://support.microsoft.com/support/visualj/ © Microsoft Corporation 1999, All Rights Reserved. Additional query words:
Keywords : kbfile kbJavaVM kbVJ kbGrpJava kbDSupport |
Last Reviewed: November 16, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |