SAMPLE: Clean Up Java Resources Automatically with Gc.exe (231183)



The information in this article applies to:

  • Microsoft Visual J++ 6.0
  • Microsoft virtual machine

This article was previously published under Q231183

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.

The Gc.exe file contains a sample that demonstrates how to automatically clean up deleted Java objects and the resources they hold. It does this by running the garbage collector in the class constructor when the number of allocated objects goes above a specified count.

MORE INFORMATION

The following file is available for download from the Microsoft Download Center:
For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

Codebase.dat 38
Form1.java 3989
Gc.sln 501
Gc.suo 4608
Gc.vjp 4666
Nested.java 656
Waste.java 359

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:
public class CgcCtor{
     public  static final int MaxCnt = 10;
     public static long m_cnt = 0;
    
     public CgcCtor(){
         m_cnt++;
         if (m_cnt > MaxCnt)
             Kleen.doObjectCleanup();
     }
     public void finalize(){
         m_cnt--;
     }
}

class Kleen{
   public static void doObjectCleanup(){
      System.gc();
      System.runFinalization();
      System.gc();
      }

}
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:
System.gc();
System.runFinalization();
System.gc();
				

REFERENCES

For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

Modification Type:MajorLast Reviewed:6/14/2006
Keywords:kbdownload kbfile KB231183 kbAudDeveloper